Skip to content

Commit dbb7e3b

Browse files
committed
Added rate limit handling
1 parent 19a4878 commit dbb7e3b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

discord_slash/http.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import aiohttp
2+
import asyncio
23
from .error import RequestFailure
34

45

56
class SlashCommandRequest:
6-
def __init__(self):
7-
pass
7+
def __init__(self, logger):
8+
self.logger = logger
89

910
async def post(self, _resp, bot_id, interaction_id, token, initial=False) -> None:
1011
"""
@@ -24,6 +25,11 @@ async def post(self, _resp, bot_id, interaction_id, token, initial=False) -> Non
2425
else f"https://discord.com/api/v8/webhooks/{bot_id}/{token}"
2526
async with aiohttp.ClientSession() as session:
2627
async with session.post(req_url, json=_resp) as resp:
28+
if resp.status == 429:
29+
_json = await resp.json()
30+
self.logger.warning(f"We are being rate limited, retrying after {_json['retry_after']} seconds.")
31+
await asyncio.sleep(_json["retry_after"])
32+
return await self.post(_resp, bot_id, interaction_id, token, initial)
2733
if not 200 <= resp.status < 300:
2834
raise RequestFailure(resp.status, await resp.text())
2935
return None

0 commit comments

Comments
 (0)