Skip to content

Commit 2bc364c

Browse files
MiddledotBobDotCom
andauthored
Fix broken cog attribute (#1662)
* 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 <[email protected]>
1 parent 303a584 commit 2bc364c

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

discord/cog.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ def _inject(self: CogT, bot) -> CogT:
531531
command._set_cog(self)
532532

533533
if isinstance(command, ApplicationCommand):
534+
if isinstance(command, discord.SlashCommandGroup):
535+
for x in command.walk_commands():
536+
x.parent = command
534537
bot.add_application_command(command)
535538

536539
elif command.parent is None:

discord/commands/core.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,6 @@ def __init__(self, func: Callable, *args, **kwargs) -> None:
686686
self._before_invoke = None
687687
self._after_invoke = None
688688

689-
self._cog = MISSING
690-
691689
def _validate_parameters(self):
692690
params = self._get_signature_parameters()
693691
if kwop := self.options:
@@ -810,7 +808,7 @@ def _is_typing_optional(self, annotation):
810808

811809
@property
812810
def cog(self):
813-
return self._cog
811+
return getattr(self, "_cog", MISSING)
814812

815813
@cog.setter
816814
def cog(self, val):
@@ -1116,7 +1114,7 @@ def __init__(
11161114

11171115
self._before_invoke = None
11181116
self._after_invoke = None
1119-
self.cog = None
1117+
self.cog = MISSING
11201118
self.id = None
11211119

11221120
# Permissions
@@ -1160,12 +1158,21 @@ def to_dict(self) -> dict:
11601158

11611159
return as_dict
11621160

1161+
def add_command(self, command: SlashCommand) -> None:
1162+
# check if subcommand has no cog set
1163+
# also check if cog is MISSING because it
1164+
# might not have been set by the cog yet
1165+
if command.cog is MISSING and self.cog is not MISSING:
1166+
command.cog = self.cog
1167+
1168+
self.subcommands.append(command)
1169+
11631170
def command(
11641171
self, cls: type[T] = SlashCommand, **kwargs
11651172
) -> Callable[[Callable], SlashCommand]:
11661173
def wrap(func) -> T:
11671174
command = cls(func, parent=self, **kwargs)
1168-
self.subcommands.append(command)
1175+
self.add_command(command)
11691176
return command
11701177

11711178
return wrap
@@ -1262,7 +1269,7 @@ def inner(cls: type[SlashCommandGroup]) -> SlashCommandGroup:
12621269
guild_ids=guild_ids,
12631270
parent=self,
12641271
)
1265-
self.subcommands.append(group)
1272+
self.add_command(group)
12661273
return group
12671274

12681275
return inner

discord/ext/commands/help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def get_bot_mapping(self):
384384
"""Retrieves the bot mapping passed to :meth:`send_bot_help`."""
385385
bot = self.context.bot
386386
mapping = {cog: cog.get_commands() for cog in bot.cogs.values()}
387-
mapping[None] = [c for c in bot.commands if c.cog is None]
387+
mapping[None] = [c for c in bot.commands if not c.cog]
388388
return mapping
389389

390390
@property

0 commit comments

Comments
 (0)