[commands] Unwrap functions to get their module and globalns
Fixes #7002
This commit is contained in:
parent
78275023cc
commit
bac6c2fc7b
@ -24,6 +24,7 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
|
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
|
Callable,
|
||||||
Dict,
|
Dict,
|
||||||
Literal,
|
Literal,
|
||||||
Union,
|
Union,
|
||||||
@ -69,8 +70,18 @@ __all__ = (
|
|||||||
'bot_has_guild_permissions'
|
'bot_has_guild_permissions'
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_signature_parameters(function: types.FunctionType) -> Dict[str, inspect.Parameter]:
|
def unwrap_function(function: Callable[..., Any]) -> Callable[..., Any]:
|
||||||
globalns = function.__globals__
|
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)
|
signature = inspect.signature(function)
|
||||||
params = {}
|
params = {}
|
||||||
cache: Dict[str, Any] = {}
|
cache: Dict[str, Any] = {}
|
||||||
@ -329,8 +340,15 @@ class Command(_BaseCommand):
|
|||||||
@callback.setter
|
@callback.setter
|
||||||
def callback(self, function):
|
def callback(self, function):
|
||||||
self._callback = function
|
self._callback = function
|
||||||
self.module = function.__module__
|
unwrap = unwrap_function(function)
|
||||||
self.params = get_signature_parameters(function)
|
self.module = unwrap.__module__
|
||||||
|
|
||||||
|
try:
|
||||||
|
globalns = unwrap.__globals__
|
||||||
|
except AttributeError:
|
||||||
|
globalns = {}
|
||||||
|
|
||||||
|
self.params = get_signature_parameters(function, globalns)
|
||||||
|
|
||||||
def add_check(self, func):
|
def add_check(self, func):
|
||||||
"""Adds a check to the command.
|
"""Adds a check to the command.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user