Skip to content

Commit

Permalink
fix: run only local hooks for SlashCommandGroup (#2139)
Browse files Browse the repository at this point in the history
* run local hooks only

* Changelog

---------

Signed-off-by: Om <92863779+Om1609@users.noreply.github.com>
  • Loading branch information
OmLanke committed Jun 25, 2023
1 parent 27d7782 commit 297053d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -142,6 +142,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2091](https://github.com/Pycord-Development/pycord/pull/2091))
- Fixed `AttributeError` when accessing a `Select`'s values when it hasn't been
interacted with. ([#2104](https://github.com/Pycord-Development/pycord/pull/2104))
- Fixed `before_invoke` being run twice for slash subcommands.
([#2139](https://github.com/Pycord-Development/pycord/pull/2139))
- Fixed `Guild._member_count` sometimes not being set.
([#2145](https://github.com/Pycord-Development/pycord/pull/2145))
- Fixed `Thread.applied_tags` not being updated.
Expand Down
22 changes: 22 additions & 0 deletions discord/commands/core.py
Expand Up @@ -1368,6 +1368,28 @@ async def invoke_autocomplete_callback(self, ctx: AutocompleteContext) -> None:
ctx.interaction.data = option
await command.invoke_autocomplete_callback(ctx)

async def call_before_hooks(self, ctx: ApplicationContext) -> None:
# only call local hooks
cog = self.cog
if self._before_invoke is not None:
# should be cog if @commands.before_invoke is used
instance = getattr(self._before_invoke, "__self__", cog)
# __self__ only exists for methods, not functions
# however, if @command.before_invoke is used, it will be a function
if instance:
await self._before_invoke(instance, ctx) # type: ignore
else:
await self._before_invoke(ctx) # type: ignore

async def call_after_hooks(self, ctx: ApplicationContext) -> None:
cog = self.cog
if self._after_invoke is not None:
instance = getattr(self._after_invoke, "__self__", cog)
if instance:
await self._after_invoke(instance, ctx) # type: ignore
else:
await self._after_invoke(ctx) # type: ignore

def walk_commands(self) -> Generator[SlashCommand | SlashCommandGroup, None, None]:
"""An iterator that recursively walks through all slash commands and groups in this group.
Expand Down

0 comments on commit 297053d

Please sign in to comment.