mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
Token Cache bug fix and Rate Limiting
This commit is contained in:
@@ -115,6 +115,9 @@ var InternalClient = (function () {
|
||||
}
|
||||
|
||||
InternalClient.prototype.apiRequest = function apiRequest(method, url, useAuth, data, file) {
|
||||
var _this = this,
|
||||
_arguments = arguments;
|
||||
|
||||
var ret = _superagent2["default"][method](url);
|
||||
if (useAuth) {
|
||||
ret.set("authorization", this.token);
|
||||
@@ -129,9 +132,20 @@ var InternalClient = (function () {
|
||||
return new Promise(function (resolve, reject) {
|
||||
ret.end(function (error, data) {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
if (error.response && error.response.error && error.response.error.status && error.response.error.status === 429) {
|
||||
if (data.headers["retry-after"] || data.headers["Retry-After"]) {
|
||||
var toWait = data.headers["retry-after"] || data.headers["Retry-After"];
|
||||
toWait = parseInt(toWait);
|
||||
setTimeout(function () {
|
||||
_this.apiRequest.apply(_this, _arguments).then(resolve)["catch"](reject);
|
||||
}, toWait);
|
||||
}
|
||||
} else {
|
||||
return reject(error);
|
||||
}
|
||||
} else {
|
||||
resolve(data.body);
|
||||
}
|
||||
resolve(data.body);
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -220,11 +234,11 @@ var InternalClient = (function () {
|
||||
//def awaitResponse
|
||||
|
||||
InternalClient.prototype.awaitResponse = function awaitResponse(msg) {
|
||||
var _this = this;
|
||||
var _this2 = this;
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
|
||||
msg = _this.resolver.resolveMessage(msg);
|
||||
msg = _this2.resolver.resolveMessage(msg);
|
||||
|
||||
if (!msg) {
|
||||
reject(new Error("message undefined"));
|
||||
@@ -233,18 +247,18 @@ var InternalClient = (function () {
|
||||
|
||||
var awaitID = msg.channel.id + msg.author.id;
|
||||
|
||||
if (!_this.messageAwaits[awaitID]) {
|
||||
_this.messageAwaits[awaitID] = [];
|
||||
if (!_this2.messageAwaits[awaitID]) {
|
||||
_this2.messageAwaits[awaitID] = [];
|
||||
}
|
||||
|
||||
_this.messageAwaits[awaitID].push(resolve);
|
||||
_this2.messageAwaits[awaitID].push(resolve);
|
||||
});
|
||||
};
|
||||
|
||||
//def joinVoiceChannel
|
||||
|
||||
InternalClient.prototype.joinVoiceChannel = function joinVoiceChannel(chann) {
|
||||
var _this2 = this;
|
||||
var _this3 = this;
|
||||
|
||||
var channel = this.resolver.resolveVoiceChannel(chann);
|
||||
|
||||
@@ -265,20 +279,20 @@ var InternalClient = (function () {
|
||||
} else if (data.t === "VOICE_SERVER_UPDATE") {
|
||||
token = data.d.token;
|
||||
endpoint = data.d.endpoint;
|
||||
var chan = _this2.voiceConnection = new _VoiceVoiceConnection2["default"](channel, _this2.client, session, token, server, endpoint);
|
||||
var chan = _this3.voiceConnection = new _VoiceVoiceConnection2["default"](channel, _this3.client, session, token, server, endpoint);
|
||||
|
||||
chan.on("ready", function () {
|
||||
return resolve(chan);
|
||||
});
|
||||
chan.on("error", reject);
|
||||
|
||||
_this2.client.emit("debug", "removed temporary voice websocket listeners");
|
||||
_this2.websocket.removeListener("message", check);
|
||||
_this3.client.emit("debug", "removed temporary voice websocket listeners");
|
||||
_this3.websocket.removeListener("message", check);
|
||||
}
|
||||
};
|
||||
|
||||
_this2.websocket.on("message", check);
|
||||
_this2.sendWS({
|
||||
_this3.websocket.on("message", check);
|
||||
_this3.sendWS({
|
||||
op: 4,
|
||||
d: {
|
||||
"guild_id": server.id,
|
||||
@@ -294,7 +308,7 @@ var InternalClient = (function () {
|
||||
// def createServer
|
||||
|
||||
InternalClient.prototype.createServer = function createServer(name) {
|
||||
var _this3 = this;
|
||||
var _this4 = this;
|
||||
|
||||
var region = arguments.length <= 1 || arguments[1] === undefined ? "london" : arguments[1];
|
||||
|
||||
@@ -303,7 +317,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 _this3.servers.get("id", res.id);
|
||||
return _this4.servers.get("id", res.id);
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -311,7 +325,7 @@ var InternalClient = (function () {
|
||||
//def joinServer
|
||||
|
||||
InternalClient.prototype.joinServer = function joinServer(invite) {
|
||||
var _this4 = this;
|
||||
var _this5 = this;
|
||||
|
||||
invite = this.resolver.resolveInviteID(invite);
|
||||
if (!invite) {
|
||||
@@ -320,7 +334,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 _this4.servers.get("id", res.guild.id);
|
||||
return _this5.servers.get("id", res.guild.id);
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -328,7 +342,7 @@ var InternalClient = (function () {
|
||||
//def leaveServer
|
||||
|
||||
InternalClient.prototype.leaveServer = function leaveServer(srv) {
|
||||
var _this5 = this;
|
||||
var _this6 = this;
|
||||
|
||||
var server = this.resolver.resolveServer(srv);
|
||||
if (!server) {
|
||||
@@ -351,38 +365,38 @@ var InternalClient = (function () {
|
||||
|
||||
var chan = _ref2;
|
||||
|
||||
_this5.channels.remove(chan);
|
||||
_this6.channels.remove(chan);
|
||||
}
|
||||
// remove server
|
||||
_this5.servers.remove(server);
|
||||
_this6.servers.remove(server);
|
||||
});
|
||||
};
|
||||
|
||||
// def login
|
||||
|
||||
InternalClient.prototype.login = function login(email, password) {
|
||||
var _this6 = this;
|
||||
var _this7 = this;
|
||||
|
||||
var client = this.client;
|
||||
|
||||
if (!this.tokenCacher.done) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
setTimeout(function () {
|
||||
_this6.login(email, password).then(resolve)["catch"](reject);
|
||||
_this7.login(email, password).then(resolve)["catch"](reject);
|
||||
}, 20);
|
||||
});
|
||||
} else {
|
||||
var tk = this.tokenCacher.getToken(email, password);
|
||||
if (tk) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
_this6.client.emit("debug", "bypassed direct API login, used cached token");
|
||||
_this6.state = _ConnectionState2["default"].LOGGED_IN;
|
||||
_this6.token = tk;
|
||||
_this6.email = email;
|
||||
_this6.password = password;
|
||||
_this7.client.emit("debug", "bypassed direct API login, used cached token");
|
||||
_this7.state = _ConnectionState2["default"].LOGGED_IN;
|
||||
_this7.token = tk;
|
||||
_this7.email = email;
|
||||
_this7.password = password;
|
||||
|
||||
return _this6.getGateway().then(function (url) {
|
||||
_this6.createWS(url);
|
||||
return _this7.getGateway().then(function (url) {
|
||||
_this7.createWS(url);
|
||||
return tk;
|
||||
});
|
||||
});
|
||||
@@ -399,23 +413,23 @@ var InternalClient = (function () {
|
||||
email: email,
|
||||
password: password
|
||||
}).then(function (res) {
|
||||
_this6.client.emit("debug", "direct API login, cached token was unavailable");
|
||||
_this7.client.emit("debug", "direct API login, cached token was unavailable");
|
||||
var token = res.token;
|
||||
_this6.tokenCacher.setToken(email, password, token);
|
||||
_this6.state = _ConnectionState2["default"].LOGGED_IN;
|
||||
_this6.token = token;
|
||||
_this6.email = email;
|
||||
_this6.password = password;
|
||||
_this7.tokenCacher.setToken(email, password, token);
|
||||
_this7.state = _ConnectionState2["default"].LOGGED_IN;
|
||||
_this7.token = token;
|
||||
_this7.email = email;
|
||||
_this7.password = password;
|
||||
|
||||
return _this6.getGateway().then(function (url) {
|
||||
_this6.createWS(url);
|
||||
return _this7.getGateway().then(function (url) {
|
||||
_this7.createWS(url);
|
||||
return token;
|
||||
});
|
||||
}, function (error) {
|
||||
_this6.websocket = null;
|
||||
_this7.websocket = null;
|
||||
throw error;
|
||||
})["catch"](function (error) {
|
||||
_this6.state = _ConnectionState2["default"].DISCONNECTED;
|
||||
_this7.state = _ConnectionState2["default"].DISCONNECTED;
|
||||
client.emit("disconnected");
|
||||
throw error;
|
||||
});
|
||||
@@ -424,28 +438,28 @@ var InternalClient = (function () {
|
||||
// def logout
|
||||
|
||||
InternalClient.prototype.logout = function logout() {
|
||||
var _this7 = this;
|
||||
var _this8 = 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 (_this7.websocket) {
|
||||
_this7.websocket.close();
|
||||
_this7.websocket = null;
|
||||
if (_this8.websocket) {
|
||||
_this8.websocket.close();
|
||||
_this8.websocket = null;
|
||||
}
|
||||
_this7.token = null;
|
||||
_this7.email = null;
|
||||
_this7.password = null;
|
||||
_this7.state = _ConnectionState2["default"].DISCONNECTED;
|
||||
_this8.token = null;
|
||||
_this8.email = null;
|
||||
_this8.password = null;
|
||||
_this8.state = _ConnectionState2["default"].DISCONNECTED;
|
||||
});
|
||||
};
|
||||
|
||||
// def startPM
|
||||
|
||||
InternalClient.prototype.startPM = function startPM(resUser) {
|
||||
var _this8 = this;
|
||||
var _this9 = this;
|
||||
|
||||
var user = this.resolver.resolveUser(resUser);
|
||||
if (!user) {
|
||||
@@ -455,7 +469,7 @@ var InternalClient = (function () {
|
||||
return this.apiRequest("post", _Constants.Endpoints.USER_CHANNELS(user.id), true, {
|
||||
recipient_id: user.id
|
||||
}).then(function (res) {
|
||||
return _this8.private_channels.add(new _StructuresPMChannel2["default"](res, _this8.client));
|
||||
return _this9.private_channels.add(new _StructuresPMChannel2["default"](res, _this9.client));
|
||||
});
|
||||
};
|
||||
|
||||
@@ -470,21 +484,21 @@ var InternalClient = (function () {
|
||||
// def sendMessage
|
||||
|
||||
InternalClient.prototype.sendMessage = function sendMessage(where, _content) {
|
||||
var _this9 = this;
|
||||
var _this10 = this;
|
||||
|
||||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
|
||||
|
||||
return this.resolver.resolveChannel(where).then(function (destination) {
|
||||
//var destination;
|
||||
var content = _this9.resolver.resolveString(_content);
|
||||
var mentions = _this9.resolver.resolveMentions(content);
|
||||
var content = _this10.resolver.resolveString(_content);
|
||||
var mentions = _this10.resolver.resolveMentions(content);
|
||||
|
||||
return _this9.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, {
|
||||
return _this10.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, {
|
||||
content: content,
|
||||
mentions: mentions,
|
||||
tts: options.tts
|
||||
}).then(function (res) {
|
||||
return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this9.client));
|
||||
return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this10.client));
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -492,7 +506,7 @@ var InternalClient = (function () {
|
||||
// def deleteMessage
|
||||
|
||||
InternalClient.prototype.deleteMessage = function deleteMessage(_message) {
|
||||
var _this10 = this;
|
||||
var _this11 = this;
|
||||
|
||||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
||||
|
||||
@@ -503,7 +517,7 @@ var InternalClient = (function () {
|
||||
|
||||
var chain = options.wait ? delay(options.wait) : Promise.resolve();
|
||||
return chain.then(function () {
|
||||
return _this10.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true);
|
||||
return _this11.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true);
|
||||
}).then(function () {
|
||||
return message.channel.messages.remove(message);
|
||||
});
|
||||
@@ -512,7 +526,7 @@ var InternalClient = (function () {
|
||||
// def updateMessage
|
||||
|
||||
InternalClient.prototype.updateMessage = function updateMessage(msg, _content) {
|
||||
var _this11 = this;
|
||||
var _this12 = this;
|
||||
|
||||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
|
||||
|
||||
@@ -530,23 +544,23 @@ var InternalClient = (function () {
|
||||
tts: options.tts,
|
||||
mentions: mentions
|
||||
}).then(function (res) {
|
||||
return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this11.client));
|
||||
return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this12.client));
|
||||
});
|
||||
};
|
||||
|
||||
// def sendFile
|
||||
|
||||
InternalClient.prototype.sendFile = function sendFile(where, _file) {
|
||||
var _this12 = this;
|
||||
var _this13 = this;
|
||||
|
||||
var name = arguments.length <= 2 || arguments[2] === undefined ? "image.png" : arguments[2];
|
||||
|
||||
return this.resolver.resolveChannel(where).then(function (channel) {
|
||||
return _this12.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
|
||||
return _this13.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
|
||||
name: name,
|
||||
file: _this12.resolver.resolveFile(_file)
|
||||
file: _this13.resolver.resolveFile(_file)
|
||||
}).then(function (res) {
|
||||
return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this12.client));
|
||||
return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this13.client));
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -554,7 +568,7 @@ var InternalClient = (function () {
|
||||
// def getChannelLogs
|
||||
|
||||
InternalClient.prototype.getChannelLogs = function getChannelLogs(_channel) {
|
||||
var _this13 = this;
|
||||
var _this14 = this;
|
||||
|
||||
var limit = arguments.length <= 1 || arguments[1] === undefined ? 500 : arguments[1];
|
||||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
|
||||
@@ -562,21 +576,21 @@ var InternalClient = (function () {
|
||||
return this.resolver.resolveChannel(_channel).then(function (channel) {
|
||||
var qsObject = { limit: limit };
|
||||
if (options.before) {
|
||||
var res = _this13.resolver.resolveMessage(options.before);
|
||||
var res = _this14.resolver.resolveMessage(options.before);
|
||||
if (res) {
|
||||
qsObject.before = res;
|
||||
}
|
||||
}
|
||||
if (options.after) {
|
||||
var res = _this13.resolver.resolveMessage(options.after);
|
||||
var res = _this14.resolver.resolveMessage(options.after);
|
||||
if (res) {
|
||||
qsObject.after = res;
|
||||
}
|
||||
}
|
||||
|
||||
return _this13.apiRequest("get", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id) + "?" + _querystring2["default"].stringify(qsObject), true).then(function (res) {
|
||||
return _this14.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, _this13.client));
|
||||
return channel.messages.add(new _StructuresMessage2["default"](msg, channel, _this14.client));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -585,13 +599,13 @@ var InternalClient = (function () {
|
||||
// def getBans
|
||||
|
||||
InternalClient.prototype.getBans = function getBans(server) {
|
||||
var _this14 = this;
|
||||
var _this15 = this;
|
||||
|
||||
server = this.resolver.resolveServer(server);
|
||||
|
||||
return this.apiRequest("get", _Constants.Endpoints.SERVER_BANS(server.id), true).then(function (res) {
|
||||
res.map(function (ban) {
|
||||
return _this14.users.add(new _StructuresUser2["default"](ban.user, _this14.client));
|
||||
return _this15.users.add(new _StructuresUser2["default"](ban.user, _this15.client));
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -599,7 +613,7 @@ var InternalClient = (function () {
|
||||
// def createChannel
|
||||
|
||||
InternalClient.prototype.createChannel = function createChannel(server, name) {
|
||||
var _this15 = this;
|
||||
var _this16 = this;
|
||||
|
||||
var type = arguments.length <= 2 || arguments[2] === undefined ? "text" : arguments[2];
|
||||
|
||||
@@ -611,23 +625,23 @@ var InternalClient = (function () {
|
||||
}).then(function (res) {
|
||||
var channel;
|
||||
if (res.type === "text") {
|
||||
channel = new _StructuresTextChannel2["default"](res, _this15.client, server);
|
||||
channel = new _StructuresTextChannel2["default"](res, _this16.client, server);
|
||||
} else {
|
||||
channel = new _StructuresVoiceChannel2["default"](res, _this15.client, server);
|
||||
channel = new _StructuresVoiceChannel2["default"](res, _this16.client, server);
|
||||
}
|
||||
return server.channels.add(_this15.channels.add(channel));
|
||||
return server.channels.add(_this16.channels.add(channel));
|
||||
});
|
||||
};
|
||||
|
||||
// def deleteChannel
|
||||
|
||||
InternalClient.prototype.deleteChannel = function deleteChannel(_channel) {
|
||||
var _this16 = this;
|
||||
var _this17 = this;
|
||||
|
||||
return this.resolver.resolveChannel(_channel).then(function (channel) {
|
||||
return _this16.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () {
|
||||
return _this17.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () {
|
||||
channel.server.channels.remove(channel);
|
||||
_this16.channels.remove(channel);
|
||||
_this17.channels.remove(channel);
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -665,15 +679,15 @@ var InternalClient = (function () {
|
||||
// def createRole
|
||||
|
||||
InternalClient.prototype.createRole = function createRole(server, data) {
|
||||
var _this17 = this;
|
||||
var _this18 = 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, _this17.client));
|
||||
var role = server.roles.add(new _StructuresRole2["default"](res, server, _this18.client));
|
||||
|
||||
if (data) {
|
||||
return _this17.updateRole(role, data);
|
||||
return _this18.updateRole(role, data);
|
||||
}
|
||||
return role;
|
||||
});
|
||||
@@ -682,7 +696,7 @@ var InternalClient = (function () {
|
||||
// def updateRole
|
||||
|
||||
InternalClient.prototype.updateRole = function updateRole(role, data) {
|
||||
var _this18 = this;
|
||||
var _this19 = this;
|
||||
|
||||
var server = this.resolver.resolveServer(role.server);
|
||||
|
||||
@@ -718,7 +732,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, _this18.client));
|
||||
return server.roles.update(role, new _StructuresRole2["default"](res, server, _this19.client));
|
||||
});
|
||||
};
|
||||
|
||||
@@ -857,7 +871,7 @@ var InternalClient = (function () {
|
||||
// def createInvite
|
||||
|
||||
InternalClient.prototype.createInvite = function createInvite(chanServ, options) {
|
||||
var _this19 = this;
|
||||
var _this20 = this;
|
||||
|
||||
if (chanServ instanceof _StructuresChannel2["default"]) {
|
||||
// do something
|
||||
@@ -890,7 +904,7 @@ var InternalClient = (function () {
|
||||
}
|
||||
|
||||
return this.apiRequest("post", epoint, true, options).then(function (res) {
|
||||
return new _StructuresInvite2["default"](res, _this19.channels.get("id", res.channel.id), _this19.client);
|
||||
return new _StructuresInvite2["default"](res, _this20.channels.get("id", res.channel.id), _this20.client);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -908,7 +922,7 @@ var InternalClient = (function () {
|
||||
//def overwritePermissions
|
||||
|
||||
InternalClient.prototype.overwritePermissions = function overwritePermissions(channel, role, updated) {
|
||||
var _this20 = this;
|
||||
var _this21 = this;
|
||||
|
||||
return this.resolver.resolveChannel(channel).then(function (channel) {
|
||||
var user;
|
||||
@@ -949,7 +963,7 @@ var InternalClient = (function () {
|
||||
}
|
||||
}
|
||||
|
||||
return _this20.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data);
|
||||
return _this21.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -985,49 +999,49 @@ var InternalClient = (function () {
|
||||
//def sendTyping
|
||||
|
||||
InternalClient.prototype.sendTyping = function sendTyping(channel) {
|
||||
var _this21 = this;
|
||||
var _this22 = this;
|
||||
|
||||
return this.resolver.resolveChannel(channel).then(function (channel) {
|
||||
return _this21.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true);
|
||||
return _this22.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true);
|
||||
});
|
||||
};
|
||||
|
||||
//def startTyping
|
||||
|
||||
InternalClient.prototype.startTyping = function startTyping(channel) {
|
||||
var _this22 = this;
|
||||
var _this23 = this;
|
||||
|
||||
return this.resolver.resolveChannel(channel).then(function (channel) {
|
||||
|
||||
if (_this22.intervals.typing[channel.id]) {
|
||||
if (_this23.intervals.typing[channel.id]) {
|
||||
// typing interval already exists, leave it alone
|
||||
throw new Error("Already typing in that channel");
|
||||
}
|
||||
|
||||
_this22.intervals.typing[channel.id] = setInterval(function () {
|
||||
return _this22.sendTyping(channel)["catch"](function (error) {
|
||||
return _this22.emit("error", error);
|
||||
_this23.intervals.typing[channel.id] = setInterval(function () {
|
||||
return _this23.sendTyping(channel)["catch"](function (error) {
|
||||
return _this23.emit("error", error);
|
||||
});
|
||||
}, 4000);
|
||||
|
||||
return _this22.sendTyping(channel);
|
||||
return _this23.sendTyping(channel);
|
||||
});
|
||||
};
|
||||
|
||||
//def stopTyping
|
||||
|
||||
InternalClient.prototype.stopTyping = function stopTyping(channel) {
|
||||
var _this23 = this;
|
||||
var _this24 = this;
|
||||
|
||||
return this.resolver.resolveChannel(channel).then(function (channel) {
|
||||
|
||||
if (!_this23.intervals.typing[channel.id]) {
|
||||
if (!_this24.intervals.typing[channel.id]) {
|
||||
// typing interval doesn"t exist
|
||||
throw new Error("Not typing in that channel");
|
||||
}
|
||||
|
||||
clearInterval(_this23.intervals.typing[channel.id]);
|
||||
_this23.intervals.typing[channel.id] = false;
|
||||
clearInterval(_this24.intervals.typing[channel.id]);
|
||||
_this24.intervals.typing[channel.id] = false;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1058,12 +1072,12 @@ var InternalClient = (function () {
|
||||
//def setTopic
|
||||
|
||||
InternalClient.prototype.setChannelTopic = function setChannelTopic(chann) {
|
||||
var _this24 = this;
|
||||
var _this25 = this;
|
||||
|
||||
var topic = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1];
|
||||
|
||||
return this.resolver.resolveChannel(chann).then(function (channel) {
|
||||
return _this24.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
|
||||
return _this25.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
|
||||
name: channel.name,
|
||||
position: channel.position,
|
||||
topic: topic
|
||||
@@ -1076,12 +1090,12 @@ var InternalClient = (function () {
|
||||
//def setChannelName
|
||||
|
||||
InternalClient.prototype.setChannelName = function setChannelName(chann) {
|
||||
var _this25 = this;
|
||||
var _this26 = this;
|
||||
|
||||
var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1];
|
||||
|
||||
return this.resolver.resolveChannel(chann).then(function (channel) {
|
||||
return _this25.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
|
||||
return _this26.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
|
||||
name: name,
|
||||
position: channel.position,
|
||||
topic: channel.topic
|
||||
@@ -1094,13 +1108,13 @@ var InternalClient = (function () {
|
||||
//def setChannelNameAndTopic
|
||||
|
||||
InternalClient.prototype.setChannelNameAndTopic = function setChannelNameAndTopic(chann) {
|
||||
var _this26 = this;
|
||||
var _this27 = 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 _this26.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
|
||||
return _this27.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
|
||||
name: name,
|
||||
position: channel.position,
|
||||
topic: topic
|
||||
@@ -1114,12 +1128,12 @@ var InternalClient = (function () {
|
||||
//def setTopic
|
||||
|
||||
InternalClient.prototype.setChannelPosition = function setChannelPosition(chann) {
|
||||
var _this27 = this;
|
||||
var _this28 = this;
|
||||
|
||||
var position = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
|
||||
|
||||
return this.resolver.resolveChannel(chann).then(function (channel) {
|
||||
return _this27.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
|
||||
return _this28.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
|
||||
name: channel.name,
|
||||
position: position,
|
||||
topic: channel.topic
|
||||
|
||||
@@ -43,7 +43,11 @@ var TokenCacher = (function (_EventEmitter) {
|
||||
this.data = {};
|
||||
}
|
||||
|
||||
TokenCacher.prototype.setToken = function setToken(email, password, token) {
|
||||
TokenCacher.prototype.setToken = function setToken() {
|
||||
var email = arguments.length <= 0 || arguments[0] === undefined ? "" : arguments[0];
|
||||
var password = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1];
|
||||
var token = arguments.length <= 2 || arguments[2] === undefined ? "" : arguments[2];
|
||||
|
||||
email = secureEmail(email, password);
|
||||
var cipher = _crypto2["default"].createCipher(algo, password);
|
||||
var crypted = cipher.update("valid" + token, "utf8", "hex");
|
||||
@@ -56,7 +60,9 @@ var TokenCacher = (function (_EventEmitter) {
|
||||
_fsExtra2["default"].writeJson(this.savePath, this.data);
|
||||
};
|
||||
|
||||
TokenCacher.prototype.getToken = function getToken(email, password) {
|
||||
TokenCacher.prototype.getToken = function getToken() {
|
||||
var email = arguments.length <= 0 || arguments[0] === undefined ? "" : arguments[0];
|
||||
var password = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1];
|
||||
|
||||
email = secureEmail(email, password);
|
||||
|
||||
|
||||
@@ -64,10 +64,21 @@ export default class InternalClient {
|
||||
ret.set('User-Agent', this.userAgentInfo.full);
|
||||
return new Promise((resolve, reject) => {
|
||||
ret.end((error, data) => {
|
||||
if(error) {
|
||||
return reject(error);
|
||||
}
|
||||
if (error) {
|
||||
if (error.response && error.response.error && error.response.error.status && error.response.error.status === 429) {
|
||||
if(data.headers["retry-after"] || data.headers["Retry-After"]){
|
||||
var toWait = data.headers["retry-after"] || data.headers["Retry-After"];
|
||||
toWait = parseInt(toWait);
|
||||
setTimeout(() => {
|
||||
this.apiRequest.apply(this, arguments).then(resolve).catch(reject);
|
||||
}, toWait);
|
||||
}
|
||||
} else {
|
||||
return reject(error);
|
||||
}
|
||||
}else{
|
||||
resolve(data.body);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ export default class TokenCacher extends EventEmitter {
|
||||
this.data = {};
|
||||
}
|
||||
|
||||
setToken(email, password, token) {
|
||||
setToken(email="", password="", token="") {
|
||||
email = secureEmail(email, password);
|
||||
var cipher = crypto.createCipher(algo, password)
|
||||
var crypted = cipher.update("valid" + token, "utf8", "hex")
|
||||
@@ -41,7 +41,7 @@ export default class TokenCacher extends EventEmitter {
|
||||
fs.writeJson(this.savePath, this.data);
|
||||
}
|
||||
|
||||
getToken(email, password) {
|
||||
getToken(email="", password="") {
|
||||
|
||||
email = secureEmail(email, password);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user