37
37
Coroutine ,
38
38
List ,
39
39
Optional ,
40
+ Type ,
40
41
TypeVar ,
41
42
Union ,
42
43
)
@@ -86,19 +87,23 @@ class ApplicationCommandMixin:
86
87
def __init__ (self , * args , ** kwargs ) -> None :
87
88
super ().__init__ (* args , ** kwargs )
88
89
self ._pending_application_commands = []
89
- self .application_commands = {}
90
+ self ._application_commands = {}
90
91
91
92
@property
92
93
def pending_application_commands (self ):
93
94
return self ._pending_application_commands
94
95
95
96
@property
96
97
def commands (self ) -> List [Union [ApplicationCommand , Any ]]:
97
- commands = list ( self .application_commands . values ())
98
+ commands = self .application_commands
98
99
if self ._supports_prefixed_commands :
99
100
commands += self .prefixed_commands
100
101
return commands
101
102
103
+ @property
104
+ def application_commands (self ) -> List [ApplicationCommand ]:
105
+ return list (self ._application_commands .values ())
106
+
102
107
def add_application_command (self , command : ApplicationCommand ) -> None :
103
108
"""Adds a :class:`.ApplicationCommand` into the internal list of commands.
104
109
@@ -112,7 +117,6 @@ def add_application_command(self, command: ApplicationCommand) -> None:
112
117
command: :class:`.ApplicationCommand`
113
118
The command to add.
114
119
"""
115
-
116
120
if self .debug_guilds and command .guild_ids is None :
117
121
command .guild_ids = self .debug_guilds
118
122
self ._pending_application_commands .append (command )
@@ -136,7 +140,54 @@ def remove_application_command(
136
140
The command that was removed. If the name is not valid then
137
141
``None`` is returned instead.
138
142
"""
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
140
191
141
192
async def sync_commands (self ) -> None :
142
193
"""|coro|
@@ -199,7 +250,7 @@ async def register_commands(self) -> None:
199
250
type = i ["type" ],
200
251
)
201
252
cmd .id = i ["id" ]
202
- self .application_commands [cmd .id ] = cmd
253
+ self ._application_commands [cmd .id ] = cmd
203
254
204
255
# Permissions (Roles will be converted to IDs just before Upsert for Global Commands)
205
256
global_permissions .append ({"id" : i ["id" ], "permissions" : cmd .permissions })
@@ -234,7 +285,7 @@ async def register_commands(self) -> None:
234
285
for i in cmds :
235
286
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 )
236
287
cmd .id = i ["id" ]
237
- self .application_commands [cmd .id ] = cmd
288
+ self ._application_commands [cmd .id ] = cmd
238
289
239
290
# Permissions
240
291
permissions = [
@@ -325,7 +376,7 @@ async def register_commands(self) -> None:
325
376
if len (new_cmd_perm ["permissions" ]) > 10 :
326
377
print (
327
378
"Command '{name}' has more than 10 permission overrides in guild ({guild_id}).\n will 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 ,
329
380
guild_id = guild_id ,
330
381
)
331
382
)
@@ -375,7 +426,7 @@ async def process_application_commands(self, interaction: Interaction) -> None:
375
426
return
376
427
377
428
try :
378
- command = self .application_commands [interaction .data ["id" ]]
429
+ command = self ._application_commands [interaction .data ["id" ]]
379
430
except KeyError :
380
431
self .dispatch ("unknown_command" , interaction )
381
432
else :
0 commit comments