mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-01 15:50:02 +00:00
[commands] Always respect guild IDs passed to cog adding and removal
Fixes #7657
This commit is contained in:
parent
d921a03911
commit
d68f2db7cb
@ -605,7 +605,7 @@ class BotBase(GroupMixin):
|
|||||||
if isinstance(cog, app_commands.Group):
|
if isinstance(cog, app_commands.Group):
|
||||||
self.__tree.add_command(cog, override=override, guild=guild, guilds=guilds)
|
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
|
self.__cogs[cog_name] = cog
|
||||||
|
|
||||||
def get_cog(self, name: str, /) -> Optional[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:
|
if help_command and help_command.cog is cog:
|
||||||
help_command.cog = None
|
help_command.cog = None
|
||||||
|
|
||||||
|
guild_ids = _retrieve_guild_ids(cog, guild, guilds)
|
||||||
if isinstance(cog, app_commands.Group):
|
if isinstance(cog, app_commands.Group):
|
||||||
guild_ids = _retrieve_guild_ids(cog, guild, guilds)
|
|
||||||
if guild_ids is None:
|
if guild_ids is None:
|
||||||
self.__tree.remove_command(name)
|
self.__tree.remove_command(name)
|
||||||
else:
|
else:
|
||||||
for guild_id in guild_ids:
|
for guild_id in guild_ids:
|
||||||
self.__tree.remove_command(name, guild=discord.Object(guild_id))
|
self.__tree.remove_command(name, guild=discord.Object(guild_id))
|
||||||
|
|
||||||
cog._eject(self)
|
cog._eject(self, guild_ids=guild_ids)
|
||||||
|
|
||||||
return cog
|
return cog
|
||||||
|
|
||||||
|
@ -27,12 +27,13 @@ import inspect
|
|||||||
import discord
|
import discord
|
||||||
from discord import app_commands
|
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
|
from ._types import _BaseCommand
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from typing_extensions import Self, TypeGuard
|
from typing_extensions import Self
|
||||||
|
from discord.abc import Snowflake
|
||||||
|
|
||||||
from .bot import BotBase
|
from .bot import BotBase
|
||||||
from .context import Context
|
from .context import Context
|
||||||
@ -463,7 +464,7 @@ class Cog(metaclass=CogMeta):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _inject(self, bot: BotBase) -> Self:
|
def _inject(self, bot: BotBase, override: bool, guild: Optional[Snowflake], guilds: List[Snowflake]) -> Self:
|
||||||
cls = self.__class__
|
cls = self.__class__
|
||||||
|
|
||||||
# realistically, the only thing that can cause loading errors
|
# 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__:
|
if not cls.__cog_is_app_commands_group__:
|
||||||
for command in self.__cog_app_commands__:
|
for command in self.__cog_app_commands__:
|
||||||
# This is already atomic
|
# This is already atomic
|
||||||
bot.tree.add_command(command)
|
bot.tree.add_command(command, override=override, guild=guild, guilds=guilds)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def _eject(self, bot: BotBase) -> None:
|
def _eject(self, bot: BotBase, guild_ids: Optional[Iterable[int]]) -> None:
|
||||||
cls = self.__class__
|
cls = self.__class__
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -514,7 +515,7 @@ class Cog(metaclass=CogMeta):
|
|||||||
|
|
||||||
if not cls.__cog_is_app_commands_group__:
|
if not cls.__cog_is_app_commands_group__:
|
||||||
for command in self.__cog_app_commands__:
|
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:
|
if guild_ids is None:
|
||||||
bot.tree.remove_command(command.name)
|
bot.tree.remove_command(command.name)
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user