handle voice states
This commit is contained in:
parent
a0b04d7a62
commit
fb864c08ac
3 changed files with 38 additions and 13 deletions
|
@ -69,6 +69,29 @@ class Player extends EventEmitter<PlayerEvents> {
|
|||
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);
|
||||
|
|
|
@ -135,8 +135,9 @@ class Queue<T = unknown> {
|
|||
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<T = unknown> {
|
|||
|
||||
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<T = unknown> {
|
|||
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();
|
||||
|
|
|
@ -27,9 +27,10 @@ export interface VoiceEvents {
|
|||
class StreamDispatcher extends EventEmitter<VoiceEvents> {
|
||||
public readonly voiceConnection: VoiceConnection;
|
||||
public readonly audioPlayer: AudioPlayer;
|
||||
public readonly channel: VoiceChannel | StageChannel;
|
||||
public channel: VoiceChannel | StageChannel;
|
||||
public audioResource?: AudioResource<Track>;
|
||||
private readyLock = false;
|
||||
public paused: boolean;
|
||||
|
||||
/**
|
||||
* Creates new connection object
|
||||
|
@ -58,6 +59,12 @@ class StreamDispatcher extends EventEmitter<VoiceEvents> {
|
|||
*/
|
||||
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<VoiceEvents> {
|
|||
*/
|
||||
pause(interpolateSilence?: boolean) {
|
||||
const success = this.audioPlayer.pause(interpolateSilence);
|
||||
this.paused = success;
|
||||
return success;
|
||||
}
|
||||
|
||||
|
@ -162,6 +170,7 @@ class StreamDispatcher extends EventEmitter<VoiceEvents> {
|
|||
*/
|
||||
resume() {
|
||||
const success = this.audioPlayer.unpause();
|
||||
this.paused = !success;
|
||||
return success;
|
||||
}
|
||||
|
||||
|
@ -210,14 +219,6 @@ class StreamDispatcher extends EventEmitter<VoiceEvents> {
|
|||
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 };
|
||||
|
|
Loading…
Reference in a new issue