Skip to content

Commit 1628050

Browse files
committed
Added get_all_commands coroutine
1 parent ec10e4a commit 1628050

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

discord_slash/utils/manage_commands.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import aiohttp
2+
from ..error import RequestFailure
23

34

45
async def add_slash_command(bot_id,
@@ -17,6 +18,7 @@ async def add_slash_command(bot_id,
1718
:param description: Description of the command.
1819
:param options: List of the function.
1920
:return: JSON Response of the request.
21+
:raises: :class:`.error.RequestFailure` - Requesting to Discord API has failed.
2022
"""
2123
url = f"https://discord.com/api/v8/applications/{bot_id}"
2224
url += "/commands" if not guild_id else f"/guilds/{guild_id}/commands"
@@ -28,6 +30,8 @@ async def add_slash_command(bot_id,
2830

2931
async with aiohttp.ClientSession() as session:
3032
async with session.post(url, headers={"Authorization": f"Bot {bot_token}"}, json=base) as resp:
33+
if not 200 <= resp.status < 300:
34+
raise RequestFailure(resp.status, await resp.text())
3135
return await resp.json()
3236

3337

@@ -43,15 +47,39 @@ async def remove_slash_command(bot_id,
4347
:param guild_id: ID of the guild to remove command. Pass `None` to remove global command.
4448
:param cmd_id: ID of the command.
4549
:return: Response code of the request.
50+
:raises: :class:`.error.RequestFailure` - Requesting to Discord API has failed.
4651
"""
4752
url = f"https://discord.com/api/v8/applications/{bot_id}"
4853
url += "/commands" if not guild_id else f"/guilds/{guild_id}/commands"
4954
url += f"/{cmd_id}"
5055
async with aiohttp.ClientSession() as session:
5156
async with session.delete(url, headers={"Authorization": f"Bot {bot_token}"}) as resp:
57+
if not 200 <= resp.status < 300:
58+
raise RequestFailure(resp.status, await resp.text())
5259
return resp.status
5360

5461

62+
async def get_all_commands(bot_id,
63+
bot_token,
64+
guild_id):
65+
"""
66+
A coroutine that sends a slash command get request to Discord API.
67+
68+
:param bot_id: User ID of the bot.
69+
:param bot_token: Token of the bot.
70+
:param guild_id: ID of the guild to get commands. Pass `None` to get all global commands.
71+
:return: JSON Response of the request.
72+
:raises: :class:`.error.RequestFailure` - Requesting to Discord API has failed.
73+
"""
74+
url = f"https://discord.com/api/v8/applications/{bot_id}"
75+
url += "/commands" if not guild_id else f"/guilds/{guild_id}/commands"
76+
async with aiohttp.ClientSession() as session:
77+
async with session.get(url, headers={"Authorization": f"Bot {bot_token}"}) as resp:
78+
if not 200 <= resp.status < 300:
79+
raise RequestFailure(resp.status, await resp.text())
80+
return await resp.json()
81+
82+
5583
def create_option(name: str,
5684
description: str,
5785
option_type: int,

0 commit comments

Comments
 (0)