fix: cfg 命令无法使用 (#638)

This commit is contained in:
RockChinQ 2023-12-19 17:37:40 +08:00
parent 3f290b2e1a
commit 7386daad28

View File

@ -6,7 +6,12 @@ from .. import aamgr
def config_operation(cmd, params): def config_operation(cmd, params):
reply = [] reply = []
import pkg.utils.context import pkg.utils.context
config = pkg.utils.context.get_config() # config = pkg.utils.context.get_config()
cfg_mgr = pkg.utils.context.get_config_manager()
false = False
true = True
reply_str = "" reply_str = ""
if len(params) == 0: if len(params) == 0:
reply = ["[bot]err:请输入!cmd cfg查看使用方法"] reply = ["[bot]err:请输入!cmd cfg查看使用方法"]
@ -14,25 +19,25 @@ def config_operation(cmd, params):
cfg_name = params[0] cfg_name = params[0]
if cfg_name == 'all': if cfg_name == 'all':
reply_str = "[bot]所有配置项:\n\n" reply_str = "[bot]所有配置项:\n\n"
for cfg in dir(config): for cfg in cfg_mgr.data.keys():
if not cfg.startswith('__') and not cfg == 'logging': if not cfg.startswith('__') and not cfg == 'logging':
# 根据配置项类型进行格式化如果是字典则转换为json并格式化 # 根据配置项类型进行格式化如果是字典则转换为json并格式化
if isinstance(getattr(config, cfg), str): if isinstance(cfg_mgr.data[cfg], str):
reply_str += "{}: \"{}\"\n".format(cfg, getattr(config, cfg)) reply_str += "{}: \"{}\"\n".format(cfg, cfg_mgr.data[cfg])
elif isinstance(getattr(config, cfg), dict): elif isinstance(cfg_mgr.data[cfg], dict):
# 不进行unicode转义并格式化 # 不进行unicode转义并格式化
reply_str += "{}: {}\n".format(cfg, reply_str += "{}: {}\n".format(cfg,
json.dumps(getattr(config, cfg), json.dumps(cfg_mgr.data[cfg],
ensure_ascii=False, indent=4)) ensure_ascii=False, indent=4))
else: else:
reply_str += "{}: {}\n".format(cfg, getattr(config, cfg)) reply_str += "{}: {}\n".format(cfg, cfg_mgr.data[cfg])
reply = [reply_str] reply = [reply_str]
else: else:
cfg_entry_path = cfg_name.split('.') cfg_entry_path = cfg_name.split('.')
try: try:
if len(params) == 1: if len(params) == 1: # 未指定配置值,返回配置项值
cfg_entry = getattr(config, cfg_entry_path[0]) cfg_entry = cfg_mgr.data[cfg_entry_path[0]]
if len(cfg_entry_path) > 1: if len(cfg_entry_path) > 1:
for i in range(1, len(cfg_entry_path)): for i in range(1, len(cfg_entry_path)):
cfg_entry = cfg_entry[cfg_entry_path[i]] cfg_entry = cfg_entry[cfg_entry_path[i]]
@ -48,23 +53,10 @@ def config_operation(cmd, params):
reply = [reply_str] reply = [reply_str]
else: else:
cfg_value = " ".join(params[1:]) cfg_value = " ".join(params[1:])
# 类型转换如果是json则转换为字典
# if cfg_value == 'true':
# cfg_value = True
# elif cfg_value == 'false':
# cfg_value = False
# elif cfg_value.isdigit():
# cfg_value = int(cfg_value)
# elif cfg_value.startswith('{') and cfg_value.endswith('}'):
# cfg_value = json.loads(cfg_value)
# else:
# try:
# cfg_value = float(cfg_value)
# except ValueError:
# pass
cfg_value = eval(cfg_value) cfg_value = eval(cfg_value)
cfg_entry = getattr(config, cfg_entry_path[0]) cfg_entry = cfg_mgr.data[cfg_entry_path[0]]
if len(cfg_entry_path) > 1: if len(cfg_entry_path) > 1:
for i in range(1, len(cfg_entry_path) - 1): for i in range(1, len(cfg_entry_path) - 1):
cfg_entry = cfg_entry[cfg_entry_path[i]] cfg_entry = cfg_entry[cfg_entry_path[i]]
@ -74,14 +66,12 @@ def config_operation(cmd, params):
else: else:
reply = ["[bot]err:配置项{}类型不匹配".format(cfg_name)] reply = ["[bot]err:配置项{}类型不匹配".format(cfg_name)]
else: else:
setattr(config, cfg_entry_path[0], cfg_value) cfg_mgr.data[cfg_entry_path[0]] = cfg_value
reply = ["[bot]配置项{}修改成功".format(cfg_name)] reply = ["[bot]配置项{}修改成功".format(cfg_name)]
except AttributeError: except AttributeError:
reply = ["[bot]err:未找到配置项 {}".format(cfg_name)] reply = ["[bot]err:未找到配置项 {}".format(cfg_name)]
except ValueError: except ValueError:
reply = ["[bot]err:未找到配置项 {}".format(cfg_name)] reply = ["[bot]err:未找到配置项 {}".format(cfg_name)]
# else:
# reply = ["[bot]err:未找到配置项 {}".format(cfg_name)]
return reply return reply