diff --git a/main.py b/main.py index e6a0d1ba..5c831ae1 100644 --- a/main.py +++ b/main.py @@ -39,6 +39,38 @@ def init_db(): known_exception_caught = False +def reset_logging(): + assert os.path.exists('config.py') + + config = importlib.import_module('config') + + import pkg.utils.context + + if pkg.utils.context.context['logger_handler'] is not None: + logging.getLogger().removeHandler(pkg.utils.context.context['logger_handler']) + + for handler in logging.getLogger().handlers: + logging.getLogger().removeHandler(handler) + + logging.basicConfig(level=config.logging_level, # 设置日志输出格式 + filename='qchatgpt.log', # log日志输出的文件位置和文件名 + format="[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : %(message)s", + # 日志输出的格式 + # -8表示占位符,让输出左对齐,输出长度都为8位 + datefmt="%Y-%m-%d %H:%M:%S" # 时间输出的格式 + ) + sh = logging.StreamHandler() + sh.setLevel(config.logging_level) + sh.setFormatter(colorlog.ColoredFormatter( + fmt="%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : " + "%(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + log_colors=log_colors_config + )) + logging.getLogger().addHandler(sh) + return sh + + def main(first_time_init=False): global known_exception_caught @@ -52,25 +84,7 @@ def main(first_time_init=False): import pkg.utils.context pkg.utils.context.set_config(config) - if pkg.utils.context.context['logger_handler'] is not None: - logging.getLogger().removeHandler(pkg.utils.context.context['logger_handler']) - - logging.basicConfig(level=config.logging_level, # 设置日志输出格式 - filename='qchatgpt.log', # log日志输出的文件位置和文件名 - format="[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : %(message)s", - # 日志输出的格式 - # -8表示占位符,让输出左对齐,输出长度都为8位 - datefmt="%Y-%m-%d %H:%M:%S" # 时间输出的格式 - ) - sh = logging.StreamHandler() - sh.setLevel(config.logging_level) - sh.setFormatter(colorlog.ColoredFormatter( - fmt="%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : " - "%(message)s", - datefmt="%Y-%m-%d %H:%M:%S", - log_colors=log_colors_config - )) - logging.getLogger().addHandler(sh) + sh = reset_logging() # 检查是否设置了管理员 if not (hasattr(config, 'admin_qq') and config.admin_qq != 0): diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index fe7c323e..98cd4a7d 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -2,6 +2,7 @@ import asyncio import logging import importlib +import os import pkgutil import sys import traceback @@ -108,7 +109,16 @@ def install_plugin(repo_url: str): from dulwich import porcelain logging.info("克隆插件储存库: {}".format(repo_url)) - repo = porcelain.clone(repo_url, "plugins", checkout=True) + repo = porcelain.clone(repo_url, "plugins/"+repo_url.split(".git")[0].split("/")[-1]+"/", checkout=True) + + # 检查此目录是否包含requirements.txt + if os.path.exists("plugins/"+repo_url.split(".git")[0].split("/")[-1]+"/requirements.txt"): + logging.info("检测到requirements.txt,正在安装依赖") + import pkg.utils.pkgmgr + pkg.utils.pkgmgr.install_requirements("plugins/"+repo_url.split(".git")[0].split("/")[-1]+"/requirements.txt") + + import main + main.reset_logging() class EventContext: diff --git a/pkg/plugin/switch.py b/pkg/plugin/switch.py index 5dfc41b8..ea3441fa 100644 --- a/pkg/plugin/switch.py +++ b/pkg/plugin/switch.py @@ -59,8 +59,9 @@ def load_switch(): switch_modified = False + switch_copy = switch.copy() # 检查switch中多余的和path不相符的 - for plugin_name in switch: + for plugin_name in switch_copy: if plugin_name not in host.__plugins__: del switch[plugin_name] switch_modified = True diff --git a/pkg/qqbot/command.py b/pkg/qqbot/command.py index ff56a848..b4e1bcc5 100644 --- a/pkg/qqbot/command.py +++ b/pkg/qqbot/command.py @@ -88,7 +88,7 @@ def plugin_operation(cmd, params, is_admin): plugin_list = plugin_host.__plugins__ if len(params) == 0: - reply_str = "[bot]所有插件:\n\n" + reply_str = "[bot]所有插件{}:\n\n".format(len(plugin_list)) idx = 0 for key in plugin_list: plugin = plugin_list[key] @@ -102,7 +102,16 @@ def plugin_operation(cmd, params, is_admin): pass elif params[0].startswith("http"): if is_admin: - threading.Thread(target=plugin_host.install_plugin, args=(params[0],)).start() + + def closure(): + try: + plugin_host.install_plugin(params[0]) + pkg.utils.context.get_qqbot_manager().notify_admin("插件安装成功,请发送 !reload 指令重载插件") + except Exception as e: + logging.error("插件安装失败:{}".format(e)) + pkg.utils.context.get_qqbot_manager().notify_admin("插件安装失败:{}".format(e)) + + threading.Thread(target=closure, args=()).start() reply = ["[bot]正在安装插件..."] else: reply = ["[bot]err:权限不足,请使用管理员账号私聊发起"]