@@ -75,6 +75,18 @@ def _is_submodule(parent: str, child: str) -> bool:
75
75
return parent == child or child .startswith (f"{ parent } ." )
76
76
77
77
78
+ def _is_bridge_command (command : Any ) -> TypeGuard [BridgeCommand ]:
79
+ return getattr (command , "__bridge__" , False )
80
+
81
+
82
+ def _name_filter (c : Any ) -> str :
83
+ return (
84
+ "app"
85
+ if isinstance (c , ApplicationCommand )
86
+ else ("bridge" if not _is_bridge_command (c ) else "ext" )
87
+ )
88
+
89
+
78
90
class CogMeta (type ):
79
91
"""A metaclass for defining a cog.
80
92
@@ -208,8 +220,7 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
208
220
raise TypeError (no_bot_cog .format (base , elem ))
209
221
commands [elem ] = value
210
222
211
- # a test to see if this value is a BridgeCommand
212
- if hasattr (value , "add_to" ) and not getattr (value , "parent" , None ):
223
+ if _is_bridge_command (value ) and not value .parent :
213
224
if is_static_method :
214
225
raise TypeError (
215
226
f"Command in method { base } .{ elem !r} must not be"
@@ -251,16 +262,10 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
251
262
252
263
# Either update the command with the cog provided defaults or copy it.
253
264
# r.e type ignore, type-checker complains about overriding a ClassVar
254
- new_cls .__cog_commands__ = tuple (c ._update_copy (cmd_attrs ) if not hasattr (c , "add_to" ) else c for c in new_cls .__cog_commands__ ) # type: ignore
255
-
256
- name_filter = lambda c : (
257
- "app"
258
- if isinstance (c , ApplicationCommand )
259
- else ("bridge" if not hasattr (c , "add_to" ) else "ext" )
260
- )
265
+ new_cls .__cog_commands__ = tuple (c ._update_copy (cmd_attrs ) if not _is_bridge_command (c ) else c for c in new_cls .__cog_commands__ ) # type: ignore
261
266
262
267
lookup = {
263
- f"{ name_filter (cmd )} _{ cmd .qualified_name } " : cmd
268
+ f"{ _name_filter (cmd )} _{ cmd .qualified_name } " : cmd
264
269
for cmd in new_cls .__cog_commands__
265
270
}
266
271
@@ -273,15 +278,15 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
273
278
):
274
279
command .guild_ids = new_cls .__cog_guild_ids__
275
280
276
- if not isinstance (command , SlashCommandGroup ) and not hasattr (
277
- command , "add_to"
281
+ if not isinstance (command , SlashCommandGroup ) and not _is_bridge_command (
282
+ command
278
283
):
279
284
# ignore bridge commands
280
285
cmd = getattr (new_cls , command .callback .__name__ , None )
281
- if hasattr (cmd , "add_to" ):
286
+ if _is_bridge_command (cmd ):
282
287
setattr (
283
288
cmd ,
284
- f"{ name_filter (command ).replace ('app' , 'slash' )} _variant" ,
289
+ f"{ _name_filter (command ).replace ('app' , 'slash' )} _variant" ,
285
290
command ,
286
291
)
287
292
else :
@@ -290,7 +295,7 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
290
295
parent = command .parent
291
296
if parent is not None :
292
297
# Get the latest parent reference
293
- parent = lookup [f"{ name_filter (command )} _{ parent .qualified_name } " ] # type: ignore
298
+ parent = lookup [f"{ _name_filter (command )} _{ parent .qualified_name } " ] # type: ignore
294
299
295
300
# Update our parent's reference to our self
296
301
parent .remove_command (command .name ) # type: ignore
@@ -568,7 +573,7 @@ def _inject(self: CogT, bot) -> CogT:
568
573
# we've added so far for some form of atomic loading.
569
574
570
575
for index , command in enumerate (self .__cog_commands__ ):
571
- if hasattr (command , "add_to" ):
576
+ if _is_bridge_command (command ):
572
577
bot .bridge_commands .append (command )
573
578
continue
574
579
@@ -613,7 +618,7 @@ def _eject(self, bot) -> None:
613
618
614
619
try :
615
620
for command in self .__cog_commands__ :
616
- if hasattr (command , "add_to" ):
621
+ if _is_bridge_command (command ):
617
622
bot .bridge_commands .remove (command )
618
623
continue
619
624
elif isinstance (command , ApplicationCommand ):
0 commit comments