diff options
| author | Johannes Vääräkangas <[email protected]> | 2022-02-12 04:05:02 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-02-11 20:05:02 -0600 |
| commit | 4dbb55a72119a61033d2df6e8cf46f5eaa7bba15 (patch) | |
| tree | c23bfee7a2c18a18ac8536a6b31e0e22eb1d9c6e /src/services | |
| parent | 8e5b3cfa432abc4b3e2ef76a139da2586ccb1213 (diff) | |
| download | muse-4dbb55a72119a61033d2df6e8cf46f5eaa7bba15.tar.xz muse-4dbb55a72119a61033d2df6e8cf46f5eaa7bba15.zip | |
Configurable voice channel leave behavior (#514)
Co-authored-by: Max Isom <[email protected]>
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/add-query-to-queue.ts | 5 | ||||
| -rw-r--r-- | src/services/player.ts | 32 |
2 files changed, 34 insertions, 3 deletions
diff --git a/src/services/add-query-to-queue.ts b/src/services/add-query-to-queue.ts index bb512c6..16d2f43 100644 --- a/src/services/add-query-to-queue.ts +++ b/src/services/add-query-to-queue.ts @@ -4,7 +4,7 @@ import {Except} from 'type-fest'; import shuffle from 'array-shuffle'; import {TYPES} from '../types.js'; import GetSongs from '../services/get-songs.js'; -import {QueuedSong} from './player.js'; +import {QueuedSong, STATUS} from './player.js'; import PlayerManager from '../managers/player.js'; import {prisma} from '../utils/db.js'; import {buildPlayingMessageEmbed} from '../utils/build-embed.js'; @@ -131,6 +131,9 @@ export default class AddQueryToQueue { await interaction.editReply({ embeds: [buildPlayingMessageEmbed(player)], }); + } else if (player.status === STATUS.IDLE) { + // Player is idle, start playback instead + await player.play(); } // Build response message diff --git a/src/services/player.ts b/src/services/player.ts index ce611e8..79ef2bb 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -8,6 +8,7 @@ import shuffle from 'array-shuffle'; import {AudioPlayer, AudioPlayerStatus, createAudioPlayer, createAudioResource, joinVoiceChannel, StreamType, VoiceConnection, VoiceConnectionStatus} from '@discordjs/voice'; import FileCacheProvider from './file-cache.js'; import debug from '../utils/debug.js'; +import {prisma} from '../utils/db.js'; export interface QueuedPlaylist { title: string; @@ -29,6 +30,7 @@ export interface QueuedSong { export enum STATUS { PLAYING, PAUSED, + IDLE, } export interface PlayerEvents { @@ -50,6 +52,7 @@ export default class { private positionInSeconds = 0; private readonly fileCache: FileCacheProvider; + private disconnectTimer: NodeJS.Timeout | null = null; constructor(fileCache: FileCacheProvider, guildId: string) { this.fileCache = fileCache; @@ -131,6 +134,12 @@ export default class { throw new Error('Queue empty.'); } + // Cancel any pending idle disconnection + if (this.disconnectTimer) { + clearInterval(this.disconnectTimer); + this.disconnectTimer = null; + } + // Resume from paused state if (this.status === STATUS.PAUSED && currentSong.url === this.nowPlaying?.url) { if (this.audioPlayer) { @@ -206,12 +215,31 @@ export default class { async forward(skip: number): Promise<void> { this.manualForward(skip); + console.log(this.getCurrent()); + try { if (this.getCurrent() && this.status !== STATUS.PAUSED) { await this.play(); } else { - this.status = STATUS.PAUSED; - this.disconnect(); + this.audioPlayer?.stop(); + this.status = STATUS.IDLE; + + const settings = await prisma.setting.findUnique({where: {guildId: this.guildId}}); + + if (!settings) { + throw new Error('Could not find settings for guild'); + } + + const {secondsToWaitAfterQueueEmpties} = settings; + if (secondsToWaitAfterQueueEmpties !== 0) { + this.disconnectTimer = setTimeout(() => { + // Make sure we are not accidentally playing + // when disconnecting + if (this.status === STATUS.IDLE) { + this.disconnect(); + } + }, secondsToWaitAfterQueueEmpties * 1000); + } } } catch (error: unknown) { this.queuePosition--; |
