Skip to content

Commit 4b746ef

Browse files
authored
Merge pull request #395 from Dorukyum/get_application_command
Implement `bot.get_application_command`
2 parents 9eb30ae + 0359234 commit 4b746ef

File tree

1 file changed

+59
-8
lines changed

1 file changed

+59
-8
lines changed

discord/bot.py

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
Coroutine,
3838
List,
3939
Optional,
40+
Type,
4041
TypeVar,
4142
Union,
4243
)
@@ -86,19 +87,23 @@ class ApplicationCommandMixin:
8687
def __init__(self, *args, **kwargs) -> None:
8788
super().__init__(*args, **kwargs)
8889
self._pending_application_commands = []
89-
self.application_commands = {}
90+
self._application_commands = {}
9091

9192
@property
9293
def pending_application_commands(self):
9394
return self._pending_application_commands
9495

9596
@property
9697
def commands(self) -> List[Union[ApplicationCommand, Any]]:
97-
commands = list(self.application_commands.values())
98+
commands = self.application_commands
9899
if self._supports_prefixed_commands:
99100
commands += self.prefixed_commands
100101
return commands
101102

103+
@property
104+
def application_commands(self) -> List[ApplicationCommand]:
105+
return list(self._application_commands.values())
106+
102107
def add_application_command(self, command: ApplicationCommand) -> None:
103108
"""Adds a :class:`.ApplicationCommand` into the internal list of commands.
104109
@@ -112,7 +117,6 @@ def add_application_command(self, command: ApplicationCommand) -> None:
112117
command: :class:`.ApplicationCommand`
113118
The command to add.
114119
"""
115-
116120
if self.debug_guilds and command.guild_ids is None:
117121
command.guild_ids = self.debug_guilds
118122
self._pending_application_commands.append(command)
@@ -136,7 +140,54 @@ def remove_application_command(
136140
The command that was removed. If the name is not valid then
137141
``None`` is returned instead.
138142
"""
139-
return self.application_commands.pop(command.id)
143+
return self._application_commands.pop(command.id)
144+
145+
@property
146+
def get_command(self):
147+
"""Shortcut for :meth:`.get_application_command`.
148+
149+
.. note::
150+
Overridden in :class:`ext.commands.Bot`.
151+
152+
.. versionadded:: 2.0
153+
"""
154+
# TODO: Do something like we did in self.commands for this
155+
return self.get_application_command
156+
157+
def get_application_command(
158+
self,
159+
name: str,
160+
guild_ids: Optional[List[int]] = None,
161+
type: Type[ApplicationCommand] = SlashCommand,
162+
) -> Optional[ApplicationCommand]:
163+
"""Get a :class:`.ApplicationCommand` from the internal list
164+
of commands.
165+
166+
.. versionadded:: 2.0
167+
168+
Parameters
169+
-----------
170+
name: :class:`str`
171+
The name of the command to get.
172+
guild_ids: List[:class:`int`]
173+
The guild ids associated to the command to get.
174+
type: Type[:class:`.ApplicationCommand`]
175+
The type of the command to get. Defaults to :class:`.SlashCommand`.
176+
177+
Returns
178+
--------
179+
Optional[:class:`.ApplicationCommand`]
180+
The command that was requested. If not found, returns ``None``.
181+
"""
182+
183+
for command in self._application_commands.values():
184+
if (
185+
command.name == name
186+
and isinstance(command, type)
187+
):
188+
if guild_ids is not None and command.guild_ids != guild_ids:
189+
return
190+
return command
140191

141192
async def sync_commands(self) -> None:
142193
"""|coro|
@@ -199,7 +250,7 @@ async def register_commands(self) -> None:
199250
type=i["type"],
200251
)
201252
cmd.id = i["id"]
202-
self.application_commands[cmd.id] = cmd
253+
self._application_commands[cmd.id] = cmd
203254

204255
# Permissions (Roles will be converted to IDs just before Upsert for Global Commands)
205256
global_permissions.append({"id": i["id"], "permissions": cmd.permissions})
@@ -234,7 +285,7 @@ async def register_commands(self) -> None:
234285
for i in cmds:
235286
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)
236287
cmd.id = i["id"]
237-
self.application_commands[cmd.id] = cmd
288+
self._application_commands[cmd.id] = cmd
238289

239290
# Permissions
240291
permissions = [
@@ -325,7 +376,7 @@ async def register_commands(self) -> None:
325376
if len(new_cmd_perm["permissions"]) > 10:
326377
print(
327378
"Command '{name}' has more than 10 permission overrides in guild ({guild_id}).\nwill only use the first 10 permission overrides.".format(
328-
name=self.application_commands[new_cmd_perm["id"]].name,
379+
name=self._application_commands[new_cmd_perm["id"]].name,
329380
guild_id=guild_id,
330381
)
331382
)
@@ -375,7 +426,7 @@ async def process_application_commands(self, interaction: Interaction) -> None:
375426
return
376427

377428
try:
378-
command = self.application_commands[interaction.data["id"]]
429+
command = self._application_commands[interaction.data["id"]]
379430
except KeyError:
380431
self.dispatch("unknown_command", interaction)
381432
else:

0 commit comments

Comments
 (0)