Fix rate limit

This commit is contained in:
SeanOMik 2020-06-17 22:17:46 -05:00
parent ef1d87a311
commit a4055c5fd9
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
1 changed files with 17 additions and 9 deletions

View File

@ -69,21 +69,29 @@ proc waitForRateLimits*(objectID: snowflake, bucketType: RateLimitBucketType) =
else: else:
case bucketType: case bucketType:
of RateLimitBucketType.channel: of RateLimitBucketType.channel:
rlmt = channelRatelimitBucket[objectID] if (channelRatelimitBucket.hasKey(objectID)):
discard rlmt = channelRatelimitBucket[objectID]
else:
channelRatelimitBucket.add(objectID, newRateLimit())
rlmt = channelRatelimitBucket[objectID]
of RateLimitBucketType.guild: of RateLimitBucketType.guild:
rlmt = guildRatelimitBucket[objectID] if (guildRatelimitBucket.hasKey(objectID)):
discard rlmt = guildRatelimitBucket[objectID]
else:
guildRatelimitBucket.add(objectID, newRateLimit())
rlmt = guildRatelimitBucket[objectID]
of RateLimitBucketType.webhook: of RateLimitBucketType.webhook:
rlmt = webhookRatelimitBucket[objectID] if (webhookRatelimitBucket.hasKey(objectID)):
discard rlmt = webhookRatelimitBucket[objectID]
else:
webhookRatelimitBucket.add(objectID, newRateLimit())
rlmt = webhookRatelimitBucket[objectID]
of RateLimitBucketType.global: of RateLimitBucketType.global:
rlmt = globalRateLimit rlmt = globalRateLimit
discard
if (rlmt.remainingLimit == 0): if (rlmt != nil and rlmt.remainingLimit == 0):
let millisecondTime: float = rlmt.ratelimitReset * 1000 - epochTime() * 1000 let millisecondTime: float = rlmt.ratelimitReset * 1000 - epochTime() * 1000
if (millisecondTime > 0): if (millisecondTime > 0):
echo fmt("Rate limit wait time: {millisecondTime} miliseconds") echo fmt("Rate limit wait time: {millisecondTime} miliseconds")
discard sleepAsync(millisecondTime) discard sleepAsync(millisecondTime)