From 68a21ee6791df83b608b859ab7abbb1854f1c0c9 Mon Sep 17 00:00:00 2001 From: abalabahaha Date: Fri, 22 Apr 2016 18:14:47 -0700 Subject: [PATCH] Potentially fix autoReconnect without internet --- lib/Client/InternalClient.js | 181 ++++++++++++++++++----------------- src/Client/InternalClient.js | 9 +- 2 files changed, 101 insertions(+), 89 deletions(-) diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index c6a5c2234..64a1be699 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -259,9 +259,13 @@ var InternalClient = (function () { // Check whether the email is set (if not, only a token has been used for login) if (_this2.email) { - _this2.login(_this2.email, _this2.password); + _this2.login(_this2.email, _this2.password)["catch"](function () { + return _this2.disconnected(true); + }); } else { - _this2.loginWithToken(_this2.token); + _this2.loginWithToken(_this2.token)["catch"](function () { + return _this2.disconnected(true); + }); } }, this.autoReconnectInterval); } @@ -724,15 +728,20 @@ var InternalClient = (function () { // def getGateway InternalClient.prototype.getGateway = function getGateway() { + var _this16 = this; + + if (this.gatewayURL) { + return Promise.resolve(this.gatewayURL); + } return this.apiRequest("get", _Constants.Endpoints.GATEWAY, true).then(function (res) { - return res.url; + return _this16.gatewayURL = res.url; }); }; // def sendMessage InternalClient.prototype.sendMessage = function sendMessage(where, _content) { - var _this16 = this; + var _this17 = this; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; @@ -755,26 +764,26 @@ var InternalClient = (function () { } return this.resolver.resolveChannel(where).then(function (destination) { - var content = _this16.resolver.resolveString(_content); + var content = _this17.resolver.resolveString(_content); if (options.file) { - return _this16.resolver.resolveFile(options.file.file).then(function (file) { - return _this16.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, { + return _this17.resolver.resolveFile(options.file.file).then(function (file) { + return _this17.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, { content: content, tts: options.tts }, { name: options.file.name, file: file }).then(function (res) { - return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this16.client)); + return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this17.client)); }); }); } else { - return _this16.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, { + return _this17.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, _this16.client)); + return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this17.client)); }); } }); @@ -783,7 +792,7 @@ var InternalClient = (function () { // def sendFile InternalClient.prototype.sendFile = function sendFile(where, _file, name, _content) { - var _this17 = this; + var _this18 = this; if (!name) { if (_file instanceof String || typeof _file === "string") { @@ -805,12 +814,12 @@ var InternalClient = (function () { } return this.resolver.resolveChannel(where).then(function (channel) { - return _this17.resolver.resolveFile(_file).then(function (file) { - return _this17.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, content, { + return _this18.resolver.resolveFile(_file).then(function (file) { + return _this18.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, content, { name: name, file: file }).then(function (res) { - return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this17.client)); + return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this18.client)); }); }); }); @@ -819,7 +828,7 @@ var InternalClient = (function () { // def deleteMessage InternalClient.prototype.deleteMessage = function deleteMessage(_message) { - var _this18 = this; + var _this19 = this; var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; @@ -830,7 +839,7 @@ var InternalClient = (function () { var chain = options.wait ? delay(options.wait) : Promise.resolve(); return chain.then(function () { - return _this18.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true); + return _this19.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true); }).then(function () { return message.channel.messages.remove(message); }); @@ -839,7 +848,7 @@ var InternalClient = (function () { // def updateMessage InternalClient.prototype.updateMessage = function updateMessage(msg, _content) { - var _this19 = this; + var _this20 = this; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; @@ -855,14 +864,14 @@ var InternalClient = (function () { content: content, tts: options.tts }).then(function (res) { - return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this19.client)); + return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this20.client)); }); }; // def getChannelLogs InternalClient.prototype.getChannelLogs = function getChannelLogs(_channel) { - var _this20 = this; + var _this21 = this; var limit = arguments.length <= 1 || arguments[1] === undefined ? 50 : arguments[1]; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; @@ -870,21 +879,21 @@ var InternalClient = (function () { return this.resolver.resolveChannel(_channel).then(function (channel) { var qsObject = { limit: limit }; if (options.before) { - var res = _this20.resolver.resolveMessage(options.before); + var res = _this21.resolver.resolveMessage(options.before); if (res) { qsObject.before = res.id; } } if (options.after) { - var res = _this20.resolver.resolveMessage(options.after); + var res = _this21.resolver.resolveMessage(options.after); if (res) { qsObject.after = res.id; } } - return _this20.apiRequest("get", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id) + "?" + _querystring2["default"].stringify(qsObject), true).then(function (res) { + return _this21.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, _this20.client)); + return channel.messages.add(new _StructuresMessage2["default"](msg, channel, _this21.client)); }); }); }); @@ -893,13 +902,13 @@ var InternalClient = (function () { // def getBans InternalClient.prototype.getBans = function getBans(server) { - var _this21 = this; + var _this22 = 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 _this21.users.add(new _StructuresUser2["default"](ban.user, _this21.client)); + return _this22.users.add(new _StructuresUser2["default"](ban.user, _this22.client)); }); }); }; @@ -907,7 +916,7 @@ var InternalClient = (function () { // def createChannel InternalClient.prototype.createChannel = function createChannel(server, name) { - var _this22 = this; + var _this23 = this; var type = arguments.length <= 2 || arguments[2] === undefined ? "text" : arguments[2]; @@ -919,23 +928,23 @@ var InternalClient = (function () { }).then(function (res) { var channel; if (res.type === "text") { - channel = new _StructuresTextChannel2["default"](res, _this22.client, server); + channel = new _StructuresTextChannel2["default"](res, _this23.client, server); } else { - channel = new _StructuresVoiceChannel2["default"](res, _this22.client, server); + channel = new _StructuresVoiceChannel2["default"](res, _this23.client, server); } - return server.channels.add(_this22.channels.add(channel)); + return server.channels.add(_this23.channels.add(channel)); }); }; // def deleteChannel InternalClient.prototype.deleteChannel = function deleteChannel(_channel) { - var _this23 = this; + var _this24 = this; return this.resolver.resolveChannel(_channel).then(function (channel) { - return _this23.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () { + return _this24.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () { channel.server.channels.remove(channel); - _this23.channels.remove(channel); + _this24.channels.remove(channel); }); }); }; @@ -973,7 +982,7 @@ var InternalClient = (function () { // def moveMember InternalClient.prototype.moveMember = function moveMember(user, channel) { - var _this24 = this; + var _this25 = this; user = this.resolver.resolveUser(user); return this.resolver.resolveChannel(channel).then(function (channel) { @@ -983,7 +992,7 @@ var InternalClient = (function () { if (channel.type !== "voice") { throw new Error("Can't moveMember into a non-voice channel"); } else { - return _this24.apiRequest("patch", _Constants.Endpoints.SERVER_MEMBERS(server.id) + "/" + user.id, true, { channel_id: channel.id }).then(function (res) { + return _this25.apiRequest("patch", _Constants.Endpoints.SERVER_MEMBERS(server.id) + "/" + user.id, true, { channel_id: channel.id }).then(function (res) { user.voiceChannel = channel; return res; }); @@ -1026,15 +1035,15 @@ var InternalClient = (function () { // def createRole InternalClient.prototype.createRole = function createRole(server, data) { - var _this25 = this; + var _this26 = 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, _this25.client)); + var role = server.roles.add(new _StructuresRole2["default"](res, server, _this26.client)); if (data) { - return _this25.updateRole(role, data); + return _this26.updateRole(role, data); } return role; }); @@ -1043,7 +1052,7 @@ var InternalClient = (function () { // def updateRole InternalClient.prototype.updateRole = function updateRole(role, data) { - var _this26 = this; + var _this27 = this; role = this.resolver.resolveRole(role); var server = this.resolver.resolveServer(role.server); @@ -1080,7 +1089,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, _this26.client)); + return server.roles.update(role, new _StructuresRole2["default"](res, server, _this27.client)); }); }; @@ -1097,7 +1106,7 @@ var InternalClient = (function () { //def addMemberToRole InternalClient.prototype.addMemberToRole = function addMemberToRole(member, roles) { - var _this27 = this; + var _this28 = this; member = this.resolver.resolveUser(member); @@ -1114,7 +1123,7 @@ var InternalClient = (function () { } } else { roles = roles.map(function (r) { - return _this27.resolver.resolveRole(r); + return _this28.resolver.resolveRole(r); }); } @@ -1177,7 +1186,7 @@ var InternalClient = (function () { //def removeMemberFromRole InternalClient.prototype.removeMemberFromRole = function removeMemberFromRole(member, roles) { - var _this28 = this; + var _this29 = this; member = this.resolver.resolveUser(member); @@ -1194,7 +1203,7 @@ var InternalClient = (function () { } } else { roles = roles.map(function (r) { - return _this28.resolver.resolveRole(r); + return _this29.resolver.resolveRole(r); }); } @@ -1235,7 +1244,7 @@ var InternalClient = (function () { // def createInvite InternalClient.prototype.createInvite = function createInvite(chanServ, options) { - var _this29 = this; + var _this30 = this; return this.resolver.resolveChannel(chanServ).then(function (channel) { if (!options) { @@ -1249,8 +1258,8 @@ var InternalClient = (function () { options.xkcdpass = options.xkcd || false; } - 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); + return _this30.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true, options).then(function (res) { + return new _StructuresInvite2["default"](res, _this30.channels.get("id", res.channel.id), _this30.client); }); }); }; @@ -1268,7 +1277,7 @@ var InternalClient = (function () { //def getInvite InternalClient.prototype.getInvite = function getInvite(invite) { - var _this30 = this; + var _this31 = this; invite = this.resolver.resolveInviteID(invite); if (!invite) { @@ -1276,11 +1285,11 @@ var InternalClient = (function () { } return this.apiRequest("get", _Constants.Endpoints.INVITE(invite), true).then(function (res) { - if (!_this30.channels.has("id", res.channel.id)) { - return new _StructuresInvite2["default"](res, null, _this30.client); + if (!_this31.channels.has("id", res.channel.id)) { + return new _StructuresInvite2["default"](res, null, _this31.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); + return _this31.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) { + return new _StructuresInvite2["default"](res2, _this31.channels.get("id", res.channel.id), _this31.client); }); }); }; @@ -1288,22 +1297,22 @@ var InternalClient = (function () { //def getInvites InternalClient.prototype.getInvites = function getInvites(channel) { - var _this31 = this; + var _this32 = 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, _this31.channels.get("id", data.channel.id), _this31.client); + return new _StructuresInvite2["default"](data, _this32.channels.get("id", data.channel.id), _this32.client); }); }); } } return this.resolver.resolveChannel(channel).then(function (channel) { - return _this31.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) { + return _this32.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) { return res.map(function (data) { - return new _StructuresInvite2["default"](data, _this31.channels.get("id", data.channel.id), _this31.client); + return new _StructuresInvite2["default"](data, _this32.channels.get("id", data.channel.id), _this32.client); }); }); }); @@ -1312,7 +1321,7 @@ var InternalClient = (function () { //def overwritePermissions InternalClient.prototype.overwritePermissions = function overwritePermissions(channel, role, updated) { - var _this32 = this; + var _this33 = this; return this.resolver.resolveChannel(channel).then(function (channel) { if (!channel instanceof _StructuresServerChannel2["default"]) { @@ -1325,7 +1334,7 @@ var InternalClient = (function () { }; if (role instanceof String || typeof role === "string") { - role = _this32.resolver.resolveUser(role) || _this32.resolver.resolveRole(role); + role = _this33.resolver.resolveUser(role) || _this33.resolver.resolveRole(role); } if (role instanceof _StructuresUser2["default"]) { @@ -1358,7 +1367,7 @@ var InternalClient = (function () { } } - return _this32.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data); + return _this33.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data); }); }; @@ -1399,49 +1408,49 @@ var InternalClient = (function () { //def sendTyping InternalClient.prototype.sendTyping = function sendTyping(channel) { - var _this33 = this; + var _this34 = this; return this.resolver.resolveChannel(channel).then(function (channel) { - return _this33.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true); + return _this34.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true); }); }; //def startTyping InternalClient.prototype.startTyping = function startTyping(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 already exists, leave it alone throw new Error("Already typing in that channel"); } - _this34.intervals.typing[channel.id] = setInterval(function () { - return _this34.sendTyping(channel)["catch"](function (error) { - return _this34.emit("error", error); + _this35.intervals.typing[channel.id] = setInterval(function () { + return _this35.sendTyping(channel)["catch"](function (error) { + return _this35.emit("error", error); }); }, 4000); - return _this34.sendTyping(channel); + return _this35.sendTyping(channel); }); }; //def stopTyping InternalClient.prototype.stopTyping = function stopTyping(channel) { - var _this35 = this; + var _this36 = this; return this.resolver.resolveChannel(channel).then(function (channel) { - if (!_this35.intervals.typing[channel.id]) { + if (!_this36.intervals.typing[channel.id]) { // typing interval doesn"t exist throw new Error("Not typing in that channel"); } - clearInterval(_this35.intervals.typing[channel.id]); - _this35.intervals.typing[channel.id] = false; + clearInterval(_this36.intervals.typing[channel.id]); + _this36.intervals.typing[channel.id] = false; }); }; @@ -1481,12 +1490,12 @@ var InternalClient = (function () { //def setChannelTopic InternalClient.prototype.setChannelTopic = function setChannelTopic(chann) { - var _this36 = this; + var _this37 = this; var topic = arguments.length <= 1 || arguments[1] === undefined ? "" : 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: channel.name, position: channel.position, topic: topic @@ -1499,12 +1508,12 @@ var InternalClient = (function () { //def setChannelName InternalClient.prototype.setChannelName = function setChannelName(chann) { - var _this37 = this; + var _this38 = this; var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1]; 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: channel.topic @@ -1517,13 +1526,13 @@ var InternalClient = (function () { //def setChannelNameAndTopic InternalClient.prototype.setChannelNameAndTopic = function setChannelNameAndTopic(chann) { - var _this38 = this; + var _this39 = 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 _this38.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { + return _this39.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { name: name, position: channel.position, topic: topic @@ -1537,12 +1546,12 @@ var InternalClient = (function () { //def setTopic InternalClient.prototype.setChannelPosition = function setChannelPosition(chann) { - var _this39 = this; + var _this40 = this; var position = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; return this.resolver.resolveChannel(chann).then(function (channel) { - return _this39.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { + return _this40.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, { name: channel.name, position: position, topic: channel.topic @@ -1604,7 +1613,7 @@ var InternalClient = (function () { }; InternalClient.prototype.createWS = function createWS(url) { - var _this40 = this; + var _this41 = this; var self = this; var client = self.client; @@ -1638,14 +1647,14 @@ var InternalClient = (function () { this.websocket.onclose = function (code) { self.websocket = null; self.state = _ConnectionState2["default"].DISCONNECTED; - self.disconnected(_this40.client.options.autoReconnect); + self.disconnected(_this41.client.options.autoReconnect); }; this.websocket.onerror = function (e) { client.emit("error", e); self.websocket = null; self.state = _ConnectionState2["default"].DISCONNECTED; - self.disconnected(_this40.client.options.autoReconnect); + self.disconnected(_this41.client.options.autoReconnect); }; this.websocket.onmessage = function (e) { @@ -1674,10 +1683,10 @@ var InternalClient = (function () { self.user = self.users.add(new _StructuresUser2["default"](data.user, client)); - _this40.forceFetchCount = {}; - _this40.forceFetchQueue = []; - _this40.forceFetchLength = 1; - _this40.autoReconnectInterval = 1000; + _this41.forceFetchCount = {}; + _this41.forceFetchQueue = []; + _this41.forceFetchLength = 1; + _this41.autoReconnectInterval = 1000; data.guilds.forEach(function (server) { if (!server.unavailable) { @@ -2131,7 +2140,7 @@ var InternalClient = (function () { data.id = data.id || user.id; data.avatar = data.avatar || user.avatar; data.discriminator = data.discriminator || user.discriminator; - _this40.email = data.email || _this40.email; + _this41.email = data.email || _this41.email; var presenceUser = new _StructuresUser2["default"](data, client); diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index c292918ad..d8e222ab1 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -180,9 +180,9 @@ export default class InternalClient { // Check whether the email is set (if not, only a token has been used for login) if (this.email) { - this.login(this.email, this.password); + this.login(this.email, this.password).catch(() => this.disconnected(true)); } else { - this.loginWithToken(this.token); + this.loginWithToken(this.token).catch(() => this.disconnected(true)); } }, this.autoReconnectInterval); } @@ -594,8 +594,11 @@ export default class InternalClient { // def getGateway getGateway() { + if(this.gatewayURL) { + return Promise.resolve(this.gatewayURL); + } return this.apiRequest("get", Endpoints.GATEWAY, true) - .then(res => res.url); + .then(res => this.gatewayURL = res.url); } // def sendMessage