Skip to content

Commit

Permalink
Merge pull request #199 from eunwoo1104/AnotherCat-fix-manage_commands
Browse files Browse the repository at this point in the history
Fix manage commands
  • Loading branch information
eunwoo1104 authored Jun 4, 2021
2 parents f5baaeb + 6d74aa7 commit e950add
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions discord_slash/utils/manage_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ async def get_all_guild_commands_permissions(bot_id,
bot_token,
guild_id):
"""
A coroutine that sends a slash command get request to Discord API.
A coroutine that sends a gets all the commands permissions for that guild.
:param bot_id: User ID of the bot.
:param bot_token: Token of the bot.
:param guild_id: ID of the guild to get permissions.
:return: JSON Response of the request. A list of <https://discord.com/developers/docs/interactions/slash-commands#get-application-command-permissions>.
:raises: :class:`.error.RequestFailure` - Requesting to Discord API has failed.
"""
url = f"https://discord.com/api/v8/applications/{bot_id}/guilds/{guild_id}/permissions"
url = f"https://discord.com/api/v8/applications/{bot_id}/guilds/{guild_id}/commands/permissions"
async with aiohttp.ClientSession() as session:
async with session.get(url, headers={"Authorization": f"Bot {bot_token}"}) as resp:
if resp.status == 429:
Expand All @@ -165,15 +165,42 @@ async def get_all_guild_commands_permissions(bot_id,
if not 200 <= resp.status < 300:
raise RequestFailure(resp.status, await resp.text())
return await resp.json()

async def get_guild_command_permissions(bot_id,
bot_token,
guild_id,
command_id):
"""
A coroutine that sends a request to get a single command's permissions in guild
:param bot_id: User ID of the bot.
:param bot_token: Token of the bot.
:param guild_id: ID of the guild to update permissions on.
:param command_id: ID for the command to update permissions on.
:param permissions: List of permissions for the command.
:return: JSON Response of the request. A list of <https://discord.com/developers/docs/interactions/slash-commands#edit-application-command-permissions>
:raises: :class:`.error.RequestFailure` - Requesting to Discord API has failed.
"""
url = f"https://discord.com/api/v8/applications/{bot_id}/guilds/{guild_id}/commands/{command_id}/permissions"
async with aiohttp.ClientSession() as session:
async with session.get(url, headers={"Authorization": f"Bot {bot_token}"}) as resp:
if resp.status == 429:
_json = await resp.json()
await asyncio.sleep(_json["retry_after"])
return await get_guild_command_permissions(bot_id, bot_token, guild_id)
if not 200 <= resp.status < 300:
raise RequestFailure(resp.status, await resp.text())
return await resp.json()



async def update_single_command_permissions(bot_id,
bot_token,
guild_id,
command_id,
permissions):
"""
A coroutine that sends a slash command put request to Discord API.
A coroutine that sends a request to update a single command's permissions in guild
:param bot_id: User ID of the bot.
:param bot_token: Token of the bot.
Expand All @@ -189,18 +216,19 @@ async def update_single_command_permissions(bot_id,
if resp.status == 429:
_json = await resp.json()
await asyncio.sleep(_json["retry_after"])
return await update_guild_commands_permissions(bot_id, bot_token, guild_id, permissions)
return await update_single_command_permissions(bot_id, bot_token, guild_id, permissions)
if not 200 <= resp.status < 300:
raise RequestFailure(resp.status, await resp.text())
return await resp.json()



async def update_guild_commands_permissions(bot_id,
bot_token,
guild_id,
cmd_permissions):
"""
A coroutine that sends a slash command put request to Discord API.
A coroutine that updates permissions for all commands in a guild.
:param bot_id: User ID of the bot.
:param bot_token: Token of the bot.
Expand All @@ -209,7 +237,7 @@ async def update_guild_commands_permissions(bot_id,
:return: JSON Response of the request. A list of <https://discord.com/developers/docs/interactions/slash-commands#batch-edit-application-command-permissions>.
:raises: :class:`.error.RequestFailure` - Requesting to Discord API has failed.
"""
url = f"https://discord.com/api/v8/applications/{bot_id}/guilds/{guild_id}/permissions"
url = f"https://discord.com/api/v8/applications/{bot_id}/guilds/{guild_id}/commands/permissions"
async with aiohttp.ClientSession() as session:
async with session.put(url, headers={"Authorization": f"Bot {bot_token}"}, json=cmd_permissions) as resp:
if resp.status == 429:
Expand Down

0 comments on commit e950add

Please sign in to comment.