mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 04:53:30 +01:00
backport: rateLimit event
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
const RequestHandler = require('./RequestHandler');
|
const RequestHandler = require('./RequestHandler');
|
||||||
const DiscordAPIError = require('../DiscordAPIError');
|
const DiscordAPIError = require('../DiscordAPIError');
|
||||||
|
const { Events: { RATE_LIMIT } } = require('../../../util/Constants');
|
||||||
|
|
||||||
class BurstRequestHandler extends RequestHandler {
|
class BurstRequestHandler extends RequestHandler {
|
||||||
constructor(restManager, endpoint) {
|
constructor(restManager, endpoint) {
|
||||||
@@ -52,6 +53,15 @@ class BurstRequestHandler extends RequestHandler {
|
|||||||
this.handle();
|
this.handle();
|
||||||
}
|
}
|
||||||
} else {
|
} 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;
|
this.globalLimit = false;
|
||||||
const data = res && res.body ? res.body : {};
|
const data = res && res.body ? res.body : {};
|
||||||
item.resolve(data);
|
item.resolve(data);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const RequestHandler = require('./RequestHandler');
|
const RequestHandler = require('./RequestHandler');
|
||||||
const DiscordAPIError = require('../DiscordAPIError');
|
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
|
* 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) {
|
constructor(restManager, endpoint) {
|
||||||
super(restManager, endpoint);
|
super(restManager, endpoint);
|
||||||
|
|
||||||
|
this.manager = restManager;
|
||||||
|
this.client = restManager.client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The endpoint that this handler is handling
|
* The endpoint that this handler is handling
|
||||||
* @type {string}
|
* @type {string}
|
||||||
@@ -77,6 +81,20 @@ class SequentialRequestHandler extends RequestHandler {
|
|||||||
const data = res && res.body ? res.body : {};
|
const data = res && res.body ? res.body : {};
|
||||||
item.resolve(data);
|
item.resolve(data);
|
||||||
if (this.requestRemaining === 0) {
|
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(
|
this.restManager.client.setTimeout(
|
||||||
() => resolve(data),
|
() => resolve(data),
|
||||||
this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset
|
this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset
|
||||||
|
|||||||
@@ -303,6 +303,7 @@ exports.VoiceOPCodes = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.Events = {
|
exports.Events = {
|
||||||
|
RATE_LIMIT: 'rateLimit',
|
||||||
READY: 'ready',
|
READY: 'ready',
|
||||||
RESUME: 'resume',
|
RESUME: 'resume',
|
||||||
GUILD_CREATE: 'guildCreate',
|
GUILD_CREATE: 'guildCreate',
|
||||||
|
|||||||
Reference in New Issue
Block a user