From ed14485092832365888fb7c25fa11f03836d49dc Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 15 Jun 2023 22:59:47 +0800 Subject: [PATCH] fix command --- app/command.py | 7 ++++--- app/utils/object.py | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/command.py b/app/command.py index 74c9c35e..72382647 100644 --- a/app/command.py +++ b/app/command.py @@ -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']() diff --git a/app/utils/object.py b/app/utils/object.py index 09959ffd..507d7502 100644 --- a/app/utils/object.py +++ b/app/utils/object.py @@ -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)