diff --git a/src/clients/discord/discord.voice.service.ts b/src/clients/discord/discord.voice.service.ts index f5ad7e7..4464091 100644 --- a/src/clients/discord/discord.voice.service.ts +++ b/src/clients/discord/discord.voice.service.ts @@ -170,27 +170,44 @@ export class DiscordVoiceService { `Initialized new instance of Audio Player because it has not been defined yet`, ); this.audioPlayer = createAudioPlayer(); - this.audioPlayer.on('debug', (message) => { - this.logger.debug(message); - }); - this.audioPlayer.on('error', (message) => { - this.logger.error(message); - }); - this.audioPlayer.on('stateChange', (statusChange) => { - if (statusChange.status !== AudioPlayerStatus.AutoPaused) { - return; - } - - if (!this.playbackService.hasNextTrack()) { - return; - } - - this.playbackService.nextTrack(); - }); + this.attachEventListenersToAudioPlayer(); this.voiceConnection.subscribe(this.audioPlayer); return this.audioPlayer; } return this.audioPlayer; } + + private attachEventListenersToAudioPlayer() { + this.audioPlayer.on('debug', (message) => { + this.logger.debug(message); + }); + this.audioPlayer.on('error', (message) => { + this.logger.error(message); + }); + this.audioPlayer.on('stateChange', (previousState) => { + if (previousState.status !== AudioPlayerStatus.Playing) { + return; + } + + if (this.audioPlayer.state.status !== AudioPlayerStatus.Idle) { + return; + } + + const hasNextTrack = this.playbackService.hasNextTrack(); + + this.logger.debug( + `Deteced audio player status change from ${previousState.status} to ${ + this.audioPlayer.state.status + }. Has next track: ${hasNextTrack ? 'yes' : 'no'}`, + ); + + if (!hasNextTrack) { + this.logger.debug(`Audio Player has reached the end of the playlist`); + return; + } + + this.playbackService.nextTrack(); + }); + } } diff --git a/src/commands/command.module.ts b/src/commands/command.module.ts index 1dc13bd..3963aca 100644 --- a/src/commands/command.module.ts +++ b/src/commands/command.module.ts @@ -1,17 +1,16 @@ import { DiscordModule } from '@discord-nestjs/core'; import { Module } from '@nestjs/common'; -import { DiscordMessageService } from '../clients/discord/discord.message.service'; import { DiscordClientModule } from '../clients/discord/discord.module'; import { JellyfinClientModule } from '../clients/jellyfin/jellyfin.module'; -import { PlaybackService } from '../playback/playback.service'; +import { PlaybackModule } from '../playback/playback.module'; import { CurrentTrackCommand } from './current.command'; import { DisconnectCommand } from './disconnect.command'; import { EnqueueCommand } from './enqueue.command'; import { HelpCommand } from './help.command'; import { PausePlaybackCommand } from './pause.command'; -import { PreviousTrackCommand } from './previous.command'; import { PlayItemCommand } from './play.comands'; +import { PreviousTrackCommand } from './previous.command'; import { SkipTrackCommand } from './skip.command'; import { StatusCommand } from './status.command'; import { StopPlaybackCommand } from './stop.command'; @@ -22,6 +21,7 @@ import { SummonCommand } from './summon.command'; DiscordModule.forFeature(), JellyfinClientModule, DiscordClientModule, + PlaybackModule, ], controllers: [], providers: [ @@ -36,8 +36,6 @@ import { SummonCommand } from './summon.command'; SummonCommand, PlayItemCommand, PreviousTrackCommand, - DiscordMessageService, - PlaybackService, ], exports: [], }) diff --git a/src/commands/current.command.ts b/src/commands/current.command.ts index 42e67c8..be4db65 100644 --- a/src/commands/current.command.ts +++ b/src/commands/current.command.ts @@ -44,7 +44,7 @@ export class CurrentTrackCommand implements DiscordCommand { 3, )}${formatMillisecondsAsHumanReadable( track.track.durationInMilliseconds, - )} ${isCurrent && ' *(active track)*'}`; + )} ${isCurrent ? ' *(active track)*' : ''}`; }) .join(',\n'); diff --git a/src/main.ts b/src/main.ts index 13cad38..4c1b409 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); + app.enableShutdownHooks(); await app.listen(3000); } bootstrap();