Skip to content

Commit

Permalink
comply to the latest rate limit stuff (discord/discord-api-docs#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
amishshah committed Sep 24, 2016
1 parent a779a1b commit b13a47f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/docs.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/client/ClientManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class ClientManager {
this.client.rest.methods.getGateway().then(gateway => {
this.client.emit(Constants.Events.DEBUG, `Using gateway ${gateway}`);
this.client.ws.connect(gateway);
this.client.ws.once('close', event => {
if (event.code === 4004) reject(new Error(Constants.Errors.BAD_LOGIN));
if (event.code === 4010) reject(new Error(Constants.Errors.INVALID_SHARD));
});
this.client.once(Constants.Events.READY, () => {
resolve(token);
this.client.clearTimeout(timeout);
Expand Down
15 changes: 11 additions & 4 deletions src/client/rest/APIRequest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const request = require('superagent');
const Constants = require('../../util/Constants');

function getRoute(url) {
let route = url.split('?')[0];
if (route.includes('/channels/') || route.includes('/guilds/')) {
const startInd = ~route.indexOf('/channels/') ? route.indexOf('/channels/') : route.indexOf('/guilds/');
const majorID = route.substring(startInd).split('/')[2];
route = route.replace(/(\d{8,})/g, ':id').replace(':id', majorID);
}
return route;
}

class APIRequest {
constructor(rest, method, url, auth, data, file) {
this.rest = rest;
Expand All @@ -9,10 +19,7 @@ class APIRequest {
this.auth = auth;
this.data = data;
this.file = file;
}

getEndpoint() {
return this.url;
this.route = getRoute(this.url);
}

getAuth() {
Expand Down
6 changes: 3 additions & 3 deletions src/client/rest/RESTManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class RESTManager {
makeRequest(method, url, auth, data, file) {
const apiRequest = new APIRequest(this, method, url, auth, data, file);

if (!this.handlers[apiRequest.getEndpoint()]) {
if (!this.handlers[apiRequest.route]) {
const RequestHandlerType = this.getRequestHandler();
this.handlers[apiRequest.getEndpoint()] = new RequestHandlerType(this, apiRequest.getEndpoint());
this.handlers[apiRequest.route] = new RequestHandlerType(this, apiRequest.route);
}

return this.push(this.handlers[apiRequest.getEndpoint()], apiRequest);
return this.push(this.handlers[apiRequest.route], apiRequest);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/client/websocket/WebSocketManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const WebSocket = require('ws');
const EventEmitter = require('events').EventEmitter;
const Constants = require('../../util/Constants');
const zlib = require('zlib');
const PacketManager = require('./packets/WebSocketPacketManager');
Expand All @@ -7,8 +8,9 @@ const PacketManager = require('./packets/WebSocketPacketManager');
* The WebSocket Manager of the Client
* @private
*/
class WebSocketManager {
class WebSocketManager extends EventEmitter {
constructor(client) {
super();
/**
* The Client that instantiated this WebSocketManager
* @type {Client}
Expand Down Expand Up @@ -164,12 +166,14 @@ class WebSocketManager {
* @param {Object} event The received websocket data
*/
eventClose(event) {
this.emit('close', event);
/**
* Emitted whenever the client websocket is disconnected
* @event Client#disconnect
*/
if (!this.reconnecting) this.client.emit(Constants.Events.DISCONNECT);
if (event.code === 4004) throw new Error(Constants.Errors.BAD_LOGIN);
if (event.code === 4004) return;
if (event.code === 4010) return;
if (!this.reconnecting && event.code !== 1000) this.tryReconnect();
}

Expand Down
1 change: 1 addition & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ exports.Errors = {
NOT_A_PERMISSION: 'Invalid permission string or number.',
INVALID_RATE_LIMIT_METHOD: 'Unknown rate limiting method.',
BAD_LOGIN: 'Incorrect login details were provided.',
INVALID_SHARD: 'Invalid shard settings were provided',
};

const API = `https://discordapp.com/api/v${exports.DefaultOptions.protocol_version}`;
Expand Down

0 comments on commit b13a47f

Please sign in to comment.