1
0
forked from bot/app

feat: 添加了对markdown的简单封装

This commit is contained in:
远野千束 2024-03-19 21:56:31 +08:00
parent 15c751b1c8
commit edf390ff43
7 changed files with 74 additions and 81 deletions

View File

@ -5,3 +5,6 @@ nonebot-adapter-onebot==2.4.3
nonebot-plugin-alconna==0.41.0 nonebot-plugin-alconna==0.41.0
psutil==5.9.8 psutil==5.9.8
pydantic==2.6.4 pydantic==2.6.4
PyYAML~=6.0.1
typing_extensions~=4.10.0
starlette~=0.36.3

View File

@ -4,7 +4,8 @@ from nonebot.adapters.onebot.v11 import MessageSegment
from nonebot.params import CommandArg from nonebot.params import CommandArg
from nonebot.permission import SUPERUSER from nonebot.permission import SUPERUSER
from src.utils.adapter import T_Message, T_Bot, v11, T_MessageEvent from src.utils.message import send_markdown
from src.utils.typing import T_Message, T_Bot, v11, T_MessageEvent
md_test = on_command("mdts", aliases={"会话md"}, permission=SUPERUSER) md_test = on_command("mdts", aliases={"会话md"}, permission=SUPERUSER)
md_group = on_command("mdg", aliases={"群md"}, permission=SUPERUSER) md_group = on_command("mdg", aliases={"群md"}, permission=SUPERUSER)
@ -14,84 +15,16 @@ placeholder = {
"]": "]", "]": "]",
"&": "&", "&": "&",
",": ",", ",": ",",
"\n" : r"\n", "\n": r"\n",
"\"" : r'\\\"' "\"": r'\\\"'
} }
@md_test.handle() @md_test.handle()
async def _(bot: T_Bot, event: T_MessageEvent, arg: v11.Message = CommandArg()): async def _(bot: T_Bot, event: T_MessageEvent, arg: v11.Message = CommandArg()):
arg = str(arg).replace("\\", "\\\\").replace("\n", "\\n") await send_markdown(
print(arg) str(arg),
for k, v in placeholder.items(): bot,
arg = arg.replace(k, v) message_type=event.message_type,
sfm = await bot.call_api( session_id=event.user_id if event.message_type == "private" else event.group_id
api="send_private_forward_msg",
user_id=bot.self_id,
messages=[
{
"type": "node",
"data": {
"name": "Liteyuki",
"uin": bot.self_id,
"content": [
{
"type": "markdown",
"data": {
"content": '{"content":"%s"}' % arg
}
}
]
},
},
]
)
await md_test.finish(
message=v11.Message(
MessageSegment(
type="longmsg",
data={
"id": sfm["forward_id"]
}
)
)
)
@md_group.handle()
async def _(bot: T_Bot, event: T_MessageEvent, arg: v11.Message = CommandArg()):
group_id, arg = str(arg).split(" ", 1)
print(arg)
for k, v in placeholder.items():
arg = arg.replace(k, v)
nonebot.logger.info("Markdown 测试")
sfm = await bot.call_api(
api="send_private_forward_msg",
user_id=bot.self_id,
messages=[
{
"type": "node",
"data": {
"name": "Liteyuki",
"uin": bot.self_id,
"content": [
{
"type": "markdown",
"data": {
"content": '{"content":"%s"}' % arg
}
}
]
},
},
]
)
await bot.send_group_msg(
message=v11.Message(
MessageSegment(
type="longmsg",
data={
"id": sfm["forward_id"]
}
)
),
group_id=group_id
) )

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.adapter import T_MessageEvent from src.utils.typing import T_MessageEvent
from src.utils.language import get_user_lang from src.utils.language import get_user_lang
list_plugins = on_command("list-plugin", aliases={"列出插件"}, priority=0) list_plugins = on_command("list-plugin", aliases={"列出插件"}, priority=0)

View File

@ -1,7 +1,7 @@
from nonebot import on_command from nonebot import on_command
from nonebot.params import CommandArg from nonebot.params import CommandArg
from src.utils.adapter import T_Bot, T_Message, T_MessageEvent from src.utils.typing import T_Bot, T_Message, T_MessageEvent
from src.utils.data_manager import User, user_db from src.utils.data_manager import User, user_db
from src.utils.language import get_user_lang from src.utils.language import get_user_lang

45
src/utils/message.py Normal file
View File

@ -0,0 +1,45 @@
import nonebot
from nonebot.adapters.onebot import v11, v12
from typing_extensions import Any
from .typing import T_Bot
async def send_markdown(markdown: str, bot: T_Bot, message_type: str, session_id: str) -> dict[str, Any]:
markdown = markdown.replace("\n", r"\n").replace("\"", r'\\\"')
forward_data = await bot.call_api(
api="send_private_forward_msg",
user_id=bot.self_id,
messages=[
v11.MessageSegment(
type="node",
data={
"name": "Liteyuki.OneBot",
"uin": bot.self_id,
"content": [
{
"type": "markdown",
"data": {
"content": '{"content":"%s"}' % markdown
}
}
]
},
)
]
)
data = await bot.send_msg(
message_type=message_type,
message=[{
"type": "longmsg",
"data": {
"id": forward_data["forward_id"]
}
}],
user_id=session_id if message_type == "private" else None,
group_id=session_id if message_type == "group" else None
)
nonebot.logger.info("已发送md%s" % forward_data["forward_id"])
return data

View File

@ -29,3 +29,16 @@ def convert_size(size: int, precision: int = 2, add_unit: bool = True, suffix: s
return f"{size:.{precision}f} Y" + suffix return f"{size:.{precision}f} Y" + suffix
else: else:
return f"{size:.{precision}f}" return f"{size:.{precision}f}"
def de_escape(text: str) -> str:
str_map = {
"[": "[",
"]": "]",
"&": "&",
",": ",",
}
for k, v in str_map.items():
text = text.replace(k, v)
return text

View File

@ -5,5 +5,4 @@ T_Bot = v11.Bot | v12.Bot
T_GroupMessageEvent = v11.GroupMessageEvent | v12.GroupMessageEvent T_GroupMessageEvent = v11.GroupMessageEvent | v12.GroupMessageEvent
T_PrivateMessageEvent = v11.PrivateMessageEvent | v12.PrivateMessageEvent T_PrivateMessageEvent = v11.PrivateMessageEvent | v12.PrivateMessageEvent
T_MessageEvent = v11.MessageEvent | v12.MessageEvent T_MessageEvent = v11.MessageEvent | v12.MessageEvent
T_Message = v11.Message | v12.Message T_Message = v11.Message | v12.Message