Skip to content

Commit

Permalink
Merge pull request Pycord-Development#395 from Dorukyum/get_applicati…
Browse files Browse the repository at this point in the history
…on_command

Implement `bot.get_application_command`
  • Loading branch information
BobDotCom authored Nov 11, 2021
2 parents 9eb30ae + 0359234 commit 4b746ef
Showing 1 changed file with 59 additions and 8 deletions.
67 changes: 59 additions & 8 deletions discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Coroutine,
List,
Optional,
Type,
TypeVar,
Union,
)
Expand Down Expand Up @@ -86,19 +87,23 @@ class ApplicationCommandMixin:
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._pending_application_commands = []
self.application_commands = {}
self._application_commands = {}

@property
def pending_application_commands(self):
return self._pending_application_commands

@property
def commands(self) -> List[Union[ApplicationCommand, Any]]:
commands = list(self.application_commands.values())
commands = self.application_commands
if self._supports_prefixed_commands:
commands += self.prefixed_commands
return commands

@property
def application_commands(self) -> List[ApplicationCommand]:
return list(self._application_commands.values())

def add_application_command(self, command: ApplicationCommand) -> None:
"""Adds a :class:`.ApplicationCommand` into the internal list of commands.
Expand All @@ -112,7 +117,6 @@ def add_application_command(self, command: ApplicationCommand) -> None:
command: :class:`.ApplicationCommand`
The command to add.
"""

if self.debug_guilds and command.guild_ids is None:
command.guild_ids = self.debug_guilds
self._pending_application_commands.append(command)
Expand All @@ -136,7 +140,54 @@ def remove_application_command(
The command that was removed. If the name is not valid then
``None`` is returned instead.
"""
return self.application_commands.pop(command.id)
return self._application_commands.pop(command.id)

@property
def get_command(self):
"""Shortcut for :meth:`.get_application_command`.
.. note::
Overridden in :class:`ext.commands.Bot`.
.. versionadded:: 2.0
"""
# TODO: Do something like we did in self.commands for this
return self.get_application_command

def get_application_command(
self,
name: str,
guild_ids: Optional[List[int]] = None,
type: Type[ApplicationCommand] = SlashCommand,
) -> Optional[ApplicationCommand]:
"""Get a :class:`.ApplicationCommand` from the internal list
of commands.
.. versionadded:: 2.0
Parameters
-----------
name: :class:`str`
The name of the command to get.
guild_ids: List[:class:`int`]
The guild ids associated to the command to get.
type: Type[:class:`.ApplicationCommand`]
The type of the command to get. Defaults to :class:`.SlashCommand`.
Returns
--------
Optional[:class:`.ApplicationCommand`]
The command that was requested. If not found, returns ``None``.
"""

for command in self._application_commands.values():
if (
command.name == name
and isinstance(command, type)
):
if guild_ids is not None and command.guild_ids != guild_ids:
return
return command

async def sync_commands(self) -> None:
"""|coro|
Expand Down Expand Up @@ -199,7 +250,7 @@ async def register_commands(self) -> None:
type=i["type"],
)
cmd.id = i["id"]
self.application_commands[cmd.id] = cmd
self._application_commands[cmd.id] = cmd

# Permissions (Roles will be converted to IDs just before Upsert for Global Commands)
global_permissions.append({"id": i["id"], "permissions": cmd.permissions})
Expand Down Expand Up @@ -234,7 +285,7 @@ async def register_commands(self) -> None:
for i in cmds:
cmd = find(lambda cmd: cmd.name == i["name"] and cmd.type == i["type"] and int(i["guild_id"]) in cmd.guild_ids, self.pending_application_commands)
cmd.id = i["id"]
self.application_commands[cmd.id] = cmd
self._application_commands[cmd.id] = cmd

# Permissions
permissions = [
Expand Down Expand Up @@ -325,7 +376,7 @@ async def register_commands(self) -> None:
if len(new_cmd_perm["permissions"]) > 10:
print(
"Command '{name}' has more than 10 permission overrides in guild ({guild_id}).\nwill only use the first 10 permission overrides.".format(
name=self.application_commands[new_cmd_perm["id"]].name,
name=self._application_commands[new_cmd_perm["id"]].name,
guild_id=guild_id,
)
)
Expand Down Expand Up @@ -375,7 +426,7 @@ async def process_application_commands(self, interaction: Interaction) -> None:
return

try:
command = self.application_commands[interaction.data["id"]]
command = self._application_commands[interaction.data["id"]]
except KeyError:
self.dispatch("unknown_command", interaction)
else:
Expand Down

0 comments on commit 4b746ef

Please sign in to comment.