♻️ Audio player events

This commit is contained in:
Manuel 2023-03-01 19:32:23 +01:00
parent 308ca66474
commit 09a67b087a

View File

@ -7,6 +7,7 @@ import {
getVoiceConnection, getVoiceConnection,
getVoiceConnections, getVoiceConnections,
joinVoiceChannel, joinVoiceChannel,
NoSubscriberBehavior,
VoiceConnection, VoiceConnection,
} from '@discordjs/voice'; } from '@discordjs/voice';
@ -200,7 +201,12 @@ export class DiscordVoiceService {
this.logger.debug( this.logger.debug(
`Initialized new instance of AudioPlayer because it has not been defined yet`, `Initialized new instance of AudioPlayer because it has not been defined yet`,
); );
this.audioPlayer = createAudioPlayer(); this.audioPlayer = createAudioPlayer({
debug: process.env.DEBUG?.toLowerCase() === 'true',
behaviors: {
noSubscriber: NoSubscriberBehavior.Play,
},
});
this.attachEventListenersToAudioPlayer(); this.attachEventListenersToAudioPlayer();
this.voiceConnection.subscribe(this.audioPlayer); this.voiceConnection.subscribe(this.audioPlayer);
return this.audioPlayer; return this.audioPlayer;
@ -217,6 +223,10 @@ export class DiscordVoiceService {
this.logger.error(message); this.logger.error(message);
}); });
this.audioPlayer.on('stateChange', (previousState) => { this.audioPlayer.on('stateChange', (previousState) => {
this.logger.debug(
`Audio player changed state from ${previousState.status} to ${this.audioPlayer.state.status}`,
);
if (previousState.status !== AudioPlayerStatus.Playing) { if (previousState.status !== AudioPlayerStatus.Playing) {
return; return;
} }
@ -225,14 +235,14 @@ export class DiscordVoiceService {
return; return;
} }
this.logger.debug(`Audio player finished playing old resource`);
const hasNextTrack = this.playbackService const hasNextTrack = this.playbackService
.getPlaylistOrDefault() .getPlaylistOrDefault()
.hasNextTrackInPlaylist(); .hasNextTrackInPlaylist();
this.logger.debug( this.logger.debug(
`Deteced audio player status change from ${previousState.status} to ${ `Playlist has next track: ${hasNextTrack ? 'yes' : 'no'}`,
this.audioPlayer.state.status
}. Has next track: ${hasNextTrack ? 'yes' : 'no'}`,
); );
if (!hasNextTrack) { if (!hasNextTrack) {