forked from bot/app
fix: 插件列表显示错误问题
This commit is contained in:
@ -5,26 +5,24 @@ import sys
|
||||
from io import StringIO
|
||||
from typing import Optional
|
||||
|
||||
import aiofiles
|
||||
import aiohttp
|
||||
import nonebot
|
||||
import pip
|
||||
from arclet.alconna import Arparma, MultiVar
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.utils import run_sync
|
||||
from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand
|
||||
import pip
|
||||
|
||||
import aiohttp, aiofiles
|
||||
from typing_extensions import Any
|
||||
from nonebot_plugin_alconna import Alconna, Args, Subcommand, on_alconna
|
||||
|
||||
from src.utils.language import get_user_lang
|
||||
from src.utils.message import Markdown as md, send_markdown
|
||||
from src.utils.resource import get_res
|
||||
from src.utils.typing import T_Bot, T_MessageEvent
|
||||
|
||||
from .common import *
|
||||
from src.utils.data_manager import InstalledPlugin
|
||||
|
||||
npm_alc = on_alconna(
|
||||
Alconna(
|
||||
"lnpm",
|
||||
["lnpm", "插件管理"],
|
||||
Subcommand(
|
||||
"update",
|
||||
alias=["u"],
|
||||
@ -93,40 +91,54 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
|
||||
btn_install = md.button(ulang.get('npm.install'), 'lnpm install %s' % plugin.module_name)
|
||||
link_page = md.link(ulang.get('npm.homepage'), plugin.homepage)
|
||||
|
||||
reply += (f"\n{btn_install} | **{plugin.name}**\n"
|
||||
reply += (f"\n{btn_install} **{plugin.name}**\n"
|
||||
f"\n > **{plugin.desc}**\n"
|
||||
f"\n > {ulang.get('npm.author')}: {plugin.author} | {link_page}\n\n***\n")
|
||||
f"\n > {ulang.get('npm.author')}: {plugin.author} {link_page}\n\n***\n")
|
||||
if len(rs) > max_show:
|
||||
reply += f"\n{ulang.get('npm.too_many_results')}"
|
||||
reply += f"\n{ulang.get('npm.too_many_results', HIDE_NUM=len(rs) - max_show)}"
|
||||
else:
|
||||
reply = ulang.get("npm.search_no_result")
|
||||
await send_markdown(reply, bot, event=event)
|
||||
|
||||
elif result.subcommands.get("install"):
|
||||
plugin_name: str = result.subcommands["install"].args.get("plugin_name")
|
||||
r, log = npm_install(plugin_name)
|
||||
plugin_module_name: str = result.subcommands["install"].args.get("plugin_name")
|
||||
await npm_alc.send(ulang.get("npm.installing", NAME=plugin_module_name))
|
||||
r, log = npm_install(plugin_module_name)
|
||||
log = log.replace("\\", "/")
|
||||
if r:
|
||||
nonebot.load_plugin(plugin_name)
|
||||
installed_plugin = InstalledPlugin(module_name=plugin_name)
|
||||
store_plugin = await get_store_plugin(plugin_name)
|
||||
plugin_db.save(installed_plugin)
|
||||
await send_markdown(
|
||||
f"**{ulang.get('npm.install_success', NAME=store_plugin.name)}**\n\n"
|
||||
f"```\n{log}\n```",
|
||||
bot,
|
||||
event=event
|
||||
)
|
||||
nonebot.load_plugin(plugin_module_name) # 加载插件
|
||||
installed_plugin = InstalledPlugin(module_name=plugin_module_name) # 构造插件信息模型
|
||||
store_plugin = await get_store_plugin(plugin_module_name) # 获取商店中的插件信息
|
||||
found_in_db_plugin = plugin_db.first(InstalledPlugin, "module_name = ?", plugin_module_name) # 查询数据库中是否已经安装
|
||||
if found_in_db_plugin is None:
|
||||
plugin_db.save(installed_plugin)
|
||||
info = ulang.get('npm.install_success', NAME=store_plugin.name).replace('_', r'\\_') # markdown转义
|
||||
await send_markdown(
|
||||
f"{info}\n\n"
|
||||
f"```\n{log}\n```",
|
||||
bot,
|
||||
event=event
|
||||
)
|
||||
else:
|
||||
await npm_alc.finish(ulang.get('npm.plugin_already_installed', NAME=store_plugin.name))
|
||||
else:
|
||||
info = ulang.get('npm.install_failed', NAME=plugin_module_name).replace('_', r'\\_')
|
||||
await send_markdown(
|
||||
f"{ulang.get('npm.install_success', NAME=plugin_name)}\n\n"
|
||||
f"{info}\n\n"
|
||||
f"```\n{log}\n```",
|
||||
bot,
|
||||
event=event
|
||||
)
|
||||
|
||||
elif result.subcommands.get("remove"):
|
||||
plugin_name: str = result.subcommands["remove"].args.get("plugin_name")
|
||||
await npm_alc.finish(ulang.get("npm.remove_success"))
|
||||
plugin_module_name: str = result.subcommands["remove"].args.get("plugin_name")
|
||||
found_installed_plugin: InstalledPlugin = plugin_db.first(InstalledPlugin, "module_name = ?", plugin_module_name)
|
||||
if found_installed_plugin:
|
||||
plugin_db.delete(InstalledPlugin, "module_name = ?", plugin_module_name)
|
||||
reply = f"{ulang.get('npm.remove_success', NAME=found_installed_plugin.module_name)}"
|
||||
await npm_alc.finish(reply)
|
||||
else:
|
||||
await npm_alc.finish(ulang.get("npm.plugin_not_installed", NAME=plugin_module_name))
|
||||
|
||||
|
||||
async def npm_update() -> bool:
|
||||
@ -212,19 +224,25 @@ def npm_install(plugin_module_name) -> tuple[bool, str]:
|
||||
sys.stderr = buffer
|
||||
|
||||
mirrors = [
|
||||
"https://pypi.tuna.tsinghua.edu.cn/simple",
|
||||
"https://pypi.mirrors.cqupt.edu.cn/simple/",
|
||||
"https://pypi.org/simple",
|
||||
"https://pypi.mirrors.cqupt.edu.cn/simple", # 重庆邮电大学
|
||||
"https://pypi.tuna.tsinghua.edu.cn/simple", # 清华大学
|
||||
"https://pypi.liteyuki.icu/simple", # 轻雪镜像
|
||||
"https://pypi.org/simple", # 官方源
|
||||
]
|
||||
|
||||
# 使用pip安装包,对每个镜像尝试一次,成功后返回值
|
||||
success = False
|
||||
for mirror in mirrors:
|
||||
try:
|
||||
nonebot.logger.info(f"npm_install try mirror: {mirror}")
|
||||
result = pip.main(['install', plugin_module_name, "-i", mirror])
|
||||
success = result == 0
|
||||
break
|
||||
if success:
|
||||
break
|
||||
else:
|
||||
nonebot.logger.warning(f"npm_install failed, try next mirror.")
|
||||
except Exception as e:
|
||||
|
||||
success = False
|
||||
continue
|
||||
|
||||
|
Reference in New Issue
Block a user