Skip to content

Commit e127333

Browse files
committed
♻️ Refactor weird checks and don't redefine filter every time
1 parent 97a8303 commit e127333

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

discord/cog.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ def _is_submodule(parent: str, child: str) -> bool:
7575
return parent == child or child.startswith(f"{parent}.")
7676

7777

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+
7890
class CogMeta(type):
7991
"""A metaclass for defining a cog.
8092
@@ -208,8 +220,7 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
208220
raise TypeError(no_bot_cog.format(base, elem))
209221
commands[elem] = value
210222

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:
213224
if is_static_method:
214225
raise TypeError(
215226
f"Command in method {base}.{elem!r} must not be"
@@ -251,16 +262,10 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
251262

252263
# Either update the command with the cog provided defaults or copy it.
253264
# 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
261266

262267
lookup = {
263-
f"{name_filter(cmd)}_{cmd.qualified_name}": cmd
268+
f"{_name_filter(cmd)}_{cmd.qualified_name}": cmd
264269
for cmd in new_cls.__cog_commands__
265270
}
266271

@@ -273,15 +278,15 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
273278
):
274279
command.guild_ids = new_cls.__cog_guild_ids__
275280

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
278283
):
279284
# ignore bridge commands
280285
cmd = getattr(new_cls, command.callback.__name__, None)
281-
if hasattr(cmd, "add_to"):
286+
if _is_bridge_command(cmd):
282287
setattr(
283288
cmd,
284-
f"{name_filter(command).replace('app', 'slash')}_variant",
289+
f"{_name_filter(command).replace('app', 'slash')}_variant",
285290
command,
286291
)
287292
else:
@@ -290,7 +295,7 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
290295
parent = command.parent
291296
if parent is not None:
292297
# 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
294299

295300
# Update our parent's reference to our self
296301
parent.remove_command(command.name) # type: ignore
@@ -568,7 +573,7 @@ def _inject(self: CogT, bot) -> CogT:
568573
# we've added so far for some form of atomic loading.
569574

570575
for index, command in enumerate(self.__cog_commands__):
571-
if hasattr(command, "add_to"):
576+
if _is_bridge_command(command):
572577
bot.bridge_commands.append(command)
573578
continue
574579

@@ -613,7 +618,7 @@ def _eject(self, bot) -> None:
613618

614619
try:
615620
for command in self.__cog_commands__:
616-
if hasattr(command, "add_to"):
621+
if _is_bridge_command(command):
617622
bot.bridge_commands.remove(command)
618623
continue
619624
elif isinstance(command, ApplicationCommand):

discord/ext/bridge/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ class BridgeCommand:
184184
The prefix-based version of this bridge command.
185185
"""
186186

187+
__bridge__: bool = True
188+
187189
__special_attrs__ = ["slash_variant", "ext_variant", "parent"]
188190

189191
def __init__(self, callback, **kwargs):

0 commit comments

Comments
 (0)