From 78399a77ae9cab964e075e95e7b1a6073071122b Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Tue, 5 Apr 2016 11:42:43 +1000 Subject: [PATCH] Handle client being moved around as well --- lib/Client/InternalClient.js | 58 ++++++++++++++++++++++-------------- src/Client/InternalClient.js | 37 +++++++++++++++-------- 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 1d97be0bf..1cc511aa9 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -320,6 +320,18 @@ var InternalClient = (function () { return Promise.reject(new Error("channel is not a voice channel!")); } + var joinSendWS = function joinSendWS() { + _this4.sendWS({ + op: 4, + d: { + "guild_id": channel.server.id, + "channel_id": channel.id, + "self_mute": false, + "self_deaf": false + } + }); + }; + var joinVoice = new Promise(function (resolve, reject) { var session, token, @@ -332,41 +344,31 @@ var InternalClient = (function () { if (data.t === "VOICE_STATE_UPDATE") { session = data.d.session_id; - if (existingServerConn && data.d.channel_id !== existingServerConn.voiceChannel.id) { - existingServerConn.voiceChannel = channel; // existing connection to that server, just channel changed - } } else if (data.t === "VOICE_SERVER_UPDATE") { - token = data.d.token; - endpoint = data.d.endpoint; - var chan = new _VoiceVoiceConnection2["default"](channel, _this4.client, session, token, server, endpoint); - _this4.voiceConnections.add(chan); + token = data.d.token; + endpoint = data.d.endpoint; + var chan = new _VoiceVoiceConnection2["default"](channel, _this4.client, session, token, server, endpoint); + _this4.voiceConnections.add(chan); - chan.on("ready", function () { - return resolve(chan); - }); - chan.on("error", reject); + chan.on("ready", function () { + return resolve(chan); + }); + chan.on("error", reject); - _this4.client.emit("debug", "removed temporary voice websocket listeners"); - _this4.websocket.removeListener("message", check); - } + _this4.client.emit("debug", "removed temporary voice websocket listeners"); + _this4.websocket.removeListener("message", check); + } }; _this4.websocket.on("message", check); - _this4.sendWS({ - op: 4, - d: { - "guild_id": server.id, - "channel_id": channel.id, - "self_mute": false, - "self_deaf": false - } - }); + joinSendWS(); }); if (!_this4.user.bot && _this4.voiceConnections.length > 0) // nonbot, one voiceconn only, just like last time just disconnect return _this4.leaveVoiceChannel(_this4.voiceConnections[0]).then(joinVoice); var existingServerConn = _this4.voiceConnections.get("server", channel.server); // same server connection + if (existingServerConn) joinSendWS(); // Just needs to update by sending via WS, movement in cache will be handled by global handler return joinVoice; }); @@ -2044,6 +2046,7 @@ var InternalClient = (function () { case _Constants.PacketType.VOICE_STATE_UPDATE: var user = self.users.get("id", data.user_id); var server = self.servers.get("id", data.guild_id); + var connection = self.voiceConnections.get("server", server); if (user && server) { @@ -2063,6 +2066,15 @@ var InternalClient = (function () { client.emit("warn", "voice state update but user or server not in cache"); } + if (connection) { + // existing connection, perhaps channel moved + if (connection.voiceChannel.id !== data.channel_id) { + // moved, update info + connection.voiceChannel = self.channels.get("id", data.channel_id); + client.emit("voiceMoved", connection.voiceChannel); // Moved to a new channel + } + } + break; case _Constants.PacketType.SERVER_MEMBERS_CHUNK: diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index a8f84023f..f1c576847 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -246,6 +246,18 @@ export default class InternalClient { return Promise.reject(new Error("channel is not a voice channel!")); } + var joinSendWS = () => { + this.sendWS({ + op: 4, + d: { + "guild_id": channel.server.id, + "channel_id": channel.id, + "self_mute": false, + "self_deaf": false + } + }); + } + var joinVoice = new Promise((resolve, reject) => { var session, token, server = channel.server, endpoint; @@ -255,9 +267,6 @@ export default class InternalClient { if (data.t === "VOICE_STATE_UPDATE") { session = data.d.session_id; - if (existingServerConn && data.d.channel_id !== existingServerConn.voiceChannel.id) { - existingServerConn.voiceChannel = channel; // existing connection to that server, just channel changed. it is the same connection, just a different channel - } } else if (data.t === "VOICE_SERVER_UPDATE") { token = data.d.token; endpoint = data.d.endpoint; @@ -275,21 +284,15 @@ export default class InternalClient { }; this.websocket.on("message", check); - this.sendWS({ - op: 4, - d: { - "guild_id": server.id, - "channel_id": channel.id, - "self_mute": false, - "self_deaf": false - } - }); + joinSendWS(); }); if (!this.user.bot && this.voiceConnections.length > 0) // nonbot, one voiceconn only, just like last time just disconnect return this.leaveVoiceChannel(this.voiceConnections[0]).then(joinVoice); var existingServerConn = this.voiceConnections.get("server", channel.server); // same server connection + if (existingServerConn) + joinSendWS(); // Just needs to update by sending via WS, movement in cache will be handled by global handler return joinVoice; }); @@ -1731,6 +1734,7 @@ export default class InternalClient { case PacketType.VOICE_STATE_UPDATE: var user = self.users.get("id", data.user_id); var server = self.servers.get("id", data.guild_id); + var connection = self.voiceConnections.get("server", server); if (user && server) { @@ -1751,6 +1755,15 @@ export default class InternalClient { client.emit("warn", "voice state update but user or server not in cache"); } + if (connection) { + // existing connection, perhaps channel moved + if (connection.voiceChannel.id !== data.channel_id) { + // moved, update info + connection.voiceChannel = self.channels.get("id", data.channel_id); + client.emit("voiceMoved", connection.voiceChannel); // Moved to a new channel + } + } + break; case PacketType.SERVER_MEMBERS_CHUNK: