mirror of
				https://github.com/LiteyukiStudio/LiteyukiBot.git
				synced 2025-10-31 18:26:23 +00:00 
			
		
		
		
	fix: npm对插件无法启用的bug
feat: 资源包的管理器
This commit is contained in:
		| @@ -16,9 +16,9 @@ from .common import * | ||||
| require("nonebot_plugin_alconna") | ||||
| from nonebot_plugin_alconna import Alconna, Args, Subcommand, on_alconna | ||||
|  | ||||
| npm_alc = on_alconna( | ||||
| nps = on_alconna( | ||||
|     Alconna( | ||||
|         "npm", | ||||
|         "nps", | ||||
|         Subcommand( | ||||
|             "update", | ||||
|             alias=["u"], | ||||
| @@ -36,19 +36,15 @@ npm_alc = on_alconna( | ||||
|         Subcommand( | ||||
|             "uninstall", | ||||
|             Args["plugin_name", str], | ||||
|             alias=["rm", "移除", "卸载"], | ||||
|         ), | ||||
|         Subcommand( | ||||
|             "list", | ||||
|             alias=["l", "ls", "列表"], | ||||
|             alias=["r", "rm", "卸载"], | ||||
|         ) | ||||
|     ), | ||||
|     aliases={"插件"}, | ||||
|     aliases={"插件商店"}, | ||||
|     permission=SUPERUSER, | ||||
| ) | ||||
|  | ||||
|  | ||||
| @npm_alc.handle() | ||||
| @nps.handle() | ||||
| async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): | ||||
|     ulang = get_user_lang(str(event.user_id)) | ||||
|  | ||||
| @@ -58,9 +54,9 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): | ||||
|     if result.subcommands.get("update"): | ||||
|         r = await npm_update() | ||||
|         if r: | ||||
|             await npm_alc.finish(ulang.get("npm.store_update_success")) | ||||
|             await nps.finish(ulang.get("npm.store_update_success")) | ||||
|         else: | ||||
|             await npm_alc.finish(ulang.get("npm.store_update_failed")) | ||||
|             await nps.finish(ulang.get("npm.store_update_failed")) | ||||
|  | ||||
|     elif result.subcommands.get("search"): | ||||
|         keywords: list[str] = result.subcommands["search"].args.get("keywords") | ||||
| @@ -71,9 +67,9 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): | ||||
|         if len(rs): | ||||
|             reply = f"{ulang.get('npm.search_result')} | {ulang.get('npm.total', TOTAL=len(rs))}\n***" | ||||
|             for plugin in rs[:min(max_show, len(rs))]: | ||||
|                 btn_install = md.cmd(ulang.get("npm.install"), "npm install %s" % plugin.module_name) | ||||
|                 link_page = md.link(ulang.get("npm.homepage"), plugin.homepage) | ||||
|                 link_pypi = md.link(ulang.get("npm.pypi"), plugin.homepage) | ||||
|                 btn_install = md.btn_cmd(ulang.get("npm.install"), "npm install %s" % plugin.module_name) | ||||
|                 link_page = md.btn_link(ulang.get("npm.homepage"), plugin.homepage) | ||||
|                 link_pypi = md.btn_link(ulang.get("npm.pypi"), plugin.homepage) | ||||
|  | ||||
|                 reply += (f"\n# **{plugin.name}**\n" | ||||
|                           f"\n> **{plugin.desc}**\n" | ||||
| @@ -89,14 +85,14 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): | ||||
|     elif result.subcommands.get("install"): | ||||
|         plugin_module_name: str = result.subcommands["install"].args.get("plugin_name") | ||||
|         store_plugin = await get_store_plugin(plugin_module_name) | ||||
|         await npm_alc.send(ulang.get("npm.installing", NAME=plugin_module_name)) | ||||
|         await nps.send(ulang.get("npm.installing", NAME=plugin_module_name)) | ||||
|         r, log = npm_install(plugin_module_name) | ||||
|         log = log.replace("\\", "/") | ||||
|  | ||||
|         if not store_plugin: | ||||
|             await npm_alc.finish(ulang.get("npm.plugin_not_found", NAME=plugin_module_name)) | ||||
|             await nps.finish(ulang.get("npm.plugin_not_found", NAME=plugin_module_name)) | ||||
|  | ||||
|         homepage_btn = md.cmd(ulang.get("npm.homepage"), store_plugin.homepage) | ||||
|         homepage_btn = md.btn_cmd(ulang.get("npm.homepage"), store_plugin.homepage) | ||||
|         if r: | ||||
|  | ||||
|             r_load = nonebot.load_plugin(plugin_module_name)  # 加载插件 | ||||
| @@ -114,7 +110,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): | ||||
|                         event=event | ||||
|                     ) | ||||
|                 else: | ||||
|                     await npm_alc.finish(ulang.get("npm.plugin_already_installed", NAME=store_plugin.name)) | ||||
|                     await nps.finish(ulang.get("npm.plugin_already_installed", NAME=store_plugin.name)) | ||||
|             else: | ||||
|                 info = ulang.get("npm.load_failed", NAME=plugin_module_name, HOMEPAGE=homepage_btn).replace("_", r"\\_") | ||||
|                 await md.send_md( | ||||
| @@ -138,9 +134,9 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): | ||||
|         if found_installed_plugin: | ||||
|             plugin_db.delete(InstalledPlugin(), "module_name = ?", plugin_module_name) | ||||
|             reply = f"{ulang.get('npm.uninstall_success', NAME=found_installed_plugin.module_name)}" | ||||
|             await npm_alc.finish(reply) | ||||
|             await nps.finish(reply) | ||||
|         else: | ||||
|             await npm_alc.finish(ulang.get("npm.plugin_not_installed", NAME=plugin_module_name)) | ||||
|             await nps.finish(ulang.get("npm.plugin_not_installed", NAME=plugin_module_name)) | ||||
|  | ||||
|  | ||||
| async def npm_update() -> bool: | ||||
|   | ||||
| @@ -19,7 +19,7 @@ from .installer import get_store_plugin, npm_update | ||||
| from ...utils.tools import clamp | ||||
|  | ||||
| require("nonebot_plugin_alconna") | ||||
| from nonebot_plugin_alconna import on_alconna, Alconna, Args, Arparma | ||||
| from nonebot_plugin_alconna import on_alconna, Alconna, Args, Arparma, Subcommand | ||||
|  | ||||
| list_plugins = on_alconna( | ||||
|     Alconna( | ||||
| @@ -29,27 +29,40 @@ list_plugins = on_alconna( | ||||
|     aliases={"列出插件", "插件列表"} | ||||
| ) | ||||
|  | ||||
| toggle_plugin = on_alconna( | ||||
|     Alconna( | ||||
|         "enable", | ||||
|         Args["plugin_name", str], | ||||
|     ), | ||||
|     aliases={"disable", "启用", "停用"} | ||||
| ) | ||||
| npm = on_alconna( | ||||
|     aliases={"插件管理"}, | ||||
|     command=Alconna( | ||||
|         "npm", | ||||
|         # Args["plugin_name", str], | ||||
|         Subcommand( | ||||
|             "enable", | ||||
|             Args["plugin_name", str], | ||||
|             alias=["启用"], | ||||
|  | ||||
| toggle_plugin_global = on_alconna( | ||||
|     Alconna( | ||||
|         "enable-global", | ||||
|         Args["plugin_name", str], | ||||
|         ), | ||||
|         Subcommand( | ||||
|             "disable", | ||||
|             Args["plugin_name", str], | ||||
|             alias=["停用"], | ||||
|         ), | ||||
|         Subcommand( | ||||
|             "global-enable", | ||||
|             Args["plugin_name", str], | ||||
|             alias=["全局启用"], | ||||
|         ), | ||||
|         Subcommand( | ||||
|             "global-disable", | ||||
|             Args["plugin_name", str], | ||||
|             alias=["全局停用"], | ||||
|         ), | ||||
|     ), | ||||
|     permission=SUPERUSER, | ||||
|     aliases={"disable-global", "全局启用", "全局停用"} | ||||
|  | ||||
| ) | ||||
|  | ||||
|  | ||||
| @list_plugins.handle() | ||||
| async def _(event: T_MessageEvent, bot: T_Bot, result: Arparma): | ||||
|     lang = get_user_lang(str(event.user_id)) | ||||
|     ulang = get_user_lang(str(event.user_id)) | ||||
|     if not os.path.exists("data/liteyuki/plugins.json"): | ||||
|         await npm_update() | ||||
|  | ||||
| @@ -60,31 +73,29 @@ async def _(event: T_MessageEvent, bot: T_Bot, result: Arparma): | ||||
|     page = clamp(result.args.get("page"), 1, total) | ||||
|  | ||||
|     # 已加载插件 | 总计10 | 第1/3页 | ||||
|     reply = (f"# {lang.get('npm.loaded_plugins')} | " | ||||
|              f"{lang.get('npm.total', TOTAL=len(nonebot.get_loaded_plugins()))} | " | ||||
|              f"{lang.get('npm.page', PAGE=page, TOTAL=total)} \n***\n") | ||||
|     reply = (f"# {ulang.get('npm.loaded_plugins')} | " | ||||
|              f"{ulang.get('npm.total', TOTAL=len(nonebot.get_loaded_plugins()))} | " | ||||
|              f"{ulang.get('npm.page', PAGE=page, TOTAL=total)} \n***\n") | ||||
|  | ||||
|     for plugin in loaded_plugin_list[(page - 1) * num_per_page: min(page * num_per_page, len(loaded_plugin_list))]: | ||||
|         # 检查是否有 metadata 属性 | ||||
|         # 添加帮助按钮 | ||||
|         btn_usage = md.cmd(lang.get("npm.usage"), f"help {plugin.module_name}", False) | ||||
|         btn_usage = md.btn_cmd(ulang.get("npm.usage"), f"help {plugin.module_name}", False) | ||||
|         store_plugin = await get_store_plugin(plugin.module_name) | ||||
|  | ||||
|         session_enable = get_plugin_session_enable(event, plugin.module_name) | ||||
|  | ||||
|         if store_plugin: | ||||
|             btn_homepage = md.link(lang.get("npm.homepage"), store_plugin.homepage) | ||||
|             btn_homepage = md.btn_link(ulang.get("npm.homepage"), store_plugin.homepage) | ||||
|             show_name = store_plugin.name | ||||
|         elif plugin.metadata: | ||||
|             if plugin.metadata.extra.get("liteyuki"): | ||||
|                 btn_homepage = md.link(lang.get("npm.homepage"), "https://github.com/snowykami/LiteyukiBot") | ||||
|                 btn_homepage = md.btn_link(ulang.get("npm.homepage"), "https://github.com/snowykami/LiteyukiBot") | ||||
|             else: | ||||
|                 btn_homepage = lang.get("npm.homepage") | ||||
|                 btn_homepage = ulang.get("npm.homepage") | ||||
|             show_name = plugin.metadata.name | ||||
|         else: | ||||
|             btn_homepage = lang.get("npm.homepage") | ||||
|             btn_homepage = ulang.get("npm.homepage") | ||||
|             show_name = plugin.name | ||||
|             lang.get("npm.no_description") | ||||
|             ulang.get("npm.no_description") | ||||
|  | ||||
|         if plugin.metadata: | ||||
|             reply += f"\n**{md.escape(show_name)}**\n" | ||||
| @@ -95,11 +106,10 @@ async def _(event: T_MessageEvent, bot: T_Bot, result: Arparma): | ||||
|  | ||||
|         if await GROUP_ADMIN(bot, event) or await GROUP_OWNER(bot, event) or await SUPERUSER(bot, event): | ||||
|             # 添加启用/停用插件按钮 | ||||
|             cmd_toggle = f"{'disable' if session_enable else 'enable'} {plugin.module_name}" | ||||
|             text_toggle = lang.get("npm.disable" if session_enable else "npm.enable") | ||||
|             cmd_toggle = f"npm {'disable' if session_enable else 'enable'} {plugin.module_name}" | ||||
|             text_toggle = ulang.get("npm.disable" if session_enable else "npm.enable") | ||||
|             can_be_toggle = get_plugin_can_be_toggle(plugin.module_name) | ||||
|             btn_toggle = text_toggle if not can_be_toggle else md.cmd(text_toggle, cmd_toggle) | ||||
|  | ||||
|             btn_toggle = text_toggle if not can_be_toggle else md.btn_cmd(text_toggle, cmd_toggle) | ||||
|             reply += f"  {btn_toggle}" | ||||
|  | ||||
|             if await SUPERUSER(bot, event): | ||||
| @@ -107,20 +117,18 @@ async def _(event: T_MessageEvent, bot: T_Bot, result: Arparma): | ||||
|                 # 添加移除插件和全局切换按钮 | ||||
|                 global_enable = get_plugin_global_enable(plugin.module_name) | ||||
|                 btn_uninstall = ( | ||||
|                         md.cmd(lang.get("npm.uninstall"), f'npm uninstall {plugin.module_name}')) if plugin_in_database else lang.get( | ||||
|                         md.btn_cmd(ulang.get("npm.uninstall"), f'npm uninstall {plugin.module_name}')) if plugin_in_database else ulang.get( | ||||
|                     'npm.uninstall') | ||||
|  | ||||
|                 btn_toggle_global_text = lang.get("npm.disable_global" if global_enable else "npm.enable_global") | ||||
|                 cmd_toggle_global = f"{'disable-global' if global_enable else 'enable-global'} {plugin.module_name}" | ||||
|                 btn_toggle_global = btn_toggle_global_text if not can_be_toggle else md.cmd(btn_toggle_global_text, cmd_toggle_global) | ||||
|                 btn_toggle_global_text = ulang.get("npm.disable_global" if global_enable else "npm.enable_global") | ||||
|                 cmd_toggle_global = f"npm {'global-disable' if global_enable else 'global-enable'} {plugin.module_name}" | ||||
|                 btn_toggle_global = btn_toggle_global_text if not can_be_toggle else md.btn_cmd(btn_toggle_global_text, cmd_toggle_global) | ||||
|  | ||||
|                 reply += f"  {btn_uninstall}  {btn_toggle_global}" | ||||
|  | ||||
|         reply += "\n\n***\n" | ||||
|     await md.send_md(reply, bot, event=event) | ||||
|  | ||||
|  | ||||
| @toggle_plugin.handle() | ||||
| @npm.handle() | ||||
| async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): | ||||
|     if not os.path.exists("data/liteyuki/plugins.json"): | ||||
|         await npm_update() | ||||
| @@ -128,103 +136,96 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): | ||||
|     ulang = get_user_lang(str(event.user_id)) | ||||
|     plugin_module_name = result.args.get("plugin_name") | ||||
|     # 支持对自定义command_start的判断 | ||||
|     toggle = result.header_result in [prefix + header for prefix in bot.config.command_start for header in ["enable-plugin", "启用"]]  # 判断是启用还是停用 | ||||
|     if result.subcommands.get("enable") or result.subcommands.get("disable"): | ||||
|  | ||||
|     session_enable = get_plugin_session_enable(event, plugin_module_name)  # 获取插件当前状态 | ||||
|         toggle = result.subcommands.get("enable") is not None | ||||
|  | ||||
|     default_enable = get_plugin_default_enable(plugin_module_name)  # 获取插件默认状态 | ||||
|         session_enable = get_plugin_session_enable(event, plugin_module_name)  # 获取插件当前状态 | ||||
|  | ||||
|     can_be_toggled = get_plugin_can_be_toggle(plugin_module_name)  # 获取插件是否可以被启用/停用 | ||||
|         default_enable = get_plugin_default_enable(plugin_module_name)  # 获取插件默认状态 | ||||
|  | ||||
|     if not can_be_toggled: | ||||
|         await toggle_plugin.finish(ulang.get("npm.plugin_cannot_be_toggled", NAME=plugin_module_name)) | ||||
|         can_be_toggled = get_plugin_can_be_toggle(plugin_module_name)  # 获取插件是否可以被启用/停用 | ||||
|  | ||||
|     if session_enable == toggle: | ||||
|         await toggle_plugin.finish( | ||||
|             ulang.get("npm.plugin_already", NAME=plugin_module_name, STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"))) | ||||
|         if not can_be_toggled: | ||||
|             await npm.finish(ulang.get("npm.plugin_cannot_be_toggled", NAME=plugin_module_name)) | ||||
|  | ||||
|         if session_enable == toggle: | ||||
|             await npm.finish( | ||||
|                 ulang.get("npm.plugin_already", NAME=plugin_module_name, STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"))) | ||||
|  | ||||
|     if event.message_type == "private": | ||||
|         session = user_db.first(User(), "user_id = ?", event.user_id, default=User(user_id=event.user_id)) | ||||
|     else: | ||||
|         if await GROUP_ADMIN(bot, event) or await GROUP_OWNER(bot, event) or await SUPERUSER(bot, event): | ||||
|             session = group_db.first(Group(), "group_id = ?", event.group_id, default=Group(group_id=str(event.group_id))) | ||||
|         else: | ||||
|             raise FinishedException(ulang.get("Permission Denied")) | ||||
|     try: | ||||
|         if toggle: | ||||
|             if default_enable: | ||||
|                 session.disabled_plugins.remove(plugin_module_name) | ||||
|             else: | ||||
|                 session.enabled_plugins.append(plugin_module_name) | ||||
|         else: | ||||
|             if default_enable: | ||||
|                 session.disabled_plugins.append(plugin_module_name) | ||||
|             else: | ||||
|                 session.enabled_plugins.remove(plugin_module_name) | ||||
|         if event.message_type == "private": | ||||
|             user_db.upsert(session) | ||||
|             session = user_db.first(User(), "user_id = ?", event.user_id, default=User(user_id=event.user_id)) | ||||
|         else: | ||||
|             group_db.upsert(session) | ||||
|     except Exception as e: | ||||
|         print(e) | ||||
|         await toggle_plugin.finish( | ||||
|             if await GROUP_ADMIN(bot, event) or await GROUP_OWNER(bot, event) or await SUPERUSER(bot, event): | ||||
|                 session = group_db.first(Group(), "group_id = ?", event.group_id, default=Group(group_id=str(event.group_id))) | ||||
|             else: | ||||
|                 raise FinishedException(ulang.get("Permission Denied")) | ||||
|         try: | ||||
|             if toggle: | ||||
|                 if default_enable: | ||||
|                     session.disabled_plugins.remove(plugin_module_name) | ||||
|                 else: | ||||
|                     session.enabled_plugins.append(plugin_module_name) | ||||
|             else: | ||||
|                 if default_enable: | ||||
|                     session.disabled_plugins.append(plugin_module_name) | ||||
|                 else: | ||||
|                     session.enabled_plugins.remove(plugin_module_name) | ||||
|             if event.message_type == "private": | ||||
|                 user_db.upsert(session) | ||||
|             else: | ||||
|                 group_db.upsert(session) | ||||
|         except Exception as e: | ||||
|             print(e) | ||||
|             await npm.finish( | ||||
|                 ulang.get( | ||||
|                     "npm.toggle_failed", | ||||
|                     NAME=plugin_module_name, | ||||
|                     STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"), | ||||
|                     ERROR=str(e)) | ||||
|             ) | ||||
|  | ||||
|         await npm.finish( | ||||
|             ulang.get( | ||||
|                 "npm.toggle_failed", | ||||
|                 "npm.toggle_success", | ||||
|                 NAME=plugin_module_name, | ||||
|                 STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"), | ||||
|                 ERROR=str(e)) | ||||
|                 STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable")) | ||||
|         ) | ||||
|     elif result.subcommands.get("global-enable") or result.subcommands.get("global-disable") and await SUPERUSER(bot, event): | ||||
|         toggle = result.subcommands.get("global-enable") is not None | ||||
|         can_be_toggled = get_plugin_can_be_toggle(plugin_module_name) | ||||
|         if not can_be_toggled: | ||||
|             await npm.finish(ulang.get("npm.plugin_cannot_be_toggled", NAME=plugin_module_name)) | ||||
|  | ||||
|     await toggle_plugin.finish( | ||||
|         ulang.get( | ||||
|             "npm.toggle_success", | ||||
|             NAME=plugin_module_name, | ||||
|             STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable")) | ||||
|     ) | ||||
|         global_enable = get_plugin_global_enable(plugin_module_name) | ||||
|         if global_enable == toggle: | ||||
|             await npm.finish( | ||||
|                 ulang.get("npm.plugin_already", NAME=plugin_module_name, STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"))) | ||||
|  | ||||
|         try: | ||||
|             plugin = plugin_db.first(GlobalPlugin(), "module_name = ?", plugin_module_name, default=GlobalPlugin(module_name=plugin_module_name)) | ||||
|             if toggle: | ||||
|                 plugin.enabled = True | ||||
|             else: | ||||
|                 plugin.enabled = False | ||||
|             plugin_db.upsert(plugin) | ||||
|         except Exception as e: | ||||
|             print(e) | ||||
|             await npm.finish( | ||||
|                 ulang.get( | ||||
|                     "npm.toggle_failed", | ||||
|                     NAME=plugin_module_name, | ||||
|                     STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"), | ||||
|                     ERROR=str(e)) | ||||
|             ) | ||||
|  | ||||
| @toggle_plugin_global.handle() | ||||
| async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): | ||||
|     if not os.path.exists("data/liteyuki/plugins.json"): | ||||
|         await npm_update() | ||||
|     # 判断会话类型 | ||||
|     ulang = get_user_lang(str(event.user_id)) | ||||
|     plugin_module_name = result.args.get("plugin_name") | ||||
|     # 支持对自定义command_start的判断 | ||||
|     toggle = result.header_result in [prefix + header for prefix in bot.config.command_start for header in ["enable-global", "全局启用"]] | ||||
|     can_be_toggled = get_plugin_can_be_toggle(plugin_module_name) | ||||
|     if not can_be_toggled: | ||||
|         await toggle_plugin_global.finish(ulang.get("npm.plugin_cannot_be_toggled", NAME=plugin_module_name)) | ||||
|  | ||||
|     global_enable = get_plugin_global_enable(plugin_module_name) | ||||
|     if global_enable == toggle: | ||||
|         await toggle_plugin_global.finish( | ||||
|             ulang.get("npm.plugin_already", NAME=plugin_module_name, STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"))) | ||||
|  | ||||
|     try: | ||||
|         plugin = plugin_db.first(GlobalPlugin(), "module_name = ?", plugin_module_name, default=GlobalPlugin(module_name=plugin_module_name)) | ||||
|         if toggle: | ||||
|             plugin.enabled = True | ||||
|         else: | ||||
|             plugin.enabled = False | ||||
|         plugin_db.upsert(plugin) | ||||
|     except Exception as e: | ||||
|         print(e) | ||||
|         await toggle_plugin_global.finish( | ||||
|         await npm.finish( | ||||
|             ulang.get( | ||||
|                 "npm.toggle_failed", | ||||
|                 "npm.toggle_success", | ||||
|                 NAME=plugin_module_name, | ||||
|                 STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable"), | ||||
|                 ERROR=str(e)) | ||||
|                 STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable")) | ||||
|         ) | ||||
|  | ||||
|     await toggle_plugin_global.finish( | ||||
|         ulang.get( | ||||
|             "npm.toggle_success", | ||||
|             NAME=plugin_module_name, | ||||
|             STATUS=ulang.get("npm.enable") if toggle else ulang.get("npm.disable")) | ||||
|     ) | ||||
|  | ||||
|  | ||||
| @run_preprocessor | ||||
| async def pre_handle(event: Event, matcher: Matcher): | ||||
|   | ||||
| @@ -1,18 +1,173 @@ | ||||
| # 轻雪资源包管理器 | ||||
| import os | ||||
|  | ||||
| import yaml | ||||
| from nonebot import require | ||||
| from nonebot.permission import SUPERUSER | ||||
| from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand, Arparma | ||||
|  | ||||
| from liteyuki.utils.ly_typing import T_Bot | ||||
| from liteyuki.utils.language import get_user_lang | ||||
| from liteyuki.utils.ly_typing import T_Bot, T_MessageEvent | ||||
| from liteyuki.utils.message import Markdown as md | ||||
| from liteyuki.utils.resource import (ResourceMetadata, add_resource_pack, change_priority, check_exist, check_status, get_loaded_resource_packs, get_resource_metadata, load_resources, remove_resource_pack) | ||||
|  | ||||
| list_rp = on_alconna( | ||||
|     aliases={"列出资源包", "资源包列表"}, | ||||
| require("nonebot_plugin_alconna") | ||||
| from nonebot_plugin_alconna import Alconna, Args, on_alconna, Arparma, Subcommand | ||||
|  | ||||
| rpm = on_alconna( | ||||
|     aliases={"资源包"}, | ||||
|     command=Alconna( | ||||
|         "list", | ||||
|         Args["page", int, 1]["num", int, 10], | ||||
|         "rpm", | ||||
|         Subcommand( | ||||
|             "list", | ||||
|             Args["page", int, 1]["num", int, 10], | ||||
|             alias=["ls", "列表", "列出"], | ||||
|         ), | ||||
|         Subcommand( | ||||
|             "load", | ||||
|             Args["name", str], | ||||
|             alias=["安装"], | ||||
|         ), | ||||
|         Subcommand( | ||||
|             "unload", | ||||
|             Args["name", str], | ||||
|             alias=["卸载"], | ||||
|         ), | ||||
|         Subcommand( | ||||
|             "up", | ||||
|             Args["name", str], | ||||
|             alias=["上移"], | ||||
|         ), | ||||
|         Subcommand( | ||||
|             "down", | ||||
|             Args["name", str], | ||||
|             alias=["下移"], | ||||
|         ), | ||||
|         Subcommand( | ||||
|             "top", | ||||
|             Args["name", str], | ||||
|             alias=["置顶"], | ||||
|         ), | ||||
|         Subcommand( | ||||
|             "reload", | ||||
|             alias=["重载"], | ||||
|         ), | ||||
|     ), | ||||
|     permission=SUPERUSER | ||||
| ) | ||||
|  | ||||
| @list_rp.handle() | ||||
| async def _(bot: T_Bot): | ||||
|     pass | ||||
|  | ||||
| @rpm.handle() | ||||
| async def _(bot: T_Bot, event: T_MessageEvent, result: Arparma): | ||||
|     ulang = get_user_lang(str(event.user_id)) | ||||
|     reply = "" | ||||
|     if result.subcommands.get("list"): | ||||
|         loaded_rps = get_loaded_resource_packs() | ||||
|         reply += f"{ulang.get('liteyuki.loaded_resources', NUM=len(loaded_rps))}\n" | ||||
|         for rp in loaded_rps: | ||||
|             btn_unload = md.btn_cmd( | ||||
|                 ulang.get("npm.uninstall"), | ||||
|                 f"rpm unload {rp.folder}" | ||||
|             ) | ||||
|             btn_move_up = md.btn_cmd( | ||||
|                 ulang.get("rpm.move_up"), | ||||
|                 f"rpm up {rp.folder}" | ||||
|             ) | ||||
|             btn_move_down = md.btn_cmd( | ||||
|                 ulang.get("rpm.move_down"), | ||||
|                 f"rpm down {rp.folder}" | ||||
|             ) | ||||
|             btn_move_top = md.btn_cmd( | ||||
|                 ulang.get("rpm.move_top"), | ||||
|                 f"rpm top {rp.folder}" | ||||
|             ) | ||||
|             # 添加新行 | ||||
|             reply += (f"\n**{md.escape(rp.name)}**({md.escape(rp.folder)})\n\n" | ||||
|                       f"> {btn_move_up} {btn_move_down} {btn_move_top} {btn_unload}\n\n***") | ||||
|         reply += f"\n\n{ulang.get('liteyuki.unloaded_resources')}\n" | ||||
|         loaded_folders = [rp.folder for rp in get_loaded_resource_packs()] | ||||
|         for folder in os.listdir("resources"): | ||||
|             if folder not in loaded_folders and os.path.exists(os.path.join("resources", folder, "metadata.yml")): | ||||
|                 metadata = ResourceMetadata( | ||||
|                     **yaml.load( | ||||
|                         open( | ||||
|                             os.path.join("resources", folder, "metadata.yml"), | ||||
|                             encoding="utf-8" | ||||
|                         ), | ||||
|                         Loader=yaml.FullLoader | ||||
|                     ) | ||||
|                 ) | ||||
|                 metadata.folder = folder | ||||
|                 metadata.path = os.path.join("resources", folder) | ||||
|                 btn_load = md.btn_cmd( | ||||
|                     ulang.get("npm.install"), | ||||
|                     f"rpm load {metadata.folder}" | ||||
|                 ) | ||||
|                 # 添加新行 | ||||
|                 reply += (f"\n**{md.escape(metadata.name)}**({md.escape(metadata.folder)})\n\n" | ||||
|                           f"> {btn_load}\n\n***") | ||||
|     elif result.subcommands.get("load") or result.subcommands.get("unload"): | ||||
|         load = result.subcommands.get("load") is not None | ||||
|         rp_name = result.args.get("name") | ||||
|         r = False  # 操作结果 | ||||
|         if check_exist(rp_name): | ||||
|             if load != check_status(rp_name): | ||||
|                 # 状态不同 | ||||
|                 if load: | ||||
|                     r = add_resource_pack(rp_name) | ||||
|                 else: | ||||
|                     r = remove_resource_pack(rp_name) | ||||
|                 rp_meta = get_resource_metadata(rp_name) | ||||
|                 reply += ulang.get( | ||||
|                     f"liteyuki.{'load' if load else 'unload'}_resource_{'success' if r else 'failed'}", | ||||
|                     NAME=rp_meta.name | ||||
|                 ) | ||||
|             else: | ||||
|                 # 重复操作 | ||||
|                 reply += ulang.get(f"liteyuki.resource_already_{'load' if load else 'unload'}ed", NAME=rp_name) | ||||
|         else: | ||||
|             reply += ulang.get("liteyuki.resource_not_found", NAME=rp_name) | ||||
|         if r: | ||||
|             btn_reload = md.btn_cmd( | ||||
|                 ulang.get("liteyuki.reload_resources"), | ||||
|                 f"rpm reload" | ||||
|             ) | ||||
|             reply += "\n" + ulang.get("liteyuki.need_reload", BTN=btn_reload) | ||||
|     elif result.subcommands.get("up") or result.subcommands.get("down") or result.subcommands.get("top"): | ||||
|         rp_name = result.args.get("name") | ||||
|         if result.subcommands.get("up"): | ||||
|             delta = -1 | ||||
|         elif result.subcommands.get("down"): | ||||
|             delta = 1 | ||||
|         else: | ||||
|             delta = 0 | ||||
|         if check_exist(rp_name): | ||||
|             if check_status(rp_name): | ||||
|                 r = change_priority(rp_name, delta) | ||||
|                 reply += ulang.get(f"liteyuki.change_priority_{'success' if r else 'failed'}", NAME=rp_name) | ||||
|                 if r: | ||||
|                     btn_reload = md.btn_cmd( | ||||
|                         ulang.get("liteyuki.reload_resources"), | ||||
|                         f"rpm reload" | ||||
|                     ) | ||||
|                     reply += "\n" + ulang.get("liteyuki.need_reload", BTN=btn_reload) | ||||
|             else: | ||||
|                 reply += ulang.get("liteyuki.resource_not_found", NAME=rp_name) | ||||
|         else: | ||||
|             reply += ulang.get("liteyuki.resource_not_found", NAME=rp_name) | ||||
|     elif result.subcommands.get("reload"): | ||||
|         load_resources() | ||||
|         reply = ulang.get( | ||||
|             "liteyuki.reload_resources_success", | ||||
|             NUM=len(get_loaded_resource_packs()) | ||||
|         ) | ||||
|     else: | ||||
|         btn_reload = md.btn_cmd( | ||||
|             ulang.get("liteyuki.reload_resources"), | ||||
|             f"rpm reload" | ||||
|         ) | ||||
|         btn_list = md.btn_cmd( | ||||
|             ulang.get("liteyuki.list_resources"), | ||||
|             f"rpm list" | ||||
|         ) | ||||
|         reply += f"{btn_list}    {btn_reload}" | ||||
|     await md.send_md(reply, bot, event=event) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user