Skip to content

Commit

Permalink
fix(ext.bridge): fix bridge_commands attribute (#1802)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Middledot and pre-commit-ci[bot] committed Nov 27, 2022
1 parent b20f134 commit 36d5a7f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
17 changes: 14 additions & 3 deletions discord/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:

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

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

name_filter = lambda c: "app" if isinstance(c, ApplicationCommand) else "ext"
name_filter = (
lambda c: "app"
if isinstance(c, ApplicationCommand)
else ("bridge" if not hasattr(c, "add_to") else "ext")
)

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

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

for index, command in enumerate(self.__cog_commands__):
if hasattr(command, "add_to"):
bot._bridge_commands.append(command)
continue

command._set_cog(self)

if isinstance(command, ApplicationCommand):
Expand Down
6 changes: 5 additions & 1 deletion discord/ext/bridge/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BotBase(ABC):
def bridge_commands(self) -> list[BridgeCommand | BridgeCommandGroup]:
"""Returns all of the bot's bridge commands."""

if cmds := getattr(self, "_bridge_commands", []):
if not (cmds := getattr(self, "_bridge_commands", None)):
self._bridge_commands = cmds = []

return cmds
Expand Down Expand Up @@ -73,6 +73,10 @@ def add_bridge_command(self, command: BridgeCommand):
"""
# Ignore the type hinting error here. All subclasses of BotBase pass the type checks.
command.add_to(self) # type: ignore

if getattr(self, "_bridge_commands", None) is None:
self._bridge_commands = []

self._bridge_commands.append(command)

def bridge_command(self, **kwargs):
Expand Down
4 changes: 4 additions & 0 deletions discord/ext/bridge/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ def description_localizations(self) -> dict[str, str]:
def description_localizations(self, value):
self.slash_variant.description_localizations = value

@property
def qualified_name(self) -> str:
return self.slash_variant.qualified_name

def add_to(self, bot: ExtBot) -> None:
"""Adds the command to a bot. This method is inherited by :class:`.BridgeCommandGroup`.
Expand Down

0 comments on commit 36d5a7f

Please sign in to comment.