Skip to content

Commit 36d5a7f

Browse files
fix(ext.bridge): fix bridge_commands attribute (#1802)
* fix(ext.bridge): fix bridge_commands attribute * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b20f134 commit 36d5a7f

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

discord/cog.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
199199

200200
commands[f"ext_{elem}"] = value.ext_variant
201201
commands[f"app_{elem}"] = value.slash_variant
202+
commands[elem] = value
202203
for cmd in getattr(value, "subcommands", []):
203204
commands[
204205
f"ext_{cmd.ext_variant.qualified_name}"
@@ -229,9 +230,13 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
229230

230231
# Either update the command with the cog provided defaults or copy it.
231232
# r.e type ignore, type-checker complains about overriding a ClassVar
232-
new_cls.__cog_commands__ = tuple(c._update_copy(cmd_attrs) for c in new_cls.__cog_commands__) # type: ignore
233+
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
233234

234-
name_filter = lambda c: "app" if isinstance(c, ApplicationCommand) else "ext"
235+
name_filter = (
236+
lambda c: "app"
237+
if isinstance(c, ApplicationCommand)
238+
else ("bridge" if not hasattr(c, "add_to") else "ext")
239+
)
235240

236241
lookup = {
237242
f"{name_filter(cmd)}_{cmd.qualified_name}": cmd
@@ -247,7 +252,9 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
247252
):
248253
command.guild_ids = new_cls.__cog_guild_ids__
249254

250-
if not isinstance(command, SlashCommandGroup):
255+
if not isinstance(command, SlashCommandGroup) and not hasattr(
256+
command, "add_to"
257+
):
251258
# ignore bridge commands
252259
cmd = getattr(new_cls, command.callback.__name__, None)
253260
if hasattr(cmd, "add_to"):
@@ -534,6 +541,10 @@ def _inject(self: CogT, bot) -> CogT:
534541
# we've added so far for some form of atomic loading.
535542

536543
for index, command in enumerate(self.__cog_commands__):
544+
if hasattr(command, "add_to"):
545+
bot._bridge_commands.append(command)
546+
continue
547+
537548
command._set_cog(self)
538549

539550
if isinstance(command, ApplicationCommand):

discord/ext/bridge/bot.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class BotBase(ABC):
4444
def bridge_commands(self) -> list[BridgeCommand | BridgeCommandGroup]:
4545
"""Returns all of the bot's bridge commands."""
4646

47-
if cmds := getattr(self, "_bridge_commands", []):
47+
if not (cmds := getattr(self, "_bridge_commands", None)):
4848
self._bridge_commands = cmds = []
4949

5050
return cmds
@@ -73,6 +73,10 @@ def add_bridge_command(self, command: BridgeCommand):
7373
"""
7474
# Ignore the type hinting error here. All subclasses of BotBase pass the type checks.
7575
command.add_to(self) # type: ignore
76+
77+
if getattr(self, "_bridge_commands", None) is None:
78+
self._bridge_commands = []
79+
7680
self._bridge_commands.append(command)
7781

7882
def bridge_command(self, **kwargs):

discord/ext/bridge/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ def description_localizations(self) -> dict[str, str]:
191191
def description_localizations(self, value):
192192
self.slash_variant.description_localizations = value
193193

194+
@property
195+
def qualified_name(self) -> str:
196+
return self.slash_variant.qualified_name
197+
194198
def add_to(self, bot: ExtBot) -> None:
195199
"""Adds the command to a bot. This method is inherited by :class:`.BridgeCommandGroup`.
196200

0 commit comments

Comments
 (0)