prof: 改善加载插件时的稳定性

This commit is contained in:
Soulter
2024-06-03 00:20:56 -04:00
parent 7a9fd7fd1e
commit e4a5cbd893
4 changed files with 30 additions and 22 deletions
+2 -2
View File
@@ -196,7 +196,7 @@ class AstrBotDashBoard():
repo_url = post_data["url"]
try:
logger.info(f"正在安装插件 {repo_url}")
putil.install_plugin(repo_url, self.dashboard_data.plugins)
putil.install_plugin(repo_url, global_object)
logger.info(f"安装插件 {repo_url} 成功")
return Response(
status="success",
@@ -237,7 +237,7 @@ class AstrBotDashBoard():
plugin_name = post_data["name"]
try:
logger.info(f"正在更新插件 {plugin_name}")
putil.update_plugin(plugin_name, self.dashboard_data.plugins)
putil.update_plugin(plugin_name, global_object)
logger.info(f"更新插件 {plugin_name} 成功")
return Response(
status="success",
+7 -7
View File
@@ -97,7 +97,7 @@ class Command:
if self.command_start_with(message, "nick"):
return True, self.set_nick(message, platform, role)
if self.command_start_with(message, "plugin"):
return True, self.plugin_oper(message, role, cached_plugins, platform)
return True, self.plugin_oper(message, role, self.global_object, platform)
if self.command_start_with(message, "myid") or self.command_start_with(message, "!myid"):
return True, self.get_my_id(message_obj, platform)
if self.command_start_with(message, "web"): # 网页搜索
@@ -141,7 +141,7 @@ class Command:
插件指令
'''
def plugin_oper(self, message: str, role: str, cached_plugins: List[RegisteredPlugin], platform: str):
def plugin_oper(self, message: str, role: str, ctx: GlobalObject, platform: str):
l = message.split(" ")
if len(l) < 2:
p = gu.create_text_image(
@@ -152,7 +152,7 @@ class Command:
if role != "admin":
return False, f"你的身份组{role}没有权限安装插件", "plugin"
try:
putil.install_plugin(l[2], cached_plugins)
putil.install_plugin(l[2], )
return True, "插件拉取并载入成功~", "plugin"
except BaseException as e:
return False, f"拉取插件失败,原因: {str(e)}", "plugin"
@@ -160,20 +160,20 @@ class Command:
if role != "admin":
return False, f"你的身份组{role}没有权限删除插件", "plugin"
try:
putil.uninstall_plugin(l[2], cached_plugins)
putil.uninstall_plugin(l[2], ctx)
return True, "插件卸载成功~", "plugin"
except BaseException as e:
return False, f"卸载插件失败,原因: {str(e)}", "plugin"
elif l[1] == "u":
try:
putil.update_plugin(l[2], cached_plugins)
putil.update_plugin(l[2], ctx)
return True, "\n更新插件成功!!", "plugin"
except BaseException as e:
return False, f"更新插件失败,原因: {str(e)}\n建议: 使用 plugin i 指令进行覆盖安装(插件数据可能会丢失)", "plugin"
elif l[1] == "l":
try:
plugin_list_info = ""
for plugin in cached_plugins:
for plugin in ctx.cached_plugins:
plugin_list_info += f"{plugin.metadata.plugin_name}: \n名称: {plugin.metadata.plugin_name}\n简介: {plugin.metadata.plugin_desc}\n版本: {plugin.metadata.version}\n作者: {plugin.metadata.author}\n"
p = gu.create_text_image(
"【已激活插件列表】", plugin_list_info + "\n使用plugin v 插件名 查看插件帮助\n")
@@ -183,7 +183,7 @@ class Command:
elif l[1] == "v":
try:
info = None
for i in cached_plugins:
for i in ctx.cached_plugins:
if i.metadata.plugin_name == l[2]:
info = i.metadata
break
+13 -5
View File
@@ -370,23 +370,31 @@ def save_temp_img(img: Image) -> str:
logger.info(f"保存临时图片: {p}")
return p
async def download_image_by_url(url: str) -> str:
async def download_image_by_url(url: str, post: bool = False, post_data: dict = None) -> str:
'''
下载图片
'''
try:
logger.info(f"下载图片: {url}")
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
return save_temp_img(await resp.read())
if post:
async with session.post(url, json=post_data) as resp:
return save_temp_img(await resp.read())
else:
async with session.get(url) as resp:
return save_temp_img(await resp.read())
except aiohttp.client_exceptions.ClientConnectorSSLError as e:
# 关闭SSL验证
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
async with aiohttp.ClientSession(trust_env=False) as session:
async with session.get(url, ssl=ssl_context) as resp:
return save_temp_img(await resp.read())
if post:
async with session.get(url, ssl=ssl_context) as resp:
return save_temp_img(await resp.read())
else:
async with session.get(url, ssl=ssl_context) as resp:
return save_temp_img(await resp.read())
except Exception as e:
raise e
+8 -8
View File
@@ -180,7 +180,7 @@ def update_plugin_dept(path):
os.system(f"{py} -m pip install -r {path} -i {mirror} --quiet")
def install_plugin(repo_url: str, cached_plugins: RegisteredPlugins):
def install_plugin(repo_url: str, ctx: GlobalObject):
ppath = get_plugin_store_path()
# 删除末尾的 /
if repo_url.endswith("/"):
@@ -195,7 +195,7 @@ def install_plugin(repo_url: str, cached_plugins: RegisteredPlugins):
if os.path.exists(plugin_path):
remove_dir(plugin_path)
Repo.clone_from(repo_url, to_path=plugin_path, branch='master')
ok, err = plugin_reload(cached_plugins)
ok, err = plugin_reload(ctx)
if not ok:
raise Exception(err)
@@ -209,19 +209,19 @@ def get_registered_plugin(plugin_name: str, cached_plugins: RegisteredPlugins) -
return ret
def uninstall_plugin(plugin_name: str, cached_plugins: RegisteredPlugins):
plugin = get_registered_plugin(plugin_name, cached_plugins)
def uninstall_plugin(plugin_name: str, ctx: GlobalObject):
plugin = get_registered_plugin(plugin_name, ctx.cached_plugins)
if not plugin:
raise Exception("插件不存在。")
root_dir_name = plugin.root_dir_name
ppath = get_plugin_store_path()
cached_plugins.remove(plugin)
ctx.cached_plugins.remove(plugin)
if not remove_dir(os.path.join(ppath, root_dir_name)):
raise Exception("移除插件成功,但是删除插件文件夹失败。您可以手动删除该文件夹,位于 addons/plugins/ 下。")
def update_plugin(plugin_name: str, cached_plugins: RegisteredPlugins):
plugin = get_registered_plugin(plugin_name, cached_plugins)
def update_plugin(plugin_name: str, ctx: GlobalObject):
plugin = get_registered_plugin(plugin_name, ctx.cached_plugins)
if not plugin:
raise Exception("插件不存在。")
ppath = get_plugin_store_path()
@@ -229,7 +229,7 @@ def update_plugin(plugin_name: str, cached_plugins: RegisteredPlugins):
plugin_path = os.path.join(ppath, root_dir_name)
repo = Repo(path=plugin_path)
repo.remotes.origin.pull()
ok, err = plugin_reload(cached_plugins)
ok, err = plugin_reload(ctx)
if not ok:
raise Exception(err)