Skip to content

Commit 6790920

Browse files
committed
Added rate limit handling to manage_commands utils
1 parent 7ffb2c7 commit 6790920

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

discord_slash/utils/manage_commands.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import aiohttp
23
from ..error import RequestFailure
34

@@ -30,6 +31,10 @@ async def add_slash_command(bot_id,
3031

3132
async with aiohttp.ClientSession() as session:
3233
async with session.post(url, headers={"Authorization": f"Bot {bot_token}"}, json=base) as resp:
34+
if resp.status == 429:
35+
_json = await resp.json()
36+
await asyncio.sleep(_json["retry_after"])
37+
return await add_slash_command(bot_id, bot_token, guild_id, cmd_name, description, options)
3338
if not 200 <= resp.status < 300:
3439
raise RequestFailure(resp.status, await resp.text())
3540
return await resp.json()
@@ -54,6 +59,10 @@ async def remove_slash_command(bot_id,
5459
url += f"/{cmd_id}"
5560
async with aiohttp.ClientSession() as session:
5661
async with session.delete(url, headers={"Authorization": f"Bot {bot_token}"}) as resp:
62+
if resp.status == 429:
63+
_json = await resp.json()
64+
await asyncio.sleep(_json["retry_after"])
65+
return await remove_slash_command(bot_id, bot_token, guild_id, cmd_id)
5766
if not 200 <= resp.status < 300:
5867
raise RequestFailure(resp.status, await resp.text())
5968
return resp.status
@@ -75,6 +84,10 @@ async def get_all_commands(bot_id,
7584
url += "/commands" if not guild_id else f"/guilds/{guild_id}/commands"
7685
async with aiohttp.ClientSession() as session:
7786
async with session.get(url, headers={"Authorization": f"Bot {bot_token}"}) as resp:
87+
if resp.status == 429:
88+
_json = await resp.json()
89+
await asyncio.sleep(_json["retry_after"])
90+
return await remove_slash_command(bot_id, bot_token, guild_id)
7891
if not 200 <= resp.status < 300:
7992
raise RequestFailure(resp.status, await resp.text())
8093
return await resp.json()

0 commit comments

Comments
 (0)