Skip to content

Commit

Permalink
refactor: remove is_in_cog
Browse files Browse the repository at this point in the history
  • Loading branch information
Middledot committed Aug 24, 2022
1 parent d88b417 commit 379247a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def add_application_command(self, command: ApplicationCommand) -> None:
if isinstance(command, SlashCommand) and command.is_subcommand:
raise TypeError("The provided command is a sub-command of group")

if not command.is_in_cog:
command.is_in_cog = False
if command.cog is MISSING:
command.cog = None

if self._bot.debug_guilds and command.guild_ids is None:
command.guild_ids = self._bot.debug_guilds
Expand Down
6 changes: 2 additions & 4 deletions discord/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,8 @@ def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:

# Update the Command instances dynamically as well
for command in new_cls.__cog_commands__:
if isinstance(command, ApplicationCommand):
command.is_in_cog = True
if isinstance(command, ApplicationCommand) and not command.guild_ids and new_cls.__cog_guild_ids__:
command.guild_ids = new_cls.__cog_guild_ids__
if isinstance(command, ApplicationCommand) and not command.guild_ids and new_cls.__cog_guild_ids__:
command.guild_ids = new_cls.__cog_guild_ids__

if not isinstance(command, SlashCommandGroup):
setattr(new_cls, command.callback.__name__, command)
Expand Down
22 changes: 13 additions & 9 deletions discord/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
from ..role import Role
from ..threads import Thread
from ..user import User
from ..utils import async_all, find, utcnow, maybe_coroutine
from ..utils import async_all, find, utcnow, maybe_coroutine, MISSING
from .context import ApplicationContext, AutocompleteContext
from .options import Option, OptionChoice

Expand Down Expand Up @@ -173,6 +173,7 @@ class _BaseCommand:

class ApplicationCommand(_BaseCommand, Generic[CogT, P, T]):
__original_kwargs__: Dict[str, Any]
cog = None

def __init__(self, func: Callable, **kwargs) -> None:
from ..ext.commands.cooldowns import BucketType, CooldownMapping, MaxConcurrency
Expand Down Expand Up @@ -203,7 +204,6 @@ def __init__(self, func: Callable, **kwargs) -> None:
except AttributeError:
checks = kwargs.get("checks", [])

self.cog = None
self.checks = checks
self.id: Optional[int] = kwargs.get("id")
self.guild_ids: Optional[List[int]] = kwargs.get("guild_ids", None)
Expand Down Expand Up @@ -660,7 +660,7 @@ def __init__(self, func: Callable, *args, **kwargs) -> None:
self._before_invoke = None
self._after_invoke = None

self._is_in_cog = None
self._cog = MISSING

def _validate_parameters(self):
params = self._get_signature_parameters()
Expand All @@ -674,7 +674,7 @@ def _check_required_params(self, params):
required_params = (
["self", "context"]
if self.attached_to_group
or self.is_in_cog
or self.cog
else ["context"]
)
for p in required_params:
Expand Down Expand Up @@ -768,12 +768,12 @@ def _is_typing_optional(self, annotation):
return self._is_typing_union(annotation) and type(None) in annotation.__args__ # type: ignore

@property
def is_in_cog(self):
return self._is_in_cog
def cog(self):
return self._cog

@is_in_cog.setter
def is_in_cog(self, val):
self._is_in_cog = val
@cog.setter
def cog(self, val):
self._cog = val
self._validate_parameters()

@property
Expand Down Expand Up @@ -968,6 +968,10 @@ def _update_copy(self, kwargs: Dict[str, Any]):
else:
return self.copy()

def _set_cog(self, cog):
super()._set_cog(cog)
self._validate_parameters()


class SlashCommandGroup(ApplicationCommand):
r"""A class that implements the protocol for a slash command group.
Expand Down

0 comments on commit 379247a

Please sign in to comment.