diff --git a/src/Player.ts b/src/Player.ts index eeeca68..5d737c2 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -69,6 +69,29 @@ class Player extends EventEmitter { const queue = this.getQueue(oldState.guild.id); if (!queue) return; + // update channel + if (oldState.channelID && newState.channelID && oldState.channelID !== newState.channelID) { + queue.connection.channel = newState.channel; + } + + if (!oldState.channelID && newState.channelID && newState.member.id === newState.guild.me.id) { + if (newState.serverMute || !newState.serverMute) { + queue.setPaused(newState.serverMute); + } else if (newState.suppress || !newState.suppress) { + if (newState.suppress) newState.guild.me.voice.setRequestToSpeak(true).catch(Util.noop); + queue.setPaused(newState.suppress); + } + } + + if (oldState.channelID === newState.channelID && oldState.member.id === newState.guild.me.id) { + if (oldState.serverMute !== newState.serverMute) { + queue.setPaused(newState.serverMute); + } else if (oldState.suppress !== newState.suppress) { + if (newState.suppress) newState.guild.me.voice.setRequestToSpeak(true).catch(Util.noop); + queue.setPaused(newState.suppress); + } + } + if (oldState.member.id === this.client.user.id && !newState.channelID) { queue.destroy(); return void this.emit("botDisconnect", queue); diff --git a/src/Structures/Queue.ts b/src/Structures/Queue.ts index b3652bd..bcb44dc 100644 --- a/src/Structures/Queue.ts +++ b/src/Structures/Queue.ts @@ -135,8 +135,9 @@ class Queue { this.connection = connection; if (channel.type === "stage") { - await channel.guild.me.voice.setRequestToSpeak(true).catch(Util.noop); - await channel.guild.me.voice.setSuppressed(false).catch(Util.noop); + await channel.guild.me.voice.setSuppressed(false).catch(async () => { + return await channel.guild.me.voice.setRequestToSpeak(true).catch(Util.noop); + }); } this.connection.on("error", (err) => this.player.emit("connectionError", this, err)); @@ -146,7 +147,7 @@ class Queue { this.connection.on("start", (resource) => { this.playing = true; - if (!this._filtersUpdate) this.player.emit("trackStart", this, resource?.metadata ?? this.current); + if (!this._filtersUpdate && resource?.metadata) this.player.emit("trackStart", this, resource?.metadata ?? this.current); this._filtersUpdate = false; }); @@ -154,7 +155,7 @@ class Queue { this.playing = false; if (this._filtersUpdate) return; this._streamTime = 0; - if (resource) this.previousTracks.push(resource.metadata); + if (resource && resource.metadata) this.previousTracks.push(resource.metadata); if (!this.tracks.length && this.repeatMode === QueueRepeatMode.OFF) { if (this.options.leaveOnEnd) this.destroy(); diff --git a/src/VoiceInterface/BasicStreamDispatcher.ts b/src/VoiceInterface/BasicStreamDispatcher.ts index edacff7..ddcb1d7 100644 --- a/src/VoiceInterface/BasicStreamDispatcher.ts +++ b/src/VoiceInterface/BasicStreamDispatcher.ts @@ -27,9 +27,10 @@ export interface VoiceEvents { class StreamDispatcher extends EventEmitter { public readonly voiceConnection: VoiceConnection; public readonly audioPlayer: AudioPlayer; - public readonly channel: VoiceChannel | StageChannel; + public channel: VoiceChannel | StageChannel; public audioResource?: AudioResource; private readyLock = false; + public paused: boolean; /** * Creates new connection object @@ -58,6 +59,12 @@ class StreamDispatcher extends EventEmitter { */ this.channel = channel; + /** + * The paused state + * @type {boolean} + */ + this.paused = false; + this.voiceConnection.on("stateChange", async (_, newState) => { if (newState.status === VoiceConnectionStatus.Disconnected) { if (newState.reason === VoiceConnectionDisconnectReason.WebSocketClose && newState.closeCode === 4014) { @@ -153,6 +160,7 @@ class StreamDispatcher extends EventEmitter { */ pause(interpolateSilence?: boolean) { const success = this.audioPlayer.pause(interpolateSilence); + this.paused = success; return success; } @@ -162,6 +170,7 @@ class StreamDispatcher extends EventEmitter { */ resume() { const success = this.audioPlayer.unpause(); + this.paused = !success; return success; } @@ -210,14 +219,6 @@ class StreamDispatcher extends EventEmitter { if (!this.audioResource) return 0; return this.audioResource.playbackDuration; } - - /** - * The paused state - * @type {boolean} - */ - get paused() { - return [AudioPlayerStatus.AutoPaused, AudioPlayerStatus.Paused].includes(this.audioPlayer.state.status); - } } export { StreamDispatcher as StreamDispatcher };