From d68f2db7cbecb83b331fb2c131375679b14bdb29 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sat, 12 Mar 2022 06:58:55 -0500 Subject: [PATCH] [commands] Always respect guild IDs passed to cog adding and removal Fixes #7657 --- discord/ext/commands/bot.py | 6 +++--- discord/ext/commands/cog.py | 13 +++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index 231583cc9..b8b9b7cc8 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -605,7 +605,7 @@ class BotBase(GroupMixin): if isinstance(cog, app_commands.Group): self.__tree.add_command(cog, override=override, guild=guild, guilds=guilds) - cog = cog._inject(self) + cog = cog._inject(self, override=override, guild=guild, guilds=guilds) self.__cogs[cog_name] = cog def get_cog(self, name: str, /) -> Optional[Cog]: @@ -681,15 +681,15 @@ class BotBase(GroupMixin): if help_command and help_command.cog is cog: help_command.cog = None + guild_ids = _retrieve_guild_ids(cog, guild, guilds) if isinstance(cog, app_commands.Group): - guild_ids = _retrieve_guild_ids(cog, guild, guilds) if guild_ids is None: self.__tree.remove_command(name) else: for guild_id in guild_ids: self.__tree.remove_command(name, guild=discord.Object(guild_id)) - cog._eject(self) + cog._eject(self, guild_ids=guild_ids) return cog diff --git a/discord/ext/commands/cog.py b/discord/ext/commands/cog.py index 25cbebcd4..3af426005 100644 --- a/discord/ext/commands/cog.py +++ b/discord/ext/commands/cog.py @@ -27,12 +27,13 @@ import inspect import discord from discord import app_commands -from typing import Any, Callable, Dict, Generator, List, Optional, TYPE_CHECKING, Tuple, TypeVar, Union, Type +from typing import Any, Callable, Dict, Generator, Iterable, List, Optional, TYPE_CHECKING, Tuple, TypeVar, Union from ._types import _BaseCommand if TYPE_CHECKING: - from typing_extensions import Self, TypeGuard + from typing_extensions import Self + from discord.abc import Snowflake from .bot import BotBase from .context import Context @@ -463,7 +464,7 @@ class Cog(metaclass=CogMeta): """ pass - def _inject(self, bot: BotBase) -> Self: + def _inject(self, bot: BotBase, override: bool, guild: Optional[Snowflake], guilds: List[Snowflake]) -> Self: cls = self.__class__ # realistically, the only thing that can cause loading errors @@ -500,11 +501,11 @@ class Cog(metaclass=CogMeta): if not cls.__cog_is_app_commands_group__: for command in self.__cog_app_commands__: # This is already atomic - bot.tree.add_command(command) + bot.tree.add_command(command, override=override, guild=guild, guilds=guilds) return self - def _eject(self, bot: BotBase) -> None: + def _eject(self, bot: BotBase, guild_ids: Optional[Iterable[int]]) -> None: cls = self.__class__ try: @@ -514,7 +515,7 @@ class Cog(metaclass=CogMeta): if not cls.__cog_is_app_commands_group__: for command in self.__cog_app_commands__: - guild_ids = command._guild_ids + guild_ids = guild_ids or command._guild_ids if guild_ids is None: bot.tree.remove_command(command.name) else: