app/src/plugins/liteyuki_plugin_npm/manager.py

42 lines
2.2 KiB
Python
Raw Normal View History

2024-03-18 18:21:56 +08:00
import nonebot.plugin
from nonebot import on_command
2024-03-19 00:27:40 +08:00
from nonebot.permission import SUPERUSER
2024-03-21 14:52:02 +08:00
from src.utils.data_manager import InstalledPlugin, plugin_db
from src.utils.message import Markdown as md, send_markdown
2024-03-21 14:52:02 +08:00
from src.utils.permission import GROUP_ADMIN, GROUP_OWNER
from src.utils.typing import T_Bot, T_MessageEvent
2024-03-18 18:21:56 +08:00
from src.utils.language import get_user_lang
2024-03-21 14:52:02 +08:00
list_plugins = on_command("list-plugin", aliases={"列出插件", "插件列表"}, priority=0)
toggle_plugin = on_command("enable-plugin", aliases={"启用插件", "停用插件", "disable-plugin"}, priority=0, permission=SUPERUSER)
2024-03-18 18:21:56 +08:00
@list_plugins.handle()
async def _(event: T_MessageEvent, bot: T_Bot):
2024-03-19 00:27:40 +08:00
lang = get_user_lang(str(event.user_id))
reply = f"# {lang.get('npm.loaded_plugins')} | {lang.get('npm.total', TOTAL=len(nonebot.get_loaded_plugins()))} \n***"
2024-03-18 18:21:56 +08:00
for plugin in nonebot.get_loaded_plugins():
2024-03-19 00:27:40 +08:00
# 检查是否有 metadata 属性
2024-03-21 14:52:02 +08:00
btn_help = md.button(lang.get('npm.help'), f'help {plugin.name}', False)
reply += f"\n{btn_help} "
2024-03-19 00:27:40 +08:00
if plugin.metadata:
2024-03-21 14:52:02 +08:00
reply += (f"**{plugin.metadata.name}**\n"
f"\n > {plugin.metadata.description}")
2024-03-19 00:27:40 +08:00
else:
2024-03-21 14:52:02 +08:00
reply += (f"**{plugin.name}**\n"
f"\n > {lang.get('npm.no_description')}")
# if await GROUP_ADMIN(bot=bot, event=event) or await GROUP_OWNER(bot=bot, event=event) or await SUPERUSER(bot=bot, event=event):
if await GROUP_ADMIN(bot, event) or await GROUP_OWNER(bot, event) or await SUPERUSER(bot, event):
btn_enable = md.button(lang.get('npm.enable'), f'enable-plugin {plugin.module_name}')
btn_disable = md.button(lang.get('npm.disable'), f'disable-plugin {plugin.module_name}')
reply += f"\n > {btn_enable} {btn_disable}"
if await SUPERUSER(bot, event):
plugin_in_database = plugin_db.first(InstalledPlugin, 'module_name = ?', plugin.module_name)
btn_remove = (
md.button(lang.get('npm.uninstall'), f'lnpm remove {plugin.module_name}')) if plugin_in_database else lang.get(
'npm.uninstall')
reply += f" {btn_remove}"
reply += "\n\n***\n"
await send_markdown(reply, bot, event=event)