Browse Source

[commands] Fix special method detection for regular function objects.

Fixes #1920
pull/1929/head
Rapptz 6 years ago
parent
commit
d5d9164810
  1. 15
      discord/ext/commands/cog.py

15
discord/ext/commands/cog.py

@ -115,6 +115,10 @@ class CogMeta(type):
def qualified_name(cls): def qualified_name(cls):
return cls.__cog_name__ return cls.__cog_name__
def _cog_special_method(func):
func.__cog_special_method__ = None
return func
class Cog(metaclass=CogMeta): class Cog(metaclass=CogMeta):
"""The base class that all cogs must inherit from. """The base class that all cogs must inherit from.
@ -181,9 +185,7 @@ class Cog(metaclass=CogMeta):
@classmethod @classmethod
def _get_overridden_method(cls, method): def _get_overridden_method(cls, method):
"""Return None if the method is not overridden. Otherwise returns the overridden method.""" """Return None if the method is not overridden. Otherwise returns the overridden method."""
if method.__func__ is getattr(cls, method.__name__): return getattr(method.__func__, '__cog_special_method__', method)
return None
return method
@classmethod @classmethod
def listener(cls, name=None): def listener(cls, name=None):
@ -211,6 +213,7 @@ class Cog(metaclass=CogMeta):
return func return func
return decorator return decorator
@_cog_special_method
def cog_unload(self): def cog_unload(self):
"""A special method that is called when the cog gets removed. """A special method that is called when the cog gets removed.
@ -221,6 +224,7 @@ class Cog(metaclass=CogMeta):
""" """
pass pass
@_cog_special_method
def bot_check_once(self, ctx): def bot_check_once(self, ctx):
"""A special method that registers as a :meth:`.Bot.check_once` """A special method that registers as a :meth:`.Bot.check_once`
check. check.
@ -230,6 +234,7 @@ class Cog(metaclass=CogMeta):
""" """
return True return True
@_cog_special_method
def bot_check(self, ctx): def bot_check(self, ctx):
"""A special method that registers as a :meth:`.Bot.check` """A special method that registers as a :meth:`.Bot.check`
check. check.
@ -239,6 +244,7 @@ class Cog(metaclass=CogMeta):
""" """
return True return True
@_cog_special_method
def cog_check(self, ctx): def cog_check(self, ctx):
"""A special method that registers as a :func:`commands.check` """A special method that registers as a :func:`commands.check`
for every command and subcommand in this cog. for every command and subcommand in this cog.
@ -248,6 +254,7 @@ class Cog(metaclass=CogMeta):
""" """
return True return True
@_cog_special_method
def cog_command_error(self, ctx, error): def cog_command_error(self, ctx, error):
"""A special method that is called whenever an error """A special method that is called whenever an error
is dispatched inside this cog. is dispatched inside this cog.
@ -266,6 +273,7 @@ class Cog(metaclass=CogMeta):
""" """
pass pass
@_cog_special_method
async def cog_before_invoke(self, ctx): async def cog_before_invoke(self, ctx):
"""A special method that acts as a cog local pre-invoke hook. """A special method that acts as a cog local pre-invoke hook.
@ -280,6 +288,7 @@ class Cog(metaclass=CogMeta):
""" """
pass pass
@_cog_special_method
async def cog_after_invoke(self, ctx): async def cog_after_invoke(self, ctx):
"""A special method that acts as a cog local post-invoke hook. """A special method that acts as a cog local post-invoke hook.

Loading…
Cancel
Save