Skip to content

Commit

Permalink
Improve VoiceConnection disconnect handling
Browse files Browse the repository at this point in the history
  • Loading branch information
abalabahaha committed Apr 25, 2021
1 parent c3d491d commit 95cffe4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
12 changes: 6 additions & 6 deletions lib/gateway/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,12 @@ class Shard extends EventEmitter {
break;
}
case "VOICE_STATE_UPDATE": { // (╯°□°)╯︵ ┻━┻
if(packet.d.guild_id && packet.d.user_id === this.client.user.id) {
const voiceConnection = this.client.voiceConnections.get(packet.d.guild_id);
if(voiceConnection && voiceConnection.channelID !== packet.d.channel_id) {
voiceConnection.switchChannel(packet.d.channel_id, true);
}
}
if(packet.d.guild_id === undefined) {
packet.d.id = packet.d.user_id;
if(packet.d.channel_id === null) {
Expand Down Expand Up @@ -740,12 +746,6 @@ class Shard extends EventEmitter {
};
const oldChannelID = member.voiceState.channelID;
member.update(packet.d, this.client);
if(member.user.id === this.client.user.id) {
const voiceConnection = this.client.voiceConnections.get(packet.d.guild_id);
if(voiceConnection && voiceConnection.channelID !== packet.d.channel_id) {
voiceConnection.switchChannel(packet.d.channel_id, true);
}
}
if(oldChannelID != packet.d.channel_id) {
let oldChannel, newChannel;
if(oldChannelID) {
Expand Down
60 changes: 51 additions & 9 deletions lib/voice/VoiceConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class VoiceConnection extends EventEmitter {
this.timestamp = 0;
this.ssrcUserMap = {};
this.connectionTimeout = null;
this.connecting = false;
this.reconnecting = false;
this.ready = false;

this.nonce = Buffer.alloc(24);

Expand Down Expand Up @@ -151,7 +154,12 @@ class VoiceConnection extends EventEmitter {
this.connecting = true;
if(this.ws && this.ws.readyState !== WebSocket.CLOSED) {
this.disconnect(undefined, true);
return setTimeout(() => this.connect(data), 500);
setTimeout(() => {
if(!this.connecting && !this.ready) {
this.connect(data);
}
}, 500);
return;
}
clearTimeout(this.connectionTimeout);
this.connectionTimeout = setTimeout(() => {
Expand Down Expand Up @@ -269,6 +277,7 @@ class VoiceConnection extends EventEmitter {
this.secret[i] = packet.d.secret_key[i];
}
this.connecting = false;
this.reconnecting = false;
this.ready = true;
/**
* Fired when the voice connection turns ready
Expand Down Expand Up @@ -337,29 +346,49 @@ class VoiceConnection extends EventEmitter {
if(code === 4006) {
reconnecting = false;
} else if(code === 4014) {
reconnecting = false;
if(this.channelID) {
data.endpoint = null;
reconnecting = true;
err = null;
} else {
reconnecting = false;
}
} else if(code === 1000) {
reconnecting = false;
}
this.disconnect(err, reconnecting);
if(reconnecting) {
setTimeout(() => this.connect(data), 500);
setTimeout(() => {
if(!this.connecting && !this.ready) {
this.connect(data);
}
}, 500);
}
}
});
}

disconnect(error, reconnecting) {
this.connecting = false;
this.reconnecting = reconnecting;
this.ready = false;
this.speaking = false;
this.timestamp = 0;
this.sequence = 0;

if(reconnecting) {
this.pause();
} else {
this.stopPlaying();
if(this.connectionTimeout) {
clearTimeout(this.connectionTimeout);
this.connectionTimeout = null;
}

try {
if(reconnecting) {
this.pause();
} else {
this.stopPlaying();
}
} catch(err) {
this.emit("error", err);
}
if(this.heartbeatInterval) {
clearInterval(this.heartbeatInterval);
Expand All @@ -377,7 +406,16 @@ class VoiceConnection extends EventEmitter {
}
if(this.ws) {
try {
this.ws.close();
if(reconnecting) {
if(this.ws.readyState === WebSocket.OPEN) {
this.ws.close(4901, "Eris: reconnect");
} else {
this.emit("debug", `Terminating websocket (state: ${this.ws.readyState})`);
this.ws.terminate();
}
} else {
this.ws.close(1000, "Eris: normal");
}
} catch(err) {
this.emit("error", err);
}
Expand Down Expand Up @@ -658,7 +696,11 @@ class VoiceConnection extends EventEmitter {
}

this.channelID = channelID;
if(!reactive) {
if(reactive) {
if(this.reconnecting && !channelID) {
this.disconnect();
}
} else {
this.updateVoiceState();
}
}
Expand Down

0 comments on commit 95cffe4

Please sign in to comment.