mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
Time Difference in REST (#2057)
This commit is contained in:
@@ -12,12 +12,22 @@ class RESTManager {
|
|||||||
this.globallyRateLimited = false;
|
this.globallyRateLimited = false;
|
||||||
this.tokenPrefix = tokenPrefix;
|
this.tokenPrefix = tokenPrefix;
|
||||||
this.versioned = true;
|
this.versioned = true;
|
||||||
|
this.timeDifferences = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
get api() {
|
get api() {
|
||||||
return routeBuilder(this);
|
return routeBuilder(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get timeDifference() {
|
||||||
|
return Math.round(this.timeDifferences.reduce((a, b) => a + b, 0) / this.timeDifferences.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
set timeDifference(ms) {
|
||||||
|
this.timeDifferences.unshift(ms);
|
||||||
|
if (this.timeDifferences.length > 5) this.timeDifferences.length = 5;
|
||||||
|
}
|
||||||
|
|
||||||
getAuth() {
|
getAuth() {
|
||||||
const token = this.client.token || this.client.accessToken;
|
const token = this.client.token || this.client.accessToken;
|
||||||
const prefixed = !!this.client.application || (this.client.user && this.client.user.bot);
|
const prefixed = !!this.client.application || (this.client.user && this.client.user.bot);
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ class RequestHandler {
|
|||||||
this.limit = Infinity;
|
this.limit = Infinity;
|
||||||
this.resetTime = null;
|
this.resetTime = null;
|
||||||
this.remaining = 1;
|
this.remaining = 1;
|
||||||
this.timeDifference = 0;
|
|
||||||
|
|
||||||
this.queue = [];
|
this.queue = [];
|
||||||
}
|
}
|
||||||
@@ -32,7 +31,7 @@ class RequestHandler {
|
|||||||
const finish = timeout => {
|
const finish = timeout => {
|
||||||
if (timeout || this.limited) {
|
if (timeout || this.limited) {
|
||||||
if (!timeout) {
|
if (!timeout) {
|
||||||
timeout = this.resetTime - Date.now() + this.timeDifference + this.client.options.restTimeOffset;
|
timeout = this.resetTime - Date.now() + this.manager.timeDifference + this.client.options.restTimeOffset;
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line prefer-promise-reject-errors
|
// eslint-disable-next-line prefer-promise-reject-errors
|
||||||
reject({ timeout });
|
reject({ timeout });
|
||||||
@@ -50,7 +49,7 @@ class RequestHandler {
|
|||||||
this.client.emit(RATE_LIMIT, {
|
this.client.emit(RATE_LIMIT, {
|
||||||
timeout,
|
timeout,
|
||||||
limit: this.limit,
|
limit: this.limit,
|
||||||
timeDifference: this.timeDifference,
|
timeDifference: this.manager.timeDifference,
|
||||||
method: item.request.method,
|
method: item.request.method,
|
||||||
path: item.request.path,
|
path: item.request.path,
|
||||||
route: item.request.route,
|
route: item.request.route,
|
||||||
@@ -66,7 +65,7 @@ class RequestHandler {
|
|||||||
this.limit = Number(res.headers['x-ratelimit-limit']);
|
this.limit = Number(res.headers['x-ratelimit-limit']);
|
||||||
this.resetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
|
this.resetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
|
||||||
this.remaining = Number(res.headers['x-ratelimit-remaining']);
|
this.remaining = Number(res.headers['x-ratelimit-remaining']);
|
||||||
this.timeDifference = Date.now() - new Date(res.headers.date).getTime();
|
this.manager.timeDifference = Date.now() - new Date(res.headers.date).getTime();
|
||||||
}
|
}
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.status === 429) {
|
if (err.status === 429) {
|
||||||
|
|||||||
Reference in New Issue
Block a user