def command(fn, name=None):
"""Decorator for functions that should be exposed as commands."""
module = sys.modules[fn.__module__]
if name is None:
name = fn.__name__
if asyncio.iscoroutinefunction(fn if inspect.isfunction(fn) else fn.__func__ if inspect.ismethod(fn) else fn.__call__): # Get the actual function for coroutine check
@functools.wraps(fn)
async def wrapper(*args, **kwargs):
try:
frame = inspect.currentframe()
ctx = frame.f_back.f_locals['ctx']
return await fn(ctx, *args, **kwargs)
finally:
del frame
else:
@functools.wraps(fn)
def wrapper(*args, **kwargs):
try:
frame = inspect.currentframe()
ctx = frame.f_back.f_locals['ctx']
return fn(ctx, *args, **kwargs)
finally:
del frame
vars(module).setdefault('commands', {})[fn.__name__] = wrapper
return wrapper
评论列表
文章目录