diff --git a/discord/ext/commands/cog.py b/discord/ext/commands/cog.py index 5f29297df..1fdf40111 100644 --- a/discord/ext/commands/cog.py +++ b/discord/ext/commands/cog.py @@ -289,17 +289,19 @@ class Cog(metaclass=CogMeta): # Update our parent's reference to our self parent.remove_command(command.name) # type: ignore parent.add_command(command) # type: ignore - elif self.__cog_app_commands_group__: - if hasattr(command, '__commands_is_hybrid__') and command.parent is None: - parent = self.__cog_app_commands_group__ - app_command: Optional[Union[app_commands.Group, app_commands.Command[Self, ..., Any]]] = getattr( - command, 'app_command', None - ) - if app_command: - app_command = app_command._copy_with(parent=parent, binding=self) + + if hasattr(command, '__commands_is_hybrid__') and parent is None: + app_command: Optional[Union[app_commands.Group, app_commands.Command[Self, ..., Any]]] = getattr( + command, 'app_command', None + ) + if app_command: + group_parent = self.__cog_app_commands_group__ + app_command = app_command._copy_with(parent=group_parent, binding=self) + # The type checker does not see the app_command attribute even though it exists + command.app_command = app_command # type: ignore + + if self.__cog_app_commands_group__: children.append(app_command) - # The type checker does not see the app_command attribute even though it exists - command.app_command = app_command # type: ignore for command in cls.__cog_app_commands__: copy = command._copy_with(parent=self.__cog_app_commands_group__, binding=self) diff --git a/discord/ext/commands/hybrid.py b/discord/ext/commands/hybrid.py index 555a3ef39..8ed9e0816 100644 --- a/discord/ext/commands/hybrid.py +++ b/discord/ext/commands/hybrid.py @@ -565,6 +565,19 @@ class HybridGroup(Group[CogT, P, T]): copy.fallback = self.fallback return copy + def _update_copy(self, kwargs: Dict[str, Any]) -> Self: + copy = super()._update_copy(kwargs) + # This is only really called inside CogMeta + # I want to ensure that the children of the app command are copied + # For example, if someone defines an app command using the app_command property + # while inside the cog. This copy is not propagated in normal means. + # For some reason doing this copy above will lead to duplicates. + if copy.app_command and self.app_command: + # This is a very lazy copy because the CogMeta will properly copy it + # with bindings and all later + copy.app_command._children = self.app_command._children.copy() + return copy + def autocomplete( self, name: str ) -> Callable[[AutocompleteCallback[CogT, ChoiceT]], AutocompleteCallback[CogT, ChoiceT]]: