From 9554e0e80a8d9e1f25e2cc28c2f5486c6eb07a20 Mon Sep 17 00:00:00 2001 From: Middledot Date: Sat, 10 Sep 2022 21:47:39 -0400 Subject: [PATCH 1/2] fix(ext.bridge): groups fail --- discord/cog.py | 18 +++++------------- discord/ext/bridge/core.py | 9 +++++++-- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/discord/cog.py b/discord/cog.py index 625818af04..38a24ab5f7 100644 --- a/discord/cog.py +++ b/discord/cog.py @@ -178,12 +178,9 @@ def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta: if elem in listeners: del listeners[elem] - try: - if getattr(value, "parent") is not None and isinstance(value, ApplicationCommand): - # Skip commands if they are a part of a group - continue - except AttributeError: - pass + if getattr(value, "parent", None) and isinstance(value, ApplicationCommand): + # Skip commands if they are a part of a group + continue is_static_method = isinstance(value, staticmethod) if is_static_method: @@ -195,10 +192,8 @@ def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta: raise TypeError(no_bot_cog.format(base, elem)) commands[elem] = value - try: - # a test to see if this value is a BridgeCommand - getattr(value, "add_to") - + # a test to see if this value is a BridgeCommand + if hasattr(value, "add_to") and not getattr(value, "parent", None): if is_static_method: raise TypeError(f"Command in method {base}.{elem!r} must not be staticmethod.") if elem.startswith(("cog_", "bot_")): @@ -206,9 +201,6 @@ def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta: commands[f"ext_{elem}"] = value.ext_variant commands[f"application_{elem}"] = value.slash_variant - except AttributeError: - # we are confident that the value is not a Bridge Command - pass if inspect.iscoroutinefunction(value): try: diff --git a/discord/ext/bridge/core.py b/discord/ext/bridge/core.py index d0855dbb04..6a2d8ef140 100644 --- a/discord/ext/bridge/core.py +++ b/discord/ext/bridge/core.py @@ -87,6 +87,7 @@ class BridgeSlashGroup(SlashCommandGroup): def __init__(self, callback, *args, **kwargs): super().__init__(*args, **kwargs) self.callback = callback + self.__original_kwargs__["callback"] = callback self.__command = None async def _invoke(self, ctx: BridgeApplicationContext) -> None: @@ -107,6 +108,7 @@ class BridgeExtGroup(BridgeExtCommand, Group): """A subclass of :class:`.ext.commands.Group` that is used for bridge commands.""" pass + class BridgeCommand: """Compatibility class between prefixed-based commands and slash commands. @@ -122,10 +124,13 @@ class BridgeCommand: callback: Callable[[:class:`.BridgeContext`, ...], Awaitable[Any]] The callback to invoke when the command is executed. The first argument will be a :class:`BridgeContext`, and any additional arguments will be passed to the callback. This callback must be a coroutine. + parent: Optional[:class:`.BridgeCommandGroup`]: + Parent of the BridgeCommand. kwargs: Optional[Dict[:class:`str`, Any]] Keyword arguments that are directly passed to the respective command constructors. (:class:`.SlashCommand` and :class:`.ext.commands.Command`) """ def __init__(self, callback, **kwargs): + self.parent = kwargs.pop("parent") self.slash_variant: BridgeSlashCommand = kwargs.pop("slash_variant", None) or BridgeSlashCommand(callback, **kwargs) self.ext_variant: BridgeExtCommand = kwargs.pop("ext_variant", None) or BridgeExtCommand(callback, **kwargs) @@ -295,8 +300,8 @@ def command(self, *args, **kwargs): """ def wrap(callback): slash = self.slash_variant.command(*args, **filter_params(kwargs, brief="description"), cls=BridgeSlashCommand)(callback) - ext = self.ext_variant.command(*args, **filter_params(kwargs, description="brief"), cls=BridgeExtGroup)(callback) - command = BridgeCommand(callback, slash_variant=slash, ext_variant=ext) + ext = self.ext_variant.command(*args, **filter_params(kwargs, description="brief"), cls=BridgeExtCommand)(callback) + command = BridgeCommand(callback, parent=self, slash_variant=slash, ext_variant=ext) self.subcommands.append(command) return command From 472dcaa5eccda0bb9e87d4723d1e51969db01c83 Mon Sep 17 00:00:00 2001 From: Middledot Date: Sat, 10 Sep 2022 21:58:42 -0400 Subject: [PATCH 2/2] fix: optional attr --- discord/ext/bridge/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/ext/bridge/core.py b/discord/ext/bridge/core.py index 6a2d8ef140..cac95e2a76 100644 --- a/discord/ext/bridge/core.py +++ b/discord/ext/bridge/core.py @@ -130,7 +130,7 @@ class BridgeCommand: Keyword arguments that are directly passed to the respective command constructors. (:class:`.SlashCommand` and :class:`.ext.commands.Command`) """ def __init__(self, callback, **kwargs): - self.parent = kwargs.pop("parent") + self.parent = kwargs.pop("parent", None) self.slash_variant: BridgeSlashCommand = kwargs.pop("slash_variant", None) or BridgeSlashCommand(callback, **kwargs) self.ext_variant: BridgeExtCommand = kwargs.pop("ext_variant", None) or BridgeExtCommand(callback, **kwargs)