Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext.bridge): fix bridge_commands attribute #1802

Merged
merged 2 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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