diff --git a/src/client/voice/VoiceConnection.js b/src/client/voice/VoiceConnection.js index d3b785cc3..4c4701feb 100644 --- a/src/client/voice/VoiceConnection.js +++ b/src/client/voice/VoiceConnection.js @@ -429,7 +429,7 @@ class VoiceConnection extends EventEmitter { speaking = Boolean(speaking); const guild = this.channel.guild; const user = this.client.users.get(user_id); - this.ssrcMap.set(+ssrc, user); + this.ssrcMap.set(+ssrc, user_id); /** * Emitted whenever a user starts/stops speaking. * @event VoiceConnection#speaking diff --git a/src/client/voice/networking/VoiceWebSocket.js b/src/client/voice/networking/VoiceWebSocket.js index 08ee114a9..087203e63 100644 --- a/src/client/voice/networking/VoiceWebSocket.js +++ b/src/client/voice/networking/VoiceWebSocket.js @@ -9,20 +9,20 @@ const WebSocket = require('../../../WebSocket'); * @private */ class VoiceWebSocket extends EventEmitter { - constructor(voiceConnection) { + constructor(connection) { super(); /** * The client of this voice WebSocket * @type {Client} */ - this.client = voiceConnection.voiceManager.client; + this.client = connection.voiceManager.client; /** * The Voice Connection that this WebSocket serves * @type {VoiceConnection} */ - this.voiceConnection = voiceConnection; + this.connection = connection; /** * How many connection attempts have been made @@ -32,7 +32,7 @@ class VoiceWebSocket extends EventEmitter { this.connect(); this.dead = false; - this.voiceConnection.on('closing', this.shutdown.bind(this)); + this.connection.on('closing', this.shutdown.bind(this)); } shutdown() { @@ -68,7 +68,7 @@ class VoiceWebSocket extends EventEmitter { * The actual WebSocket used to connect to the Voice WebSocket Server. * @type {WebSocket} */ - this.ws = WebSocket.create(`wss://${this.voiceConnection.authentication.endpoint}/`, { v: 4 }); + this.ws = WebSocket.create(`wss://${this.connection.authentication.endpoint}/`, { v: 4 }); this.ws.onopen = this.onOpen.bind(this); this.ws.onmessage = this.onMessage.bind(this); this.ws.onclose = this.onClose.bind(this); @@ -110,10 +110,10 @@ class VoiceWebSocket extends EventEmitter { this.sendPacket({ op: OPCodes.DISPATCH, d: { - server_id: this.voiceConnection.channel.guild.id, + server_id: this.connection.channel.guild.id, user_id: this.client.user.id, - token: this.voiceConnection.authentication.token, - session_id: this.voiceConnection.authentication.sessionID, + token: this.connection.authentication.token, + session_id: this.connection.authentication.sessionID, }, }).catch(() => { this.emit('error', new Error('VOICE_JOIN_SOCKET_CLOSED')); @@ -177,6 +177,15 @@ class VoiceWebSocket extends EventEmitter { */ this.emit('sessionDescription', packet.d.mode, key); break; + case VoiceOPCodes.CLIENT_CONNECT: + this.connection.ssrcMap.set(+packet.d.audio_ssrc, packet.d.user_id); + break; + case VoiceOPCodes.CLIENT_DISCONNECT: + for (const receiver of this.connection.receivers) { + const streamInfo = receiver.packets.streams.get(packet.d.user_id); + if (streamInfo) streamInfo.stream.push(null); + } + break; case VoiceOPCodes.SPEAKING: /** * Emitted whenever a speaking packet is received. diff --git a/src/client/voice/receiver/PacketHandler.js b/src/client/voice/receiver/PacketHandler.js index 1931f06cf..03b5b6f69 100644 --- a/src/client/voice/receiver/PacketHandler.js +++ b/src/client/voice/receiver/PacketHandler.js @@ -11,11 +11,13 @@ class PacketHandler extends EventEmitter { this.streams = new Map(); } + get connection() { + return this.receiver.connection; + } + _stoppedSpeaking(userID) { - if (this.streams.has(userID)) { - const { stream, end } = this.streams.get(userID); - if (end === 'silence') stream.push(null); - } + const streamInfo = this.streams.get(userID); + if (streamInfo && streamInfo.end === 'silence') streamInfo.stream.push(null); } makeStream(user, end) { @@ -63,7 +65,7 @@ class PacketHandler extends EventEmitter { return packet; } - userFromSSRC(ssrc) { return this.receiver.connection.ssrcMap.get(ssrc); } + userFromSSRC(ssrc) { return this.connection.client.users.get(this.connection.ssrcMap.get(ssrc)); } push(buffer) { const ssrc = buffer.readUInt32BE(8); diff --git a/src/util/Constants.js b/src/util/Constants.js index 7a3c2bcca..79f95881b 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -198,6 +198,8 @@ exports.VoiceOPCodes = { SESSION_DESCRIPTION: 4, SPEAKING: 5, HELLO: 8, + CLIENT_CONNECT: 12, + CLIENT_DISCONNECT: 13, }; exports.Events = {