mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
backport: rateLimit event
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -303,6 +303,7 @@ exports.VoiceOPCodes = {
|
||||
};
|
||||
|
||||
exports.Events = {
|
||||
RATE_LIMIT: 'rateLimit',
|
||||
READY: 'ready',
|
||||
RESUME: 'resume',
|
||||
GUILD_CREATE: 'guildCreate',
|
||||
|
||||
Reference in New Issue
Block a user