mirror of
https://github.com/danbulant/discord.js
synced 2026-06-09 17:52:55 +00:00
Use Date headers to offset time differences to fix rate limiting
This commit is contained in:
parent
00be14b5d1
commit
fd64fb0a82
1 changed files with 9 additions and 1 deletions
|
|
@ -16,6 +16,13 @@ module.exports = class SequentialRequestHandler extends RequestHandler {
|
||||||
* @type {Boolean}
|
* @type {Boolean}
|
||||||
*/
|
*/
|
||||||
this.waiting = false;
|
this.waiting = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The time difference between Discord's Dates and the local computer's Dates. A positive number means the local
|
||||||
|
* computer's time is ahead of Discord's.
|
||||||
|
* @type {Number}
|
||||||
|
*/
|
||||||
|
this.timeDifference = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
push(request) {
|
push(request) {
|
||||||
|
|
@ -35,6 +42,7 @@ module.exports = class SequentialRequestHandler extends RequestHandler {
|
||||||
this.requestLimit = res.headers['x-ratelimit-limit'];
|
this.requestLimit = res.headers['x-ratelimit-limit'];
|
||||||
this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
|
this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
|
||||||
this.requestRemaining = Number(res.headers['x-ratelimit-remaining']);
|
this.requestRemaining = Number(res.headers['x-ratelimit-remaining']);
|
||||||
|
this.timeDifference = Date.now() - new Date(res.headers.date).getTime();
|
||||||
}
|
}
|
||||||
if (err) {
|
if (err) {
|
||||||
this.waiting = false;
|
this.waiting = false;
|
||||||
|
|
@ -48,7 +56,7 @@ module.exports = class SequentialRequestHandler extends RequestHandler {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.waiting = false;
|
this.waiting = false;
|
||||||
resolve(data);
|
resolve(data);
|
||||||
}, (this.requestResetTime - Date.now()) + 4000);
|
}, (this.requestResetTime - Date.now()) + this.timeDifference + 1000);
|
||||||
} else {
|
} else {
|
||||||
this.waiting = false;
|
this.waiting = false;
|
||||||
resolve(data);
|
resolve(data);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue