mirror of
https://github.com/TriM-Organization/LiteyukiBot-TriM.git
synced 2024-11-28 16:24:51 +08:00
feat: 对部分消息回复用markdown进行了重新排版
This commit is contained in:
parent
2b8cb2afb6
commit
14d9f041ce
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,3 +10,4 @@ compile.bat
|
||||
# nuitka
|
||||
main.build/
|
||||
main.exe
|
||||
main.cmd
|
@ -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
|
@ -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)
|
||||
|
@ -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}
|
@ -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}
|
@ -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()})"
|
||||
|
@ -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="")
|
||||
|
Loading…
Reference in New Issue
Block a user