This commit is contained in:
May 2023-10-27 10:11:48 -07:00 committed by GitHub
parent 16db5c3196
commit a096a3352e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,6 +14,10 @@ interface QueuedRequest {
params : RequestInit
}
/**
* @description Extracts data on ratelimits from headers
* @param headers Headers object to extract information from
*/
function extractRatelimitData(headers: Headers): RatelimitData {
return {
bucket_name : headers.get("x-ratelimit-bucket")!,
@ -63,15 +67,6 @@ class DiscordAPIBucket {
}
/**
* @description update the remainding amount of requests
* @param remaining number to update to
*/
update(remaining: number) {
this.remaining = Math.max(Math.min(0, remaining), this.remaining)
return this
}
/**
* @description Link a route to this bucket
* @param route Route to link
@ -138,8 +133,9 @@ export class REST {
// check if there's already a bucket, and check if it's full
let known_bucket = getBucket( path )
if (known_bucket && known_bucket.remaining) {
return this.queue(path, options)
if (known_bucket) {
if (known_bucket.remaining <= 0) return this.queue(path, options)
else known_bucket.remaining-- // just in case...
}
// there's no known bucket for this route; let's carry on with the request
@ -157,9 +153,9 @@ export class REST {
so this would be fine */
}
// let's update the bucket...
// let's update the bucket with data from the source now
let rd = extractRatelimitData( response.headers )
bucket.update(rd.remaining)
bucket.remaining = rd.remaining
} else return response