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): bridge groups failing #1633

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions discord/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -195,20 +192,15 @@ 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_")):
raise TypeError(no_bot_cog.format(base, elem))

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:
Expand Down
9 changes: 7 additions & 2 deletions discord/ext/bridge/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.

Expand All @@ -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", 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)

Expand Down Expand Up @@ -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

Expand Down