diff --git a/.gitignore b/.gitignore index df0e3b4..820e189 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ compile.bat # nuitka main.build/ -main.exe \ No newline at end of file +main.exe +main.cmd \ No newline at end of file diff --git a/src/plugins/liteyuki_plugin_npm/installer.py b/src/plugins/liteyuki_plugin_npm/installer.py index e69de29..6e21aa4 100644 --- a/src/plugins/liteyuki_plugin_npm/installer.py +++ b/src/plugins/liteyuki_plugin_npm/installer.py @@ -0,0 +1,10 @@ +import pip + + +def install(plugin_name) -> bool: + try: + pip.main(['install', plugin_name]) + return True + except Exception as e: + print(e) + return False diff --git a/src/plugins/liteyuki_plugin_npm/manager.py b/src/plugins/liteyuki_plugin_npm/manager.py index 2c07e98..f954a42 100644 --- a/src/plugins/liteyuki_plugin_npm/manager.py +++ b/src/plugins/liteyuki_plugin_npm/manager.py @@ -2,7 +2,7 @@ import nonebot.plugin from nonebot import on_command from nonebot.permission import SUPERUSER -from src.utils.message import send_markdown +from src.utils.message import button, send_markdown from src.utils.typing import T_Bot, T_MessageEvent from src.utils.language import get_user_lang @@ -17,7 +17,9 @@ async def _(event: T_MessageEvent, bot: T_Bot): for plugin in nonebot.get_loaded_plugins(): # 检查是否有 metadata 属性 if plugin.metadata: - reply += f"\n- **{plugin.metadata.name}**".replace('_', r'\_') + reply += (f"\n{button(lang.get('npm.disable'), 'disable-plugin %s' % plugin.name, False, False)} **{plugin.metadata.name}**\n" + f"\n > {plugin.metadata.description}\n\n***\n") else: - reply += f"\n- **{plugin.name}**".replace('_', r'\_') + reply += (f"\n{button(lang.get('npm.disable'), 'disable-plugin %s' % plugin.name, False, False)} **{plugin.name}**\n" + f"\n > {lang.get('npm.no_description')}\n\n***\n") await send_markdown(reply, bot, event=event) diff --git a/src/resources/lang/en.lang b/src/resources/lang/en.lang index a359fea..b0e56ed 100644 --- a/src/resources/lang/en.lang +++ b/src/resources/lang/en.lang @@ -12,6 +12,9 @@ main.monitor.usage=Usage npm.loaded_plugins=Loaded plugins npm.total=Total {TOTAL} +npm.disable=Disable +npm.enable=Enable +npm.no_description=No description user.profile_manager.query=Your {ATTR} is {VALUE} user.profile_manager.set=Your {ATTR} has been set to {VALUE} \ No newline at end of file diff --git a/src/resources/lang/zh-CN.lang b/src/resources/lang/zh-CN.lang index bc6ada4..2d1563c 100644 --- a/src/resources/lang/zh-CN.lang +++ b/src/resources/lang/zh-CN.lang @@ -12,6 +12,9 @@ main.monitor.usage=使用率 npm.loaded_plugins=已加载插件 npm.total=总计 {TOTAL} +npm.disable=停用 +npm.enable=启用 +npm.no_description=无描述 user.profile_manager.query=你的个人信息 {ATTR} 为 {VALUE} user.profile_manager.set=你的个人信息 {ATTR} 已设置为 {VALUE} \ No newline at end of file diff --git a/src/utils/message.py b/src/utils/message.py index 24b9d1b..2e2d5c8 100644 --- a/src/utils/message.py +++ b/src/utils/message.py @@ -2,11 +2,11 @@ import nonebot from nonebot.adapters.onebot import v11, v12 from typing_extensions import Any -from .tools import de_escape +from .tools import de_escape, encode_url from .typing import T_Bot, T_MessageEvent -async def send_markdown(markdown: str, bot: T_Bot, *, message_type: str=None, session_id: str | int=None, event: T_MessageEvent=None) -> dict[str, Any]: +async def send_markdown(markdown: str, bot: T_Bot, *, message_type: str = None, session_id: str | int = None, event: T_MessageEvent = None) -> dict[str, Any]: formatted_md = de_escape(markdown).replace("\n", r"\n").replace("\"", r'\\\"') if event is not None and message_type is None: message_type = event.message_type @@ -71,3 +71,18 @@ async def send_markdown(markdown: str, bot: T_Bot, *, message_type: str=None, s nonebot.logger.error("send_markdown: bot type not supported") data = {} return data + + +def button(name: str, cmd: str, reply: bool = False, enter: bool = True) -> str: + """生成点击按钮的链接 + Args: + name: + cmd: + reply: 是否以回复的方式发送消息 + enter: 自动发送消息则为True + + Returns: + markdown格式的链接 + + """ + return f"[{name}](mqqapi://aio/inlinecmd?command={encode_url(cmd)}&reply={str(reply).lower()}&enter={str(enter).lower()})" diff --git a/src/utils/tools.py b/src/utils/tools.py index 7b147ba..62deac4 100644 --- a/src/utils/tools.py +++ b/src/utils/tools.py @@ -1,3 +1,6 @@ +from urllib.parse import quote + + def convert_size(size: int, precision: int = 2, add_unit: bool = True, suffix: str = "iB") -> str: """把字节数转换为人类可读的字符串,计算正负 @@ -33,12 +36,16 @@ def convert_size(size: int, precision: int = 2, add_unit: bool = True, suffix: s def de_escape(text: str) -> str: str_map = { - "[": "[", - "]": "]", - "&": "&", - ",": ",", + "[": "[", + "]": "]", + "&": "&", + ",": ",", } for k, v in str_map.items(): text = text.replace(k, v) return text + + +def encode_url(text: str) -> str: + return quote(text, safe="")