🐛 Duplicated singleton services and playlist ending

This commit is contained in:
Manuel Ruwe 2022-12-17 21:39:52 +01:00
parent ef01e80890
commit f9a376495b
4 changed files with 39 additions and 23 deletions

View File

@ -170,27 +170,44 @@ export class DiscordVoiceService {
`Initialized new instance of Audio Player because it has not been defined yet`, `Initialized new instance of Audio Player because it has not been defined yet`,
); );
this.audioPlayer = createAudioPlayer(); this.audioPlayer = createAudioPlayer();
this.audioPlayer.on('debug', (message) => { this.attachEventListenersToAudioPlayer();
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.voiceConnection.subscribe(this.audioPlayer); this.voiceConnection.subscribe(this.audioPlayer);
return this.audioPlayer; return 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();
});
}
} }

View File

@ -1,17 +1,16 @@
import { DiscordModule } from '@discord-nestjs/core'; import { DiscordModule } from '@discord-nestjs/core';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { DiscordMessageService } from '../clients/discord/discord.message.service';
import { DiscordClientModule } from '../clients/discord/discord.module'; import { DiscordClientModule } from '../clients/discord/discord.module';
import { JellyfinClientModule } from '../clients/jellyfin/jellyfin.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 { CurrentTrackCommand } from './current.command';
import { DisconnectCommand } from './disconnect.command'; import { DisconnectCommand } from './disconnect.command';
import { EnqueueCommand } from './enqueue.command'; import { EnqueueCommand } from './enqueue.command';
import { HelpCommand } from './help.command'; import { HelpCommand } from './help.command';
import { PausePlaybackCommand } from './pause.command'; import { PausePlaybackCommand } from './pause.command';
import { PreviousTrackCommand } from './previous.command';
import { PlayItemCommand } from './play.comands'; import { PlayItemCommand } from './play.comands';
import { PreviousTrackCommand } from './previous.command';
import { SkipTrackCommand } from './skip.command'; import { SkipTrackCommand } from './skip.command';
import { StatusCommand } from './status.command'; import { StatusCommand } from './status.command';
import { StopPlaybackCommand } from './stop.command'; import { StopPlaybackCommand } from './stop.command';
@ -22,6 +21,7 @@ import { SummonCommand } from './summon.command';
DiscordModule.forFeature(), DiscordModule.forFeature(),
JellyfinClientModule, JellyfinClientModule,
DiscordClientModule, DiscordClientModule,
PlaybackModule,
], ],
controllers: [], controllers: [],
providers: [ providers: [
@ -36,8 +36,6 @@ import { SummonCommand } from './summon.command';
SummonCommand, SummonCommand,
PlayItemCommand, PlayItemCommand,
PreviousTrackCommand, PreviousTrackCommand,
DiscordMessageService,
PlaybackService,
], ],
exports: [], exports: [],
}) })

View File

@ -44,7 +44,7 @@ export class CurrentTrackCommand implements DiscordCommand {
3, 3,
)}${formatMillisecondsAsHumanReadable( )}${formatMillisecondsAsHumanReadable(
track.track.durationInMilliseconds, track.track.durationInMilliseconds,
)} ${isCurrent && ' *(active track)*'}`; )} ${isCurrent ? ' *(active track)*' : ''}`;
}) })
.join(',\n'); .join(',\n');

View File

@ -3,6 +3,7 @@ import { AppModule } from './app.module';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
app.enableShutdownHooks();
await app.listen(3000); await app.listen(3000);
} }
bootstrap(); bootstrap();