mirror of
https://github.com/langbot-app/LangBot.git
synced 2025-11-25 11:29:39 +08:00
feat(!cfg): 支持使用点号索引子配置项
This commit is contained in:
@@ -8,7 +8,7 @@ def config_operation(cmd, params):
|
|||||||
config = pkg.utils.context.get_config()
|
config = pkg.utils.context.get_config()
|
||||||
reply_str = ""
|
reply_str = ""
|
||||||
if len(params) == 0:
|
if len(params) == 0:
|
||||||
reply = ["[bot]err:请输入配置项"]
|
reply = ["[bot]err:请输入!cmd cfg查看使用方法"]
|
||||||
else:
|
else:
|
||||||
cfg_name = params[0]
|
cfg_name = params[0]
|
||||||
if cfg_name == 'all':
|
if cfg_name == 'all':
|
||||||
@@ -26,17 +26,24 @@ def config_operation(cmd, params):
|
|||||||
else:
|
else:
|
||||||
reply_str += "{}: {}\n".format(cfg, getattr(config, cfg))
|
reply_str += "{}: {}\n".format(cfg, getattr(config, cfg))
|
||||||
reply = [reply_str]
|
reply = [reply_str]
|
||||||
elif cfg_name in dir(config):
|
else:
|
||||||
|
cfg_entry_path = cfg_name.split('.')
|
||||||
|
|
||||||
|
try:
|
||||||
if len(params) == 1:
|
if len(params) == 1:
|
||||||
# 按照配置项类型进行格式化
|
cfg_entry = getattr(config, cfg_entry_path[0])
|
||||||
if isinstance(getattr(config, cfg_name), str):
|
if len(cfg_entry_path) > 1:
|
||||||
reply_str = "[bot]配置项{}: \"{}\"\n".format(cfg_name, getattr(config, cfg_name))
|
for i in range(1, len(cfg_entry_path)):
|
||||||
elif isinstance(getattr(config, cfg_name), dict):
|
cfg_entry = cfg_entry[cfg_entry_path[i]]
|
||||||
|
|
||||||
|
if isinstance(cfg_entry, str):
|
||||||
|
reply_str = "[bot]配置项{}: \"{}\"\n".format(cfg_name, cfg_entry)
|
||||||
|
elif isinstance(cfg_entry, dict):
|
||||||
reply_str = "[bot]配置项{}: {}\n".format(cfg_name,
|
reply_str = "[bot]配置项{}: {}\n".format(cfg_name,
|
||||||
json.dumps(getattr(config, cfg_name),
|
json.dumps(cfg_entry,
|
||||||
ensure_ascii=False, indent=4))
|
ensure_ascii=False, indent=4))
|
||||||
else:
|
else:
|
||||||
reply_str = "[bot]配置项{}: {}\n".format(cfg_name, getattr(config, cfg_name))
|
reply_str = "[bot]配置项{}: {}\n".format(cfg_name, cfg_entry)
|
||||||
reply = [reply_str]
|
reply = [reply_str]
|
||||||
else:
|
else:
|
||||||
cfg_value = " ".join(params[1:])
|
cfg_value = " ".join(params[1:])
|
||||||
@@ -55,16 +62,24 @@ def config_operation(cmd, params):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# 检查类型是否匹配
|
cfg_entry = getattr(config, cfg_entry_path[0])
|
||||||
if isinstance(getattr(config, cfg_name), type(cfg_value)):
|
if len(cfg_entry_path) > 1:
|
||||||
setattr(config, cfg_name, cfg_value)
|
for i in range(1, len(cfg_entry_path) - 1):
|
||||||
pkg.utils.context.set_config(config)
|
cfg_entry = cfg_entry[cfg_entry_path[i]]
|
||||||
|
if isinstance(cfg_entry[cfg_entry_path[-1]], type(cfg_value)):
|
||||||
|
cfg_entry[cfg_entry_path[-1]] = cfg_value
|
||||||
reply = ["[bot]配置项{}修改成功".format(cfg_name)]
|
reply = ["[bot]配置项{}修改成功".format(cfg_name)]
|
||||||
else:
|
else:
|
||||||
reply = ["[bot]err:配置项{}类型不匹配".format(cfg_name)]
|
reply = ["[bot]err:配置项{}类型不匹配".format(cfg_name)]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
setattr(config, cfg_entry_path[0], cfg_value)
|
||||||
|
reply = ["[bot]配置项{}修改成功".format(cfg_name)]
|
||||||
|
except AttributeError:
|
||||||
reply = ["[bot]err:未找到配置项 {}".format(cfg_name)]
|
reply = ["[bot]err:未找到配置项 {}".format(cfg_name)]
|
||||||
|
except ValueError:
|
||||||
|
reply = ["[bot]err:未找到配置项 {}".format(cfg_name)]
|
||||||
|
# else:
|
||||||
|
# reply = ["[bot]err:未找到配置项 {}".format(cfg_name)]
|
||||||
|
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user