feat: 对部分消息回复用markdown进行了重新排版

This commit is contained in:
远野千束 2024-03-20 22:30:52 +08:00
parent 2b8cb2afb6
commit 14d9f041ce
7 changed files with 51 additions and 10 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ compile.bat
# nuitka # nuitka
main.build/ main.build/
main.exe main.exe
main.cmd

View File

@ -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

View File

@ -2,7 +2,7 @@ import nonebot.plugin
from nonebot import on_command from nonebot import on_command
from nonebot.permission import SUPERUSER 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.typing import T_Bot, T_MessageEvent
from src.utils.language import get_user_lang 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(): for plugin in nonebot.get_loaded_plugins():
# 检查是否有 metadata 属性 # 检查是否有 metadata 属性
if plugin.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: 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) await send_markdown(reply, bot, event=event)

View File

@ -12,6 +12,9 @@ main.monitor.usage=Usage
npm.loaded_plugins=Loaded plugins npm.loaded_plugins=Loaded plugins
npm.total=Total {TOTAL} 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.query=Your {ATTR} is {VALUE}
user.profile_manager.set=Your {ATTR} has been set to {VALUE} user.profile_manager.set=Your {ATTR} has been set to {VALUE}

View File

@ -12,6 +12,9 @@ main.monitor.usage=使用率
npm.loaded_plugins=已加载插件 npm.loaded_plugins=已加载插件
npm.total=总计 {TOTAL} npm.total=总计 {TOTAL}
npm.disable=停用
npm.enable=启用
npm.no_description=无描述
user.profile_manager.query=你的个人信息 {ATTR} 为 {VALUE} user.profile_manager.query=你的个人信息 {ATTR} 为 {VALUE}
user.profile_manager.set=你的个人信息 {ATTR} 已设置为 {VALUE} user.profile_manager.set=你的个人信息 {ATTR} 已设置为 {VALUE}

View File

@ -2,11 +2,11 @@ import nonebot
from nonebot.adapters.onebot import v11, v12 from nonebot.adapters.onebot import v11, v12
from typing_extensions import Any from typing_extensions import Any
from .tools import de_escape from .tools import de_escape, encode_url
from .typing import T_Bot, T_MessageEvent 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'\\\"') formatted_md = de_escape(markdown).replace("\n", r"\n").replace("\"", r'\\\"')
if event is not None and message_type is None: if event is not None and message_type is None:
message_type = event.message_type 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") nonebot.logger.error("send_markdown: bot type not supported")
data = {} data = {}
return 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()})"

View File

@ -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: 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: def de_escape(text: str) -> str:
str_map = { str_map = {
"[": "[", "[": "[",
"]": "]", "]": "]",
"&": "&", "&": "&",
",": ",", ",": ",",
} }
for k, v in str_map.items(): for k, v in str_map.items():
text = text.replace(k, v) text = text.replace(k, v)
return text return text
def encode_url(text: str) -> str:
return quote(text, safe="")