feat: 支持从储存库获取插件

This commit is contained in:
Rock Chin
2023-01-17 11:54:33 +08:00
parent b4938ba1fb
commit 120ec98ba7
4 changed files with 57 additions and 23 deletions

28
main.py
View File

@@ -39,22 +39,19 @@ def init_db():
known_exception_caught = False
def main(first_time_init=False):
global known_exception_caught
known_exception_caught = False
try:
# 导入config.py
def reset_logging():
assert os.path.exists('config.py')
config = importlib.import_module('config')
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'])
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",
@@ -71,6 +68,23 @@ def main(first_time_init=False):
log_colors=log_colors_config
))
logging.getLogger().addHandler(sh)
return sh
def main(first_time_init=False):
global known_exception_caught
known_exception_caught = False
try:
# 导入config.py
assert os.path.exists('config.py')
config = importlib.import_module('config')
import pkg.utils.context
pkg.utils.context.set_config(config)
sh = reset_logging()
# 检查是否设置了管理员
if not (hasattr(config, 'admin_qq') and config.admin_qq != 0):

View File

@@ -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:

View File

@@ -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

View File

@@ -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:权限不足,请使用管理员账号私聊发起"]