From 0d90798c6c344b47594aec373a83d7181ca0a8f8 Mon Sep 17 00:00:00 2001 From: Lewdcario Date: Fri, 11 May 2018 20:55:31 -0500 Subject: [PATCH] backport: rateLimit event --- src/client/rest/RequestHandlers/Burst.js | 10 ++++++++++ src/client/rest/RequestHandlers/Sequential.js | 18 ++++++++++++++++++ src/util/Constants.js | 1 + 3 files changed, 29 insertions(+) diff --git a/src/client/rest/RequestHandlers/Burst.js b/src/client/rest/RequestHandlers/Burst.js index 8f8e309d5..683c4cf94 100644 --- a/src/client/rest/RequestHandlers/Burst.js +++ b/src/client/rest/RequestHandlers/Burst.js @@ -1,5 +1,6 @@ const RequestHandler = require('./RequestHandler'); const DiscordAPIError = require('../DiscordAPIError'); +const { Events: { RATE_LIMIT } } = require('../../../util/Constants'); class BurstRequestHandler extends RequestHandler { constructor(restManager, endpoint) { @@ -52,6 +53,15 @@ class BurstRequestHandler extends RequestHandler { this.handle(); } } else { + if (this.remaining === 0) { + if (this.client.listenerCount(RATE_LIMIT)) { + this.client.emit(RATE_LIMIT, { + limit: this.limit, + timeDifference: this.timeDifference, + path: item.request.path, + }); + } + } this.globalLimit = false; const data = res && res.body ? res.body : {}; item.resolve(data); diff --git a/src/client/rest/RequestHandlers/Sequential.js b/src/client/rest/RequestHandlers/Sequential.js index 94ecd1dcb..b6afeb4aa 100644 --- a/src/client/rest/RequestHandlers/Sequential.js +++ b/src/client/rest/RequestHandlers/Sequential.js @@ -1,5 +1,6 @@ const RequestHandler = require('./RequestHandler'); const DiscordAPIError = require('../DiscordAPIError'); +const { Events: { RATE_LIMIT } } = require('../../../util/Constants'); /** * Handles API Requests sequentially, i.e. we wait until the current request is finished before moving onto @@ -16,6 +17,9 @@ class SequentialRequestHandler extends RequestHandler { constructor(restManager, endpoint) { super(restManager, endpoint); + this.manager = restManager; + this.client = restManager.client; + /** * The endpoint that this handler is handling * @type {string} @@ -77,6 +81,20 @@ class SequentialRequestHandler extends RequestHandler { const data = res && res.body ? res.body : {}; item.resolve(data); if (this.requestRemaining === 0) { + if (this.client.listenerCount(RATE_LIMIT)) { + /** + * Emitted when the client hits a rate limit while making a request + * @event Client#rateLimit + * @prop {number} requestLimit Number of requests that can be made to this endpoint + * @prop {number} timeDifference Delta-T in ms between your system and Discord servers + * @prop {string} path Path used for request that triggered this event + */ + this.client.emit(RATE_LIMIT, { + limit: this.requestLimit, + timeDifference: this.timeDifference, + path: item.request.path, + }); + } this.restManager.client.setTimeout( () => resolve(data), this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset diff --git a/src/util/Constants.js b/src/util/Constants.js index 9a252753c..25531a6d0 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -303,6 +303,7 @@ exports.VoiceOPCodes = { }; exports.Events = { + RATE_LIMIT: 'rateLimit', READY: 'ready', RESUME: 'resume', GUILD_CREATE: 'guildCreate',