From 92274bfc34cc5af1975ec86bf2cfef28bafc8a03 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 21 May 2023 15:49:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat(!cfg):=20=E6=94=AF=E6=8C=81=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=82=B9=E5=8F=B7=E7=B4=A2=E5=BC=95=E5=AD=90=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/cmds/system/cconfig.py | 93 ++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 39 deletions(-) diff --git a/pkg/qqbot/cmds/system/cconfig.py b/pkg/qqbot/cmds/system/cconfig.py index 65d684f4..56c2a354 100644 --- a/pkg/qqbot/cmds/system/cconfig.py +++ b/pkg/qqbot/cmds/system/cconfig.py @@ -8,7 +8,7 @@ def config_operation(cmd, params): config = pkg.utils.context.get_config() reply_str = "" if len(params) == 0: - reply = ["[bot]err:请输入配置项"] + reply = ["[bot]err:请输入!cmd cfg查看使用方法"] else: cfg_name = params[0] if cfg_name == 'all': @@ -26,45 +26,60 @@ def config_operation(cmd, params): else: reply_str += "{}: {}\n".format(cfg, getattr(config, cfg)) reply = [reply_str] - elif cfg_name in dir(config): - if len(params) == 1: - # 按照配置项类型进行格式化 - if isinstance(getattr(config, cfg_name), str): - reply_str = "[bot]配置项{}: \"{}\"\n".format(cfg_name, getattr(config, cfg_name)) - elif isinstance(getattr(config, cfg_name), dict): - reply_str = "[bot]配置项{}: {}\n".format(cfg_name, - json.dumps(getattr(config, cfg_name), - ensure_ascii=False, indent=4)) - else: - reply_str = "[bot]配置项{}: {}\n".format(cfg_name, getattr(config, cfg_name)) - reply = [reply_str] - else: - 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 - - # 检查类型是否匹配 - if isinstance(getattr(config, cfg_name), type(cfg_value)): - setattr(config, cfg_name, cfg_value) - pkg.utils.context.set_config(config) - reply = ["[bot]配置项{}修改成功".format(cfg_name)] - else: - reply = ["[bot]err:配置项{}类型不匹配".format(cfg_name)] - else: - reply = ["[bot]err:未找到配置项 {}".format(cfg_name)] + cfg_entry_path = cfg_name.split('.') + + try: + if len(params) == 1: + cfg_entry = getattr(config, cfg_entry_path[0]) + if len(cfg_entry_path) > 1: + for i in range(1, len(cfg_entry_path)): + 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, + json.dumps(cfg_entry, + ensure_ascii=False, indent=4)) + else: + reply_str = "[bot]配置项{}: {}\n".format(cfg_name, cfg_entry) + reply = [reply_str] + else: + 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_entry = getattr(config, cfg_entry_path[0]) + if len(cfg_entry_path) > 1: + for i in range(1, len(cfg_entry_path) - 1): + 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)] + else: + reply = ["[bot]err:配置项{}类型不匹配".format(cfg_name)] + else: + setattr(config, cfg_entry_path[0], cfg_value) + reply = ["[bot]配置项{}修改成功".format(cfg_name)] + except AttributeError: + reply = ["[bot]err:未找到配置项 {}".format(cfg_name)] + except ValueError: + reply = ["[bot]err:未找到配置项 {}".format(cfg_name)] + # else: + # reply = ["[bot]err:未找到配置项 {}".format(cfg_name)] return reply From cdcc67ff2332cbdd0a83ed8a229c5eaf1b00badf Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 21 May 2023 15:53:56 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat(!cfg):=20=E4=BD=BF=E7=94=A8eval()?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E8=BF=9B=E8=A1=8C=E7=B1=BB=E5=9E=8B=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/cmds/system/cconfig.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pkg/qqbot/cmds/system/cconfig.py b/pkg/qqbot/cmds/system/cconfig.py index 56c2a354..149c3138 100644 --- a/pkg/qqbot/cmds/system/cconfig.py +++ b/pkg/qqbot/cmds/system/cconfig.py @@ -48,19 +48,20 @@ def config_operation(cmd, params): else: 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 + # 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_entry = getattr(config, cfg_entry_path[0]) if len(cfg_entry_path) > 1: From 23016a07916eee70efb8e1e77f2e464ae59e27a6 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sun, 21 May 2023 15:58:21 +0800 Subject: [PATCH 3/3] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0wiki=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/wiki/功能使用.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/res/wiki/功能使用.md b/res/wiki/功能使用.md index 493e3031..aee88fc4 100644 --- a/res/wiki/功能使用.md +++ b/res/wiki/功能使用.md @@ -225,7 +225,7 @@ 格式: `!cfg <配置项名称> <配置项新值>` 以修改`default_prompt`示例 ``` -!cfg default_prompt 我是Rock Chin +!cfg default_prompt "我是Rock Chin" ``` 输出示例 @@ -243,7 +243,15 @@ ``` !~all !~default_prompt -!~default_prompt 我是Rock Chin +!~default_prompt "我是Rock Chin" +``` + +5. 配置项名称支持使用点号(.)拼接以索引子配置项 + +例如: `openai_config.api_key`将索引`config`字典中的`openai_config`字典中的`api_key`字段,可以通过这个方式查看或修改此子配置项 + +``` +!~openai_config.api_key ```