File tree Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Original file line number Diff line number Diff line change 1
1
import os
2
2
from dataclasses import dataclass
3
+ from time import sleep
3
4
4
5
import requests
5
6
from constants import guild_id
@@ -47,6 +48,16 @@ async def _discord_request(
47
48
"User-Agent" : "DiscordBot (https://github.com/CSSS/csss-site-backend, 1.0)"
48
49
}
49
50
)
51
+ rate_limit_reset = float (result .headers ["x-ratelimit-reset-after" ])
52
+ rate_limit_remaining_requests = int (result .headers ["x-ratelimit-remaining" ])
53
+
54
+ if rate_limit_remaining_requests <= 2 :
55
+ # this rate limits the current thread from doing too many requests, however it won't
56
+ # limit other threads.
57
+ # TODO: in the future, we'll want to create a singleton that thread locks
58
+ # usage of the same api key to N at a time, and waits if there are no requests remaining
59
+ sleep (rate_limit_reset )
60
+
50
61
return result
51
62
52
63
async def get_channel_members (
@@ -330,6 +341,7 @@ async def search_user(
330
341
url = f"https://discord.com/api/v10/guilds/{ gid } /members/search?query={ user } "
331
342
result = await _discord_request (url , token )
332
343
json = result .json ()
344
+
333
345
if len (json ) == 0 :
334
346
return None
335
347
json = json [0 ]["user" ]
You can’t perform that action at this time.
0 commit comments