Browse Source

[commands] Fix exception raised in Command invoke hooks.

pull/5810/head
Josh 5 years ago
committed by GitHub
parent
commit
f8a7d3fc54
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      discord/ext/commands/core.py

30
discord/ext/commands/core.py

@ -724,18 +724,14 @@ class Command(_BaseCommand):
# first, call the command local hook: # first, call the command local hook:
cog = self.cog cog = self.cog
if self._before_invoke is not None: if self._before_invoke is not None:
try: # should be cog if @commands.before_invoke is used
instance = self._before_invoke.__self__ instance = getattr(self._before_invoke, '__self__', cog)
# should be cog if @commands.before_invoke is used # __self__ only exists for methods, not functions
except AttributeError: # however, if @command.before_invoke is used, it will be a function
# __self__ only exists for methods, not functions if instance:
# however, if @command.before_invoke is used, it will be a function
if self.cog:
await self._before_invoke(cog, ctx)
else:
await self._before_invoke(ctx)
else:
await self._before_invoke(instance, ctx) await self._before_invoke(instance, ctx)
else:
await self._before_invoke(ctx)
# call the cog local hook if applicable: # call the cog local hook if applicable:
if cog is not None: if cog is not None:
@ -751,15 +747,11 @@ class Command(_BaseCommand):
async def call_after_hooks(self, ctx): async def call_after_hooks(self, ctx):
cog = self.cog cog = self.cog
if self._after_invoke is not None: if self._after_invoke is not None:
try: instance = getattr(self._after_invoke, '__self__', cog)
instance = self._after_invoke.__self__ if instance:
except AttributeError: await self._after_invoke(instance, ctx)
if self.cog:
await self._after_invoke(cog, ctx)
else:
await self._after_invoke(ctx)
else: else:
await self._after_invoke(instance, ctx) await self._after_invoke(ctx)
# call the cog local hook if applicable: # call the cog local hook if applicable:
if cog is not None: if cog is not None:

Loading…
Cancel
Save