From 6af55d8a1dd294a0186c83eb92cb69264df4aa52 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Tue, 17 Jan 2023 00:11:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9F=BA=E6=9C=AC=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/host.py | 19 +++++++++++++++++++ pkg/qqbot/command.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index 100d7ec4..fe7c323e 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -92,6 +92,25 @@ def unload_plugins(): logging.error("插件{}卸载时发生错误: {}".format(plugin['name'], sys.exc_info())) +def install_plugin(repo_url: str): + """ 安装插件,从git储存库获取并解决依赖 """ + try: + import pkg.utils.pkgmgr + pkg.utils.pkgmgr.ensure_dulwich() + except: + pass + + try: + import dulwich + except ModuleNotFoundError: + raise Exception("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77") + + from dulwich import porcelain + + logging.info("克隆插件储存库: {}".format(repo_url)) + repo = porcelain.clone(repo_url, "plugins", checkout=True) + + class EventContext: """ 事件上下文 """ eid = 0 diff --git a/pkg/qqbot/command.py b/pkg/qqbot/command.py index 553381cc..ff56a848 100644 --- a/pkg/qqbot/command.py +++ b/pkg/qqbot/command.py @@ -80,6 +80,35 @@ def config_operation(cmd, params): return reply +def plugin_operation(cmd, params, is_admin): + reply = [] + + import pkg.plugin.host as plugin_host + + plugin_list = plugin_host.__plugins__ + + if len(params) == 0: + reply_str = "[bot]所有插件:\n\n" + idx = 0 + for key in plugin_list: + plugin = plugin_list[key] + print(plugin) + reply_str += "#{} {}:\n{}\nv{}\n作者:{}\n\n".format((idx+1), plugin['name'], plugin['description'], + plugin['version'], plugin['author']) + idx += 1 + + reply = [reply_str] + elif params[0] == 'update': + pass + elif params[0].startswith("http"): + if is_admin: + threading.Thread(target=plugin_host.install_plugin, args=(params[0],)).start() + reply = ["[bot]正在安装插件..."] + else: + reply = ["[bot]err:权限不足,请使用管理员账号私聊发起"] + return reply + + def process_command(session_name: str, text_message: str, mgr, config, launcher_type: str, launcher_id: int, sender_id: int) -> list: reply = [] @@ -197,6 +226,11 @@ def process_command(session_name: str, text_message: str, mgr, config, pass reply = [reply_str] + + elif cmd == 'plugin': + reply = plugin_operation(cmd, params, True + if (launcher_type == 'person' and launcher_id == config.admin_qq) + else False) elif cmd == 'reload' and launcher_type == 'person' and launcher_id == config.admin_qq: def reload_task(): pkg.utils.reloader.reload_all()