Skip to content

Commit

Permalink
Fix required parameters validation error (#1589)
Browse files Browse the repository at this point in the history
  • Loading branch information
Middledot authored Aug 24, 2022
1 parent bb46862 commit 3b1a281
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
3 changes: 3 additions & 0 deletions discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ 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 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
7 changes: 2 additions & 5 deletions discord/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,9 @@ 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)
and command.guild_ids is None
and len(new_cls.__cog_guild_ids__) != 0
):
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)
parent = command.parent
Expand Down
34 changes: 25 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 @@ -186,6 +186,7 @@ def __init__(self, func: Callable, **kwargs) -> None:
buckets = cooldown
else:
raise TypeError("Cooldown must be a an instance of CooldownMapping or None.")

self._buckets: CooldownMapping = buckets

max_concurrency = getattr(func, "__commands_max_concurrency__", kwargs.get("max_concurrency"))
Expand Down Expand Up @@ -646,13 +647,7 @@ def __init__(self, func: Callable, *args, **kwargs) -> None:

self.attached_to_group: bool = False

self.cog = None

params = self._get_signature_parameters()
if kwop := kwargs.get("options", None):
self.options: List[Option] = self._match_option_param_names(params, kwop)
else:
self.options: List[Option] = self._parse_options(params)
self.options: List[Option] = kwargs.get("options", [])

try:
checks = func.__commands_checks__
Expand All @@ -665,13 +660,21 @@ def __init__(self, func: Callable, *args, **kwargs) -> None:
self._before_invoke = None
self._after_invoke = None

self._cog = MISSING

def _validate_parameters(self):
params = self._get_signature_parameters()
if kwop := self.options:
self.options: List[Option] = self._match_option_param_names(params, kwop)
else:
self.options: List[Option] = self._parse_options(params)

def _check_required_params(self, params):
params = iter(params.items())
required_params = (
["self", "context"]
if self.attached_to_group
or self.cog
or len(self.callback.__qualname__.split(".")) > 1
else ["context"]
)
for p in required_params:
Expand Down Expand Up @@ -764,6 +767,15 @@ def _is_typing_union(self, annotation):
def _is_typing_optional(self, annotation):
return self._is_typing_union(annotation) and type(None) in annotation.__args__ # type: ignore

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

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

@property
def is_subcommand(self) -> bool:
return self.parent is not None
Expand Down Expand Up @@ -956,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 3b1a281

Please sign in to comment.