From 2069ba6836e450e1c41f7acd27f5ce6b71277b5c Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Thu, 30 Mar 2023 03:38:33 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20system=E7=B1=BB=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/cmds/mgr.py | 4 ++- pkg/qqbot/cmds/system/__init__.py | 0 pkg/qqbot/cmds/system/help.py | 38 ++++++++++++++++++++++++++++ pkg/qqbot/cmds/system/reload.py | 23 +++++++++++++++++ pkg/qqbot/cmds/system/update.py | 38 ++++++++++++++++++++++++++++ pkg/qqbot/cmds/system/usage.py | 42 +++++++++++++++++++++++++++++++ pkg/qqbot/cmds/system/version.py | 27 ++++++++++++++++++++ 7 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 pkg/qqbot/cmds/system/__init__.py create mode 100644 pkg/qqbot/cmds/system/help.py create mode 100644 pkg/qqbot/cmds/system/reload.py create mode 100644 pkg/qqbot/cmds/system/update.py create mode 100644 pkg/qqbot/cmds/system/usage.py create mode 100644 pkg/qqbot/cmds/system/version.py diff --git a/pkg/qqbot/cmds/mgr.py b/pkg/qqbot/cmds/mgr.py index e181ea84..351b3809 100644 --- a/pkg/qqbot/cmds/mgr.py +++ b/pkg/qqbot/cmds/mgr.py @@ -156,7 +156,7 @@ class AbstractCommandNode: @classmethod def help(cls) -> str: """获取指令帮助信息""" - return '指令: {}\n描述: {}\n用法: {}\n别名: {}\n权限: {}'.format( + return '指令: {}\n描述: {}\n用法: \n{}\n别名: {}\n权限: {}'.format( cls.name, cls.description, cls.usage, @@ -297,6 +297,8 @@ def register_all(): # 排除不处于pkg.qqbot.cmds中的包 if not module.__name__.startswith('pkg.qqbot.cmds'): return + + logging.debug('walk: {}, path: {}'.format(module.__name__, module.__path__)) for item in pkgutil.iter_modules(module.__path__): if item.name.startswith('__'): continue diff --git a/pkg/qqbot/cmds/system/__init__.py b/pkg/qqbot/cmds/system/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pkg/qqbot/cmds/system/help.py b/pkg/qqbot/cmds/system/help.py new file mode 100644 index 00000000..08c50b88 --- /dev/null +++ b/pkg/qqbot/cmds/system/help.py @@ -0,0 +1,38 @@ +from ..mgr import AbstractCommandNode, Context, __command_list__ + + +@AbstractCommandNode.register( + parent=None, + name="help", + description="显示帮助信息", + usage="!help\n!help <指令名称>", + aliases=[], + privilege=1 +) +class HelpCommand(AbstractCommandNode): + @classmethod + def process(cls, ctx: Context) -> tuple[bool, list]: + command_list = __command_list__ + + reply = [] + + if len(ctx.params) == 0: + reply_str = "[bot]当前所有指令:\n\n" + + # 遍历顶级指令 + for key in command_list: + command = command_list[key] + if command['parent'] is None: + reply_str += "!{} - {}\n".format(key, command['description']) + + reply_str += "\n请使用 !help <指令名称> 来查看指令的详细信息" + + reply = [reply_str] + else: + command_name = ctx.params[0] + if command_name in command_list: + reply = [command_list[command_name]['cls'].help()] + else: + reply = ["[bot]指令 {} 不存在".format(command_name)] + + return True, reply \ No newline at end of file diff --git a/pkg/qqbot/cmds/system/reload.py b/pkg/qqbot/cmds/system/reload.py new file mode 100644 index 00000000..ae72b655 --- /dev/null +++ b/pkg/qqbot/cmds/system/reload.py @@ -0,0 +1,23 @@ +from ..mgr import AbstractCommandNode, Context +import threading + +@AbstractCommandNode.register( + parent=None, + name="reload", + description="执行热重载", + usage="!reload", + aliases=[], + privilege=2 +) +class ReloadCommand(AbstractCommandNode): + @classmethod + def process(cls, ctx: Context) -> tuple[bool, list]: + reply = [] + + import pkg.utils.reloader + def reload_task(): + pkg.utils.reloader.reload_all() + + threading.Thread(target=reload_task, daemon=True).start() + + return True, reply \ No newline at end of file diff --git a/pkg/qqbot/cmds/system/update.py b/pkg/qqbot/cmds/system/update.py new file mode 100644 index 00000000..b8ebfafb --- /dev/null +++ b/pkg/qqbot/cmds/system/update.py @@ -0,0 +1,38 @@ +from ..mgr import AbstractCommandNode, Context +import threading +import traceback + + +@AbstractCommandNode.register( + parent=None, + name="update", + description="更新程序", + usage="!update", + aliases=[], + privilege=2 +) +class UpdateCommand(AbstractCommandNode): + @classmethod + def process(cls, ctx: Context) -> tuple[bool, list]: + reply = [] + import pkg.utils.updater + import pkg.utils.reloader + import pkg.utils.context + + def update_task(): + try: + if pkg.utils.updater.update_all(): + pkg.utils.reloader.reload_all(notify=False) + pkg.utils.context.get_qqbot_manager().notify_admin("更新完成") + else: + pkg.utils.context.get_qqbot_manager().notify_admin("无新版本") + except Exception as e0: + traceback.print_exc() + pkg.utils.context.get_qqbot_manager().notify_admin("更新失败:{}".format(e0)) + return + + threading.Thread(target=update_task, daemon=True).start() + + reply = ["[bot]正在更新,请耐心等待,请勿重复发起更新..."] + + return True, reply \ No newline at end of file diff --git a/pkg/qqbot/cmds/system/usage.py b/pkg/qqbot/cmds/system/usage.py new file mode 100644 index 00000000..c1584b2d --- /dev/null +++ b/pkg/qqbot/cmds/system/usage.py @@ -0,0 +1,42 @@ +from ..mgr import AbstractCommandNode, Context +import logging + + +@AbstractCommandNode.register( + parent=None, + name="usage", + description="获取使用情况", + usage="!usage", + aliases=[], + privilege=1 +) +class UsageCommand(AbstractCommandNode): + @classmethod + def process(cls, ctx: Context) -> tuple[bool, list]: + import config + import pkg.utils.credit as credit + import pkg.utils.context + + reply = [] + + reply_str = "[bot]各api-key使用情况:\n\n" + + api_keys = pkg.utils.context.get_openai_manager().key_mgr.api_key + for key_name in api_keys: + text_length = pkg.utils.context.get_openai_manager().audit_mgr \ + .get_text_length_of_key(api_keys[key_name]) + image_count = pkg.utils.context.get_openai_manager().audit_mgr \ + .get_image_count_of_key(api_keys[key_name]) + reply_str += "{}:\n - 文本长度:{}\n - 图片数量:{}\n".format(key_name, int(text_length), + int(image_count)) + # 获取此key的额度 + try: + http_proxy = config.openai_config["http_proxy"] if "http_proxy" in config.openai_config else None + credit_data = credit.fetch_credit_data(api_keys[key_name], http_proxy) + reply_str += " - 使用额度:{:.2f}/{:.2f}\n".format(credit_data['total_used'],credit_data['total_granted']) + except Exception as e: + logging.warning("获取额度失败:{}".format(e)) + + reply = [reply_str] + + return True, reply \ No newline at end of file diff --git a/pkg/qqbot/cmds/system/version.py b/pkg/qqbot/cmds/system/version.py new file mode 100644 index 00000000..f164f5bf --- /dev/null +++ b/pkg/qqbot/cmds/system/version.py @@ -0,0 +1,27 @@ +from ..mgr import AbstractCommandNode, Context + + +@AbstractCommandNode.register( + parent=None, + name="version", + description="查看版本信息", + usage="!version", + aliases=[], + privilege=1 +) +class VersionCommand(AbstractCommandNode): + @classmethod + def process(cls, ctx: Context) -> tuple[bool, list]: + reply = [] + import pkg.utils.updater + + reply_str = "[bot]当前版本:\n{}\n".format(pkg.utils.updater.get_current_version_info()) + try: + if pkg.utils.updater.is_new_version_available(): + reply_str += "\n有新版本可用,请使用命令 !update 进行更新" + except: + pass + + reply = [reply_str] + + return True, reply \ No newline at end of file