From 5619e97a3950f1dd939d29f6567a5c0ad6216807 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Tue, 5 Apr 2016 12:22:50 +1000 Subject: [PATCH] Minor fixes for d/c handler and non-bot accounts --- lib/Client/InternalClient.js | 311 ++++++++++++++++++----------------- src/Client/InternalClient.js | 67 ++++---- 2 files changed, 198 insertions(+), 180 deletions(-) diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 1cc511aa9..fd9e12670 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -217,11 +217,15 @@ var InternalClient = (function () { }; InternalClient.prototype.disconnected = function disconnected() { + var _this2 = this; + var forced = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0]; this.cleanIntervals(); - this.leaveVoiceChannel(); + this.voiceConnections.forEach(function (vc) { + _this2.leaveVoiceChannel(vc); + }); if (this.client.options.revive && !forced) { this.setup(); @@ -240,13 +244,13 @@ var InternalClient = (function () { //def leaveVoiceChannel InternalClient.prototype.leaveVoiceChannel = function leaveVoiceChannel(chann) { - var _this2 = this; + var _this3 = this; if (this.user.bot) { var leave = function leave(connection) { return new Promise(function (resolve, reject) { connection.destroy(); - _this2.voiceConnections.remove(connection); + _this3.voiceConnections.remove(connection); resolve(); }); }; @@ -261,7 +265,7 @@ var InternalClient = (function () { return Promise.reject(new Error("channel is not a voice channel!")); } - var connection = _this2.voiceConnections.get("voiceChannel", channel); + var connection = _this3.voiceConnections.get("voiceChannel", channel); if (!connection) { return Promise.reject(new Error("not connected to that voice channel")); } @@ -276,7 +280,7 @@ var InternalClient = (function () { // preserve old functionality for non-bots if (this.voiceConnections[0]) { this.voiceConnections[0].destroy(); - this.voiceConnections = []; + this.voiceConnections.remove(this.voiceConnections[0]); } return Promise.resolve(); } @@ -285,11 +289,11 @@ var InternalClient = (function () { //def awaitResponse InternalClient.prototype.awaitResponse = function awaitResponse(msg) { - var _this3 = this; + var _this4 = this; return new Promise(function (resolve, reject) { - msg = _this3.resolver.resolveMessage(msg); + msg = _this4.resolver.resolveMessage(msg); if (!msg) { reject(new Error("message undefined")); @@ -298,18 +302,18 @@ var InternalClient = (function () { var awaitID = msg.channel.id + msg.author.id; - if (!_this3.messageAwaits[awaitID]) { - _this3.messageAwaits[awaitID] = []; + if (!_this4.messageAwaits[awaitID]) { + _this4.messageAwaits[awaitID] = []; } - _this3.messageAwaits[awaitID].push(resolve); + _this4.messageAwaits[awaitID].push(resolve); }); }; //def joinVoiceChannel InternalClient.prototype.joinVoiceChannel = function joinVoiceChannel(chann) { - var _this4 = this; + var _this5 = this; return this.resolver.resolveChannel(chann).then(function (channel) { if (!channel) { @@ -321,7 +325,7 @@ var InternalClient = (function () { } var joinSendWS = function joinSendWS() { - _this4.sendWS({ + _this5.sendWS({ op: 4, d: { "guild_id": channel.server.id, @@ -332,52 +336,59 @@ var InternalClient = (function () { }); }; - var joinVoice = new Promise(function (resolve, reject) { - var session, - token, - server = channel.server, - endpoint; + var joinVoice = function joinVoice() { + return new Promise(function (resolve, reject) { + var session, + token, + server = channel.server, + endpoint; - var check = function check(m) { - var data = JSON.parse(m); - if (data.d.guild_id !== server.id) return; // ensure it is the right server + var check = function check(m) { + var data = JSON.parse(m); + if (data.d.guild_id !== server.id) return; // ensure it is the right server - if (data.t === "VOICE_STATE_UPDATE") { - session = data.d.session_id; - } 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); + if (data.t === "VOICE_STATE_UPDATE") { + session = data.d.session_id; + } else if (data.t === "VOICE_SERVER_UPDATE") { + token = data.d.token; + endpoint = data.d.endpoint; + var chan = new _VoiceVoiceConnection2["default"](channel, _this5.client, session, token, server, endpoint); + _this5.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); - } - }; + _this5.client.emit("debug", "removed temporary voice websocket listeners"); + _this5.websocket.removeListener("message", check); + } + }; - _this4.websocket.on("message", check); - joinSendWS(); - }); + _this5.websocket.on("message", check); + 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 = _this5.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 Promise.resolve(existingServerConn); + } - 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 + if (!_this5.user.bot && _this5.voiceConnections.length > 0) { + // nonbot, one voiceconn only, just like last time just disconnect + return _this5.leaveVoiceChannel().then(joinVoice); + } - return joinVoice; + return joinVoice(); }); }; // def forceFetchUsers InternalClient.prototype.forceFetchUsers = function forceFetchUsers() { - var _this5 = this; + var _this6 = this; this.sendWS({ op: 8, @@ -414,7 +425,7 @@ var InternalClient = (function () { if (!server) { resolve(); } else { - _this5.chunkloaderCallback = resolve; + _this6.chunkloaderCallback = resolve; } }); }; @@ -422,7 +433,7 @@ var InternalClient = (function () { // def createServer InternalClient.prototype.createServer = function createServer(name) { - var _this6 = this; + var _this7 = this; var region = arguments.length <= 1 || arguments[1] === undefined ? "london" : arguments[1]; @@ -431,7 +442,7 @@ var InternalClient = (function () { return this.apiRequest('post', _Constants.Endpoints.SERVERS, true, { name: name, region: region }).then(function (res) { // valid server, wait until it is cached return waitFor(function () { - return _this6.servers.get("id", res.id); + return _this7.servers.get("id", res.id); }); }); }; @@ -439,7 +450,7 @@ var InternalClient = (function () { //def joinServer InternalClient.prototype.joinServer = function joinServer(invite) { - var _this7 = this; + var _this8 = this; invite = this.resolver.resolveInviteID(invite); if (!invite) { @@ -449,7 +460,7 @@ var InternalClient = (function () { return this.apiRequest("post", _Constants.Endpoints.INVITE(invite), true).then(function (res) { // valid server, wait until it is received via ws and cached return waitFor(function () { - return _this7.servers.get("id", res.guild.id); + return _this8.servers.get("id", res.guild.id); }); }); }; @@ -457,7 +468,7 @@ var InternalClient = (function () { //def updateServer InternalClient.prototype.updateServer = function updateServer(server, name, region) { - var _this8 = this; + var _this9 = this; var server = this.resolver.resolveServer(server); if (!server) { @@ -467,7 +478,7 @@ var InternalClient = (function () { return this.apiRequest("patch", _Constants.Endpoints.SERVER(server.id), true, { name: name || server.name, region: region || server.region }).then(function (res) { // wait until the name and region are updated return waitFor(function () { - return _this8.servers.get("name", res.name) ? _this8.servers.get("name", res.name).region === res.region ? _this8.servers.get("id", res.id) : false : false; + return _this9.servers.get("name", res.name) ? _this9.servers.get("name", res.name).region === res.region ? _this9.servers.get("id", res.id) : false : false; }); }); }; @@ -475,7 +486,7 @@ var InternalClient = (function () { //def leaveServer InternalClient.prototype.leaveServer = function leaveServer(srv) { - var _this9 = this; + var _this10 = this; var server = this.resolver.resolveServer(srv); if (!server) { @@ -498,17 +509,17 @@ var InternalClient = (function () { var chan = _ref3; - _this9.channels.remove(chan); + _this10.channels.remove(chan); } // remove server - _this9.servers.remove(server); + _this10.servers.remove(server); }); }; //def deleteServer InternalClient.prototype.deleteServer = function deleteServer(srv) { - var _this10 = this; + var _this11 = this; var server = this.resolver.resolveServer(srv); if (!server) { @@ -531,10 +542,10 @@ var InternalClient = (function () { var chan = _ref4; - _this10.channels.remove(chan); + _this11.channels.remove(chan); } // remove server - _this10.servers.remove(server); + _this11.servers.remove(server); }); }; @@ -542,7 +553,7 @@ var InternalClient = (function () { // email and password are optional InternalClient.prototype.loginWithToken = function loginWithToken(token, email, password) { - var _this11 = this; + var _this12 = this; this.state = _ConnectionState2["default"].LOGGED_IN; this.token = token; @@ -550,7 +561,7 @@ var InternalClient = (function () { this.password = password; return this.getGateway().then(function (url) { - _this11.createWS(url); + _this12.createWS(url); return token; }); }; @@ -558,14 +569,14 @@ var InternalClient = (function () { // def login InternalClient.prototype.login = function login(email, password) { - var _this12 = this; + var _this13 = this; var client = this.client; if (!this.tokenCacher.done) { return new Promise(function (resolve, reject) { setTimeout(function () { - _this12.login(email, password).then(resolve)["catch"](reject); + _this13.login(email, password).then(resolve)["catch"](reject); }, 20); }); } else { @@ -586,15 +597,15 @@ var InternalClient = (function () { email: email, password: password }).then(function (res) { - _this12.client.emit("debug", "direct API login, cached token was unavailable"); + _this13.client.emit("debug", "direct API login, cached token was unavailable"); var token = res.token; - _this12.tokenCacher.setToken(email, password, token); - return _this12.loginWithToken(token, email, password); + _this13.tokenCacher.setToken(email, password, token); + return _this13.loginWithToken(token, email, password); }, function (error) { - _this12.websocket = null; + _this13.websocket = null; throw error; })["catch"](function (error) { - _this12.state = _ConnectionState2["default"].DISCONNECTED; + _this13.state = _ConnectionState2["default"].DISCONNECTED; client.emit("disconnected"); throw error; }); @@ -603,28 +614,28 @@ var InternalClient = (function () { // def logout InternalClient.prototype.logout = function logout() { - var _this13 = this; + var _this14 = this; if (this.state === _ConnectionState2["default"].DISCONNECTED || this.state === _ConnectionState2["default"].IDLE) { return Promise.reject(new Error("Client is not logged in!")); } return this.apiRequest("post", _Constants.Endpoints.LOGOUT, true).then(function () { - if (_this13.websocket) { - _this13.websocket.close(); - _this13.websocket = null; + if (_this14.websocket) { + _this14.websocket.close(); + _this14.websocket = null; } - _this13.token = null; - _this13.email = null; - _this13.password = null; - _this13.state = _ConnectionState2["default"].DISCONNECTED; + _this14.token = null; + _this14.email = null; + _this14.password = null; + _this14.state = _ConnectionState2["default"].DISCONNECTED; }); }; // def startPM InternalClient.prototype.startPM = function startPM(resUser) { - var _this14 = this; + var _this15 = this; var user = this.resolver.resolveUser(resUser); if (!user) { @@ -634,7 +645,7 @@ var InternalClient = (function () { return this.apiRequest("post", _Constants.Endpoints.USER_CHANNELS(this.user.id), true, { recipient_id: user.id }).then(function (res) { - return _this14.private_channels.add(new _StructuresPMChannel2["default"](res, _this14.client)); + return _this15.private_channels.add(new _StructuresPMChannel2["default"](res, _this15.client)); }); }; @@ -649,19 +660,19 @@ var InternalClient = (function () { // def sendMessage InternalClient.prototype.sendMessage = function sendMessage(where, _content) { - var _this15 = this; + var _this16 = this; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; return this.resolver.resolveChannel(where).then(function (destination) { //var destination; - var content = _this15.resolver.resolveString(_content); + var content = _this16.resolver.resolveString(_content); - return _this15.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, { + return _this16.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, { content: content, tts: options.tts }).then(function (res) { - return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this15.client)); + return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this16.client)); }); }); }; @@ -669,7 +680,7 @@ var InternalClient = (function () { // def deleteMessage InternalClient.prototype.deleteMessage = function deleteMessage(_message) { - var _this16 = this; + var _this17 = this; var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; @@ -680,7 +691,7 @@ var InternalClient = (function () { var chain = options.wait ? delay(options.wait) : Promise.resolve(); return chain.then(function () { - return _this16.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true); + return _this17.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true); }).then(function () { return message.channel.messages.remove(message); }); @@ -689,7 +700,7 @@ var InternalClient = (function () { // def updateMessage InternalClient.prototype.updateMessage = function updateMessage(msg, _content) { - var _this17 = this; + var _this18 = this; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; @@ -705,14 +716,14 @@ var InternalClient = (function () { content: content, tts: options.tts }).then(function (res) { - return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this17.client)); + return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this18.client)); }); }; // def sendFile InternalClient.prototype.sendFile = function sendFile(where, _file, name) { - var _this18 = this; + var _this19 = this; if (!name) { if (_file instanceof String || typeof _file === "string") { @@ -726,12 +737,12 @@ var InternalClient = (function () { } return this.resolver.resolveChannel(where).then(function (channel) { - return _this18.resolver.resolveFile(_file).then(function (file) { - return _this18.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, { + return _this19.resolver.resolveFile(_file).then(function (file) { + return _this19.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, { name: name, file: file }).then(function (res) { - return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this18.client)); + return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this19.client)); }); }); }); @@ -740,7 +751,7 @@ var InternalClient = (function () { // def getChannelLogs InternalClient.prototype.getChannelLogs = function getChannelLogs(_channel) { - var _this19 = this; + var _this20 = this; var limit = arguments.length <= 1 || arguments[1] === undefined ? 50 : arguments[1]; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; @@ -748,21 +759,21 @@ var InternalClient = (function () { return this.resolver.resolveChannel(_channel).then(function (channel) { var qsObject = { limit: limit }; if (options.before) { - var res = _this19.resolver.resolveMessage(options.before); + var res = _this20.resolver.resolveMessage(options.before); if (res) { qsObject.before = res.id; } } if (options.after) { - var res = _this19.resolver.resolveMessage(options.after); + var res = _this20.resolver.resolveMessage(options.after); if (res) { qsObject.after = res.id; } } - return _this19.apiRequest("get", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id) + "?" + _querystring2["default"].stringify(qsObject), true).then(function (res) { + return _this20.apiRequest("get", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id) + "?" + _querystring2["default"].stringify(qsObject), true).then(function (res) { return res.map(function (msg) { - return channel.messages.add(new _StructuresMessage2["default"](msg, channel, _this19.client)); + return channel.messages.add(new _StructuresMessage2["default"](msg, channel, _this20.client)); }); }); }); @@ -771,13 +782,13 @@ var InternalClient = (function () { // def getBans InternalClient.prototype.getBans = function getBans(server) { - var _this20 = this; + var _this21 = this; server = this.resolver.resolveServer(server); return this.apiRequest("get", _Constants.Endpoints.SERVER_BANS(server.id), true).then(function (res) { return res.map(function (ban) { - return _this20.users.add(new _StructuresUser2["default"](ban.user, _this20.client)); + return _this21.users.add(new _StructuresUser2["default"](ban.user, _this21.client)); }); }); }; @@ -785,7 +796,7 @@ var InternalClient = (function () { // def createChannel InternalClient.prototype.createChannel = function createChannel(server, name) { - var _this21 = this; + var _this22 = this; var type = arguments.length <= 2 || arguments[2] === undefined ? "text" : arguments[2]; @@ -797,23 +808,23 @@ var InternalClient = (function () { }).then(function (res) { var channel; if (res.type === "text") { - channel = new _StructuresTextChannel2["default"](res, _this21.client, server); + channel = new _StructuresTextChannel2["default"](res, _this22.client, server); } else { - channel = new _StructuresVoiceChannel2["default"](res, _this21.client, server); + channel = new _StructuresVoiceChannel2["default"](res, _this22.client, server); } - return server.channels.add(_this21.channels.add(channel)); + return server.channels.add(_this22.channels.add(channel)); }); }; // def deleteChannel InternalClient.prototype.deleteChannel = function deleteChannel(_channel) { - var _this22 = this; + var _this23 = this; return this.resolver.resolveChannel(_channel).then(function (channel) { - return _this22.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () { + return _this23.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () { channel.server.channels.remove(channel); - _this22.channels.remove(channel); + _this23.channels.remove(channel); }); }); }; @@ -851,7 +862,7 @@ var InternalClient = (function () { // def moveMember InternalClient.prototype.moveMember = function moveMember(user, channel) { - var _this23 = this; + var _this24 = this; user = this.resolver.resolveUser(user); return this.resolver.resolveChannel(channel).then(function (channel) { @@ -861,7 +872,7 @@ var InternalClient = (function () { if (channel.type !== "voice") { throw new Error("Can't moveMember into a non-voice channel"); } else { - return _this23.apiRequest("patch", _Constants.Endpoints.SERVER_MEMBERS(server.id) + "/" + user.id, true, { channel_id: channel.id }).then(function (res) { + return _this24.apiRequest("patch", _Constants.Endpoints.SERVER_MEMBERS(server.id) + "/" + user.id, true, { channel_id: channel.id }).then(function (res) { user.voiceChannel = channel; return res; }); @@ -904,15 +915,15 @@ var InternalClient = (function () { // def createRole InternalClient.prototype.createRole = function createRole(server, data) { - var _this24 = this; + var _this25 = this; server = this.resolver.resolveServer(server); return this.apiRequest("post", _Constants.Endpoints.SERVER_ROLES(server.id), true).then(function (res) { - var role = server.roles.add(new _StructuresRole2["default"](res, server, _this24.client)); + var role = server.roles.add(new _StructuresRole2["default"](res, server, _this25.client)); if (data) { - return _this24.updateRole(role, data); + return _this25.updateRole(role, data); } return role; }); @@ -921,7 +932,7 @@ var InternalClient = (function () { // def updateRole InternalClient.prototype.updateRole = function updateRole(role, data) { - var _this25 = this; + var _this26 = this; role = this.resolver.resolveRole(role); var server = this.resolver.resolveServer(role.server); @@ -958,7 +969,7 @@ var InternalClient = (function () { } return this.apiRequest("patch", _Constants.Endpoints.SERVER_ROLES(server.id) + "/" + role.id, true, newData).then(function (res) { - return server.roles.update(role, new _StructuresRole2["default"](res, server, _this25.client)); + return server.roles.update(role, new _StructuresRole2["default"](res, server, _this26.client)); }); }; @@ -975,7 +986,7 @@ var InternalClient = (function () { //def addMemberToRole InternalClient.prototype.addMemberToRole = function addMemberToRole(member, roles) { - var _this26 = this; + var _this27 = this; member = this.resolver.resolveUser(member); @@ -992,7 +1003,7 @@ var InternalClient = (function () { } } else { roles = roles.map(function (r) { - return _this26.resolver.resolveRole(r); + return _this27.resolver.resolveRole(r); }); } @@ -1055,7 +1066,7 @@ var InternalClient = (function () { //def removeMemberFromRole InternalClient.prototype.removeMemberFromRole = function removeMemberFromRole(member, roles) { - var _this27 = this; + var _this28 = this; member = this.resolver.resolveUser(member); @@ -1072,7 +1083,7 @@ var InternalClient = (function () { } } else { roles = roles.map(function (r) { - return _this27.resolver.resolveRole(r); + return _this28.resolver.resolveRole(r); }); } @@ -1113,7 +1124,7 @@ var InternalClient = (function () { // def createInvite InternalClient.prototype.createInvite = function createInvite(chanServ, options) { - var _this28 = this; + var _this29 = this; return this.resolver.resolveChannel(chanServ).then(function (channel) { if (!options) { @@ -1127,8 +1138,8 @@ var InternalClient = (function () { options.xkcdpass = options.xkcd || false; } - return _this28.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true, options).then(function (res) { - return new _StructuresInvite2["default"](res, _this28.channels.get("id", res.channel.id), _this28.client); + return _this29.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true, options).then(function (res) { + return new _StructuresInvite2["default"](res, _this29.channels.get("id", res.channel.id), _this29.client); }); }); }; @@ -1146,7 +1157,7 @@ var InternalClient = (function () { //def getInvite InternalClient.prototype.getInvite = function getInvite(invite) { - var _this29 = this; + var _this30 = this; invite = this.resolver.resolveInviteID(invite); if (!invite) { @@ -1154,11 +1165,11 @@ var InternalClient = (function () { } return this.apiRequest("get", _Constants.Endpoints.INVITE(invite), true).then(function (res) { - if (!_this29.channels.has("id", res.channel.id)) { - return new _StructuresInvite2["default"](res, null, _this29.client); + if (!_this30.channels.has("id", res.channel.id)) { + return new _StructuresInvite2["default"](res, null, _this30.client); } - return _this29.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) { - return new _StructuresInvite2["default"](res2, _this29.channels.get("id", res.channel.id), _this29.client); + return _this30.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) { + return new _StructuresInvite2["default"](res2, _this30.channels.get("id", res.channel.id), _this30.client); }); }); }; @@ -1166,22 +1177,22 @@ var InternalClient = (function () { //def getInvites InternalClient.prototype.getInvites = function getInvites(channel) { - var _this30 = this; + var _this31 = this; if (!(channel instanceof _StructuresChannel2["default"])) { var server = this.resolver.resolveServer(channel); if (server) { return this.apiRequest("get", _Constants.Endpoints.SERVER_INVITES(server.id), true).then(function (res) { return res.map(function (data) { - return new _StructuresInvite2["default"](data, _this30.channels.get("id", data.channel.id), _this30.client); + return new _StructuresInvite2["default"](data, _this31.channels.get("id", data.channel.id), _this31.client); }); }); } } return this.resolver.resolveChannel(channel).then(function (channel) { - return _this30.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) { + return _this31.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) { return res.map(function (data) { - return new _StructuresInvite2["default"](data, _this30.channels.get("id", data.channel.id), _this30.client); + return new _StructuresInvite2["default"](data, _this31.channels.get("id", data.channel.id), _this31.client); }); }); }); @@ -1190,7 +1201,7 @@ var InternalClient = (function () { //def overwritePermissions InternalClient.prototype.overwritePermissions = function overwritePermissions(channel, role, updated) { - var _this31 = this; + var _this32 = this; return this.resolver.resolveChannel(channel).then(function (channel) { if (!channel instanceof _StructuresServerChannel2["default"]) { @@ -1203,7 +1214,7 @@ var InternalClient = (function () { }; if (role instanceof String || typeof role === "string") { - role = _this31.resolver.resolveUser(role) || _this31.resolver.resolveRole(role); + role = _this32.resolver.resolveUser(role) || _this32.resolver.resolveRole(role); } if (role instanceof _StructuresUser2["default"]) { @@ -1236,7 +1247,7 @@ var InternalClient = (function () { } } - return _this31.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data); + return _this32.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data); }); }; @@ -1275,49 +1286,49 @@ var InternalClient = (function () { //def sendTyping InternalClient.prototype.sendTyping = function sendTyping(channel) { - var _this32 = this; + var _this33 = this; return this.resolver.resolveChannel(channel).then(function (channel) { - return _this32.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true); + return _this33.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true); }); }; //def startTyping InternalClient.prototype.startTyping = function startTyping(channel) { - var _this33 = this; + var _this34 = this; return this.resolver.resolveChannel(channel).then(function (channel) { - if (_this33.intervals.typing[channel.id]) { + if (_this34.intervals.typing[channel.id]) { // typing interval already exists, leave it alone throw new Error("Already typing in that channel"); } - _this33.intervals.typing[channel.id] = setInterval(function () { - return _this33.sendTyping(channel)["catch"](function (error) { - return _this33.emit("error", error); + _this34.intervals.typing[channel.id] = setInterval(function () { + return _this34.sendTyping(channel)["catch"](function (error) { + return _this34.emit("error", error); }); }, 4000); - return _this33.sendTyping(channel); + return _this34.sendTyping(channel); }); }; //def stopTyping InternalClient.prototype.stopTyping = function stopTyping(channel) { - var _this34 = this; + var _this35 = this; return this.resolver.resolveChannel(channel).then(function (channel) { - if (!_this34.intervals.typing[channel.id]) { + if (!_this35.intervals.typing[channel.id]) { // typing interval doesn"t exist throw new Error("Not typing in that channel"); } - clearInterval(_this34.intervals.typing[channel.id]); - _this34.intervals.typing[channel.id] = false; + clearInterval(_this35.intervals.typing[channel.id]); + _this35.intervals.typing[channel.id] = false; }); }; @@ -1355,12 +1366,12 @@ var InternalClient = (function () { //def setChannelTopic InternalClient.prototype.setChannelTopic = function setChannelTopic(chann) { - var _this35 = this; + var _this36 = this; var topic = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1]; return this.resolver.resolveChannel(chann).then(function (channel) { - return _this35.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { + return _this36.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { name: channel.name, position: channel.position, topic: topic @@ -1373,12 +1384,12 @@ var InternalClient = (function () { //def setChannelName InternalClient.prototype.setChannelName = function setChannelName(chann) { - var _this36 = this; + var _this37 = this; var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1]; return this.resolver.resolveChannel(chann).then(function (channel) { - return _this36.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { + return _this37.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { name: name, position: channel.position, topic: channel.topic @@ -1391,13 +1402,13 @@ var InternalClient = (function () { //def setChannelNameAndTopic InternalClient.prototype.setChannelNameAndTopic = function setChannelNameAndTopic(chann) { - var _this37 = this; + var _this38 = this; var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1]; var topic = arguments.length <= 2 || arguments[2] === undefined ? "" : arguments[2]; return this.resolver.resolveChannel(chann).then(function (channel) { - return _this37.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { + return _this38.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { name: name, position: channel.position, topic: topic @@ -1411,12 +1422,12 @@ var InternalClient = (function () { //def setTopic InternalClient.prototype.setChannelPosition = function setChannelPosition(chann) { - var _this38 = this; + var _this39 = this; var position = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; return this.resolver.resolveChannel(chann).then(function (channel) { - return _this38.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { + return _this39.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { name: channel.name, position: position, topic: channel.topic @@ -1478,7 +1489,7 @@ var InternalClient = (function () { }; InternalClient.prototype.createWS = function createWS(url) { - var _this39 = this; + var _this40 = this; var self = this; var client = self.client; @@ -1986,7 +1997,7 @@ var InternalClient = (function () { data.id = data.id || user.id; data.avatar = data.avatar || user.avatar; data.discriminator = data.discriminator || user.discriminator; - _this39.email = data.email || _this39.email; + _this40.email = data.email || _this40.email; var presenceUser = new _StructuresUser2["default"](data, client); diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index f1c576847..3933368da 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -143,7 +143,9 @@ export default class InternalClient { this.cleanIntervals(); - this.leaveVoiceChannel(); + this.voiceConnections.forEach(vc => { + this.leaveVoiceChannel(vc); + }); if (this.client.options.revive && !forced) { this.setup(); @@ -208,7 +210,7 @@ export default class InternalClient { // preserve old functionality for non-bots if (this.voiceConnections[0]) { this.voiceConnections[0].destroy(); - this.voiceConnections = []; + this.voiceConnections.remove(this.voiceConnections[0]) } return Promise.resolve(); } @@ -258,43 +260,48 @@ export default class InternalClient { }); } - var joinVoice = new Promise((resolve, reject) => { - var session, token, server = channel.server, endpoint; + var joinVoice = () => { + return new Promise((resolve, reject) => { + var session, token, server = channel.server, endpoint; - var check = m => { - var data = JSON.parse(m); - if (data.d.guild_id !== server.id) return // ensure it is the right server + var check = m => { + var data = JSON.parse(m); + if (data.d.guild_id !== server.id) return // ensure it is the right server - if (data.t === "VOICE_STATE_UPDATE") { - session = data.d.session_id; - } else if (data.t === "VOICE_SERVER_UPDATE") { - token = data.d.token; - endpoint = data.d.endpoint; - var chan = new VoiceConnection( - channel, this.client, session, token, server, endpoint - ); - this.voiceConnections.add(chan); + if (data.t === "VOICE_STATE_UPDATE") { + session = data.d.session_id; + } else if (data.t === "VOICE_SERVER_UPDATE") { + token = data.d.token; + endpoint = data.d.endpoint; + var chan = new VoiceConnection( + channel, this.client, session, token, server, endpoint + ); + this.voiceConnections.add(chan); - chan.on("ready", () => resolve(chan)); - chan.on("error", reject); + chan.on("ready", () => resolve(chan)); + chan.on("error", reject); - this.client.emit("debug", "removed temporary voice websocket listeners"); - this.websocket.removeListener("message", check); - } - }; + this.client.emit("debug", "removed temporary voice websocket listeners"); + this.websocket.removeListener("message", check); + } + }; - this.websocket.on("message", check); - 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); + this.websocket.on("message", check); + joinSendWS(); + }); + } var existingServerConn = this.voiceConnections.get("server", channel.server); // same server connection - if (existingServerConn) + if (existingServerConn) { joinSendWS(); // Just needs to update by sending via WS, movement in cache will be handled by global handler + return Promise.resolve(existingServerConn); + } - return joinVoice; + if (!this.user.bot && this.voiceConnections.length > 0) { // nonbot, one voiceconn only, just like last time just disconnect + return this.leaveVoiceChannel().then(joinVoice); + } + + return joinVoice(); }); }