fix command
This commit is contained in:
parent
e459880373
commit
ed14485092
@ -184,16 +184,17 @@ class Command(metaclass=Singleton):
|
||||
try:
|
||||
logger.info(f"用户 {userid} 开始执行:{command.get('description')} ...")
|
||||
cmd_data = command['data'] if command.get('data') else {}
|
||||
if ObjectUtils.has_arguments(command['func']):
|
||||
args_num = ObjectUtils.has_arguments(command['func'])
|
||||
if args_num:
|
||||
if cmd_data:
|
||||
# 使用内置参数
|
||||
command['func'](**cmd_data)
|
||||
elif data_str:
|
||||
elif args_num > 1:
|
||||
# 使用用户输入参数
|
||||
command['func'](data_str, userid)
|
||||
else:
|
||||
# 没有用户输入参数
|
||||
command['func'](userid)
|
||||
command['func'](data_str)
|
||||
else:
|
||||
# 没有参数
|
||||
command['func']()
|
||||
|
@ -1,5 +1,5 @@
|
||||
import inspect
|
||||
from typing import Any
|
||||
from typing import Any, Callable
|
||||
|
||||
|
||||
class ObjectUtils:
|
||||
@ -12,9 +12,9 @@ class ObjectUtils:
|
||||
return str(obj).startswith("{") or str(obj).startswith("[")
|
||||
|
||||
@staticmethod
|
||||
def has_arguments(func):
|
||||
def has_arguments(func: Callable) -> int:
|
||||
"""
|
||||
判断函数是否有参数
|
||||
返回函数的参数个数
|
||||
"""
|
||||
signature = inspect.signature(func)
|
||||
parameters = signature.parameters
|
||||
@ -24,4 +24,4 @@ class ObjectUtils:
|
||||
if parameter_names and parameter_names[0] == 'self':
|
||||
parameter_names = parameter_names[1:]
|
||||
|
||||
return len(parameter_names) > 0
|
||||
return len(parameter_names)
|
||||
|
Loading…
x
Reference in New Issue
Block a user