diff options
| author | Rapptz <[email protected]> | 2021-06-01 08:51:35 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2021-06-01 08:51:35 -0400 |
| commit | bac6c2fc7b5c12c8da260578302d45e8ee2e5417 (patch) | |
| tree | a893470b4bb53c7f7773579fb824046b945f304b | |
| parent | Add Client.persistent_views to get all persistent views (diff) | |
| download | discord.py-bac6c2fc7b5c12c8da260578302d45e8ee2e5417.tar.xz discord.py-bac6c2fc7b5c12c8da260578302d45e8ee2e5417.zip | |
[commands] Unwrap functions to get their module and globalns
Fixes #7002
| -rw-r--r-- | discord/ext/commands/core.py | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 7cd736d4..e9dbd385 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -24,6 +24,7 @@ DEALINGS IN THE SOFTWARE. from typing import ( Any, + Callable, Dict, Literal, Union, @@ -69,8 +70,18 @@ __all__ = ( 'bot_has_guild_permissions' ) -def get_signature_parameters(function: types.FunctionType) -> Dict[str, inspect.Parameter]: - globalns = function.__globals__ +def unwrap_function(function: Callable[..., Any]) -> Callable[..., Any]: + partial = functools.partial + while True: + if hasattr(function, '__wrapped__'): + function = function.__wrapped__ + elif isinstance(function, partial): + function = function.func + else: + return function + + +def get_signature_parameters(function: Callable[..., Any], globalns: Dict[str, Any]) -> Dict[str, inspect.Parameter]: signature = inspect.signature(function) params = {} cache: Dict[str, Any] = {} @@ -329,8 +340,15 @@ class Command(_BaseCommand): @callback.setter def callback(self, function): self._callback = function - self.module = function.__module__ - self.params = get_signature_parameters(function) + unwrap = unwrap_function(function) + self.module = unwrap.__module__ + + try: + globalns = unwrap.__globals__ + except AttributeError: + globalns = {} + + self.params = get_signature_parameters(function, globalns) def add_check(self, func): """Adds a check to the command. |