Skip to content

Commit

Permalink
fix(Voice*): internally disconnect and cleanup when forcibly disconne…
Browse files Browse the repository at this point in the history
…cted (#3597)
  • Loading branch information
SpaceEEC authored Jan 5, 2020
1 parent 1d66062 commit cbabc16
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/client/voice/ClientVoiceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ class ClientVoiceManager {

onVoiceStateUpdate({ guild_id, session_id, channel_id }) {
const connection = this.connections.get(guild_id);
if (connection) {
connection.channel = this.client.channels.get(channel_id);
connection.setSessionID(session_id);
if (!connection) return;
if (!channel_id) {
connection._disconnect();
this.connections.delete(guild_id);
return;
}

connection.channel = this.client.channels.get(channel_id);
connection.setSessionID(session_id);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/client/voice/VoiceConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,15 @@ class VoiceConnection extends EventEmitter {
this.sendVoiceStateUpdate({
channel_id: null,
});

this._disconnect();
}

/**
* Internally disconnects (doesn't send disconnect packet).
* @private
*/
_disconnect() {
this.player.destroy();
this.cleanup();
this.status = Constants.VoiceStatus.DISCONNECTED;
Expand All @@ -334,6 +343,7 @@ class VoiceConnection extends EventEmitter {
ws.removeAllListeners('ready');
ws.removeAllListeners('sessionDescription');
ws.removeAllListeners('speaking');
ws.shutdown();
}

if (udp) udp.removeAllListeners('error');
Expand Down
2 changes: 1 addition & 1 deletion src/client/websocket/packets/handlers/VoiceStateUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class VoiceStateUpdateHandler extends AbstractHandler {
// If the member left the voice channel, unset their speaking property
if (!data.channel_id) member.speaking = null;

if (member.user.id === client.user.id && data.channel_id) {
if (member.user.id === client.user.id) {
client.emit('self.voiceStateUpdate', data);
}

Expand Down

0 comments on commit cbabc16

Please sign in to comment.