Skip to content

Commit

Permalink
Fix broken cog attribute (#1662)
Browse files Browse the repository at this point in the history
* fix: broken cog attribute

* chore: improve a cog check

* Fix typehint

* fix: for the fix: for the fix

* fix: for the fix: for the fix: for the fix

* refactor: redundant code

* Fix command.parent in groups

Co-authored-by: BobDotCom <71356958+BobDotCom@users.noreply.github.com>
  • Loading branch information
Middledot and BobDotCom authored Oct 6, 2022
1 parent 303a584 commit 2bc364c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions discord/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ def _inject(self: CogT, bot) -> CogT:
command._set_cog(self)

if isinstance(command, ApplicationCommand):
if isinstance(command, discord.SlashCommandGroup):
for x in command.walk_commands():
x.parent = command
bot.add_application_command(command)

elif command.parent is None:
Expand Down
19 changes: 13 additions & 6 deletions discord/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,6 @@ 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:
Expand Down Expand Up @@ -810,7 +808,7 @@ def _is_typing_optional(self, annotation):

@property
def cog(self):
return self._cog
return getattr(self, "_cog", MISSING)

@cog.setter
def cog(self, val):
Expand Down Expand Up @@ -1116,7 +1114,7 @@ def __init__(

self._before_invoke = None
self._after_invoke = None
self.cog = None
self.cog = MISSING
self.id = None

# Permissions
Expand Down Expand Up @@ -1160,12 +1158,21 @@ def to_dict(self) -> dict:

return as_dict

def add_command(self, command: SlashCommand) -> None:
# check if subcommand has no cog set
# also check if cog is MISSING because it
# might not have been set by the cog yet
if command.cog is MISSING and self.cog is not MISSING:
command.cog = self.cog

self.subcommands.append(command)

def command(
self, cls: type[T] = SlashCommand, **kwargs
) -> Callable[[Callable], SlashCommand]:
def wrap(func) -> T:
command = cls(func, parent=self, **kwargs)
self.subcommands.append(command)
self.add_command(command)
return command

return wrap
Expand Down Expand Up @@ -1262,7 +1269,7 @@ def inner(cls: type[SlashCommandGroup]) -> SlashCommandGroup:
guild_ids=guild_ids,
parent=self,
)
self.subcommands.append(group)
self.add_command(group)
return group

return inner
Expand Down
2 changes: 1 addition & 1 deletion discord/ext/commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def get_bot_mapping(self):
"""Retrieves the bot mapping passed to :meth:`send_bot_help`."""
bot = self.context.bot
mapping = {cog: cog.get_commands() for cog in bot.cogs.values()}
mapping[None] = [c for c in bot.commands if c.cog is None]
mapping[None] = [c for c in bot.commands if not c.cog]
return mapping

@property
Expand Down

0 comments on commit 2bc364c

Please sign in to comment.