11import aiohttp
2+ from aiohttp .client_reqrep import ClientResponse
23from ..tools import MISSING , get , setup_logger
3- from ..http import BetterRoute
4+ from ..http import BetterRoute , handle_rate_limit
45
56from discord .ext import commands as com
67from discord .errors import Forbidden , HTTPException , NotFound
@@ -21,7 +22,7 @@ async def get_id(command_name, client: com.bot, guild_id=MISSING):
2122async def delete_global_commands (client : com .Bot ):
2223 commands = await client .http .request (BetterRoute ("GET" , f"/applications/{ client .user .id } /commands" ))
2324 for x in commands :
24- await delete_global_command (client , x ["id" ])
25+ await delete_global_command (client , x ["id" ])
2526async def delete_guild_commands (client : com .Bot , guild_id ):
2627 try :
2728 commands = await client .http .request (BetterRoute ("GET" , f"/applications/{ client .user .id } /guilds/{ guild_id } /commands" ))
@@ -31,37 +32,97 @@ async def delete_guild_commands(client: com.Bot, guild_id):
3132 logging .warn ("got forbidden in " + str (guild_id ))
3233
3334async def delete_global_command (client : com .Bot , command_id ):
34- return await client .http .request (BetterRoute ("DELETE" , f"/applications/{ client .user .id } /commands/{ command_id } " ))
35+ try :
36+ return await client .http .request (BetterRoute ("DELETE" , f"/applications/{ client .user .id } /commands/{ command_id } " ))
37+ except HTTPException as ex :
38+ if ex .status == 429 :
39+ await handle_rate_limit (await ex .response .json ())
40+ return await delete_global_command (client , command_id )
41+ raise ex
3542async def delete_guild_command (client : com .Bot , command_id , guild_id ):
36- return await client .http .request (BetterRoute ("DELETE" , f"/applications/{ client .user .id } /guilds/{ guild_id } /commands/{ command_id } " ))
43+ try :
44+ return await client .http .request (BetterRoute ("DELETE" , f"/applications/{ client .user .id } /guilds/{ guild_id } /commands/{ command_id } " ))
45+ except HTTPException as ex :
46+ if ex .status == 429 :
47+ await handle_rate_limit (await ex .response .json ())
48+ return await delete_guild_command (client , command_id , guild_id )
49+ else :
50+ raise ex
51+ except Exception as ex :
52+ print ("caught exception" , ex )
3753
3854async def get_command_permissions (client : com .Bot , command_id , guild_id ):
3955 try :
4056 return await client .http .request (BetterRoute ("GET" , f"/applications/{ client .user .id } /guilds/{ guild_id } /commands/{ command_id } /permissions" ))
4157 except NotFound :
4258 return {"id" : command_id , "application_id" : client .user .id , "permissions" : []}
59+ except HTTPException as ex :
60+ if ex .status == 429 :
61+ await handle_rate_limit (await ex .response .json ())
62+ return await get_command_permissions (client , command_id , guild_id )
63+ else :
64+ raise ex
4365async def update_command_permissions (application_id , token , guild_id , command_id , permissions ):
4466 async with aiohttp .ClientSession () as client :
4567 async with client .put (f"https://discord.com/api/v9/applications/{ application_id } /guilds/{ guild_id } /commands/{ command_id } /permissions" ,
4668 headers = {"Authorization" : "Bot " + token }, json = {"permissions" : permissions }) as response :
4769 if response .status == 200 :
4870 return await response .json ()
71+ elif response .status == 429 :
72+ await handle_rate_limit (await response .json ())
73+ return await update_command_permissions (application_id , token , guild_id , command_id , permissions )
4974 raise HTTPException (response , response .content )
5075
5176async def create_global_command (command : dict , client : com .Bot ):
52- return await client .http .request (BetterRoute ("POST" , f"/applications/{ client .user .id } /commands" ), json = command )
77+ try :
78+ return await client .http .request (BetterRoute ("POST" , f"/applications/{ client .user .id } /commands" ), json = command )
79+ except HTTPException as ex :
80+ if ex .status == 429 :
81+ await handle_rate_limit (await ex .response .json ())
82+ return await create_global_command (command , client )
83+ raise ex
5384async def create_guild_command (command , client : com .Bot , guild_id , permissions = []):
54- data = await client .http .request (BetterRoute ("POST" , f"/applications/{ client .user .id } /guilds/{ guild_id } /commands" ), json = command )
55- return await update_command_permissions (client .user .id , client .http .token , guild_id , data ["id" ], permissions )
85+ try :
86+ data = await client .http .request (BetterRoute ("POST" , f"/applications/{ client .user .id } /guilds/{ guild_id } /commands" ), json = command )
87+ return await update_command_permissions (client .user .id , client .http .token , guild_id , data ["id" ], permissions )
88+ except HTTPException as ex :
89+ if ex .status == 429 :
90+ await handle_rate_limit (await ex .response .json ())
91+ return await create_guild_command (command , client , guild_id , permissions )
92+ raise ex
5693
5794
5895async def edit_global_command (command_id : str , client : com .Bot , new_command : dict ):
59- return await client .http .request (BetterRoute ("PATCH" , f"/applications/{ client .user .id } /commands/{ command_id } " ), json = new_command )
96+ try :
97+ return await client .http .request (BetterRoute ("PATCH" , f"/applications/{ client .user .id } /commands/{ command_id } " ), json = new_command )
98+ except HTTPException as ex :
99+ if ex .status == 429 :
100+ await handle_rate_limit (await ex .response .json ())
101+ return await edit_global_command (command_id , client , new_command )
102+ raise ex
60103async def edit_guild_command (command_id , client : com .Bot , guild_id : str , new_command : dict , permissions : dict ):
61- data = await client .http .request (BetterRoute ("PATCH" , f"/applications/{ client .user .id } /guilds/{ guild_id } /commands/{ command_id } " ), json = new_command )
62- return await update_command_permissions (client .user .id , client .http .token , guild_id , data ["id" ], permissions )
104+ try :
105+ data = await client .http .request (BetterRoute ("PATCH" , f"/applications/{ client .user .id } /guilds/{ guild_id } /commands/{ command_id } " ), json = new_command )
106+ return await update_command_permissions (client .user .id , client .http .token , guild_id , data ["id" ], permissions )
107+ except HTTPException as ex :
108+ if ex .status == 429 :
109+ await handle_rate_limit (await ex .response .json ())
110+ return await edit_guild_command (command_id , client , guild_id , new_command , permissions )
111+ raise ex
63112
64113async def get_global_commands (client ):
65- return await client .http .request (BetterRoute ("GET" , f"/applications/{ client .user .id } /commands" ))
114+ try :
115+ return await client .http .request (BetterRoute ("GET" , f"/applications/{ client .user .id } /commands" ))
116+ except HTTPException as ex :
117+ if ex .status == 429 :
118+ await handle_rate_limit (await ex .response .json ())
119+ return await get_global_commands (client )
120+ raise ex
66121async def get_guild_commands (client , guild_id ):
67- return await client .http .request (BetterRoute ("GET" , f"/applications/{ client .user .id } /guilds/{ guild_id } /commands" ))
122+ try :
123+ return await client .http .request (BetterRoute ("GET" , f"/applications/{ client .user .id } /guilds/{ guild_id } /commands" ))
124+ except HTTPException as ex :
125+ if ex .status == 429 :
126+ await handle_rate_limit (await ex .response .json ())
127+ return await get_guild_commands (client , guild_id )
128+ raise ex
0 commit comments