Handle unexpected disconnects via packets (#1521)

This commit is contained in:
aemino
2017-07-26 01:10:21 -07:00
committed by Crawl
parent 4342ed29a8
commit e29a3ec08b
2 changed files with 18 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
const Collection = require('../../util/Collection'); const Collection = require('../../util/Collection');
const Constants = require('../../util/Constants');
const VoiceConnection = require('./VoiceConnection'); const VoiceConnection = require('./VoiceConnection');
const { Error } = require('../../errors'); const { Error } = require('../../errors');
@@ -31,10 +32,13 @@ class ClientVoiceManager {
onVoiceStateUpdate({ guild_id, session_id, channel_id }) { onVoiceStateUpdate({ guild_id, session_id, channel_id }) {
const connection = this.connections.get(guild_id); const connection = this.connections.get(guild_id);
if (connection) { if (!connection) return;
connection.channel = this.client.channels.get(channel_id); if (!channel_id && connection.status !== Constants.VoiceStatus.DISCONNECTED) {
connection.setSessionID(session_id); connection._disconnect();
return;
} }
connection.channel = this.client.channels.get(channel_id);
connection.setSessionID(session_id);
} }
/** /**

View File

@@ -307,7 +307,14 @@ class VoiceConnection extends EventEmitter {
this.sendVoiceStateUpdate({ this.sendVoiceStateUpdate({
channel_id: null, channel_id: null,
}); });
this.player.destroy(); this._disconnect();
}
/**
* Internally disconnects (doesn't send disconnect packet.)
* @private
*/
_disconnect() {
this.cleanup(); this.cleanup();
this.status = Constants.VoiceStatus.DISCONNECTED; this.status = Constants.VoiceStatus.DISCONNECTED;
/** /**
@@ -317,11 +324,14 @@ class VoiceConnection extends EventEmitter {
this.emit('disconnect'); this.emit('disconnect');
} }
/** /**
* Cleans up after disconnect. * Cleans up after disconnect.
* @private * @private
*/ */
cleanup() { cleanup() {
this.player.destroy();
const { ws, udp } = this.sockets; const { ws, udp } = this.sockets;
if (ws) { if (ws) {