mirror of
https://github.com/LiteyukiStudio/LiteyukiBot.git
synced 2024-11-11 05:17:24 +08:00
feat: 添加了对markdown的简单封装
This commit is contained in:
parent
15c751b1c8
commit
edf390ff43
@ -5,3 +5,6 @@ nonebot-adapter-onebot==2.4.3
|
||||
nonebot-plugin-alconna==0.41.0
|
||||
psutil==5.9.8
|
||||
pydantic==2.6.4
|
||||
PyYAML~=6.0.1
|
||||
typing_extensions~=4.10.0
|
||||
starlette~=0.36.3
|
@ -4,7 +4,8 @@ from nonebot.adapters.onebot.v11 import MessageSegment
|
||||
from nonebot.params import CommandArg
|
||||
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_group = on_command("mdg", aliases={"群md"}, permission=SUPERUSER)
|
||||
@ -18,80 +19,12 @@ placeholder = {
|
||||
"\"": r'\\\"'
|
||||
}
|
||||
|
||||
|
||||
@md_test.handle()
|
||||
async def _(bot: T_Bot, event: T_MessageEvent, arg: v11.Message = CommandArg()):
|
||||
arg = str(arg).replace("\\", "\\\\").replace("\n", "\\n")
|
||||
print(arg)
|
||||
for k, v in placeholder.items():
|
||||
arg = arg.replace(k, v)
|
||||
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 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
|
||||
await send_markdown(
|
||||
str(arg),
|
||||
bot,
|
||||
message_type=event.message_type,
|
||||
session_id=event.user_id if event.message_type == "private" else event.group_id
|
||||
)
|
@ -2,7 +2,7 @@ import nonebot.plugin
|
||||
from nonebot import on_command
|
||||
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
|
||||
|
||||
list_plugins = on_command("list-plugin", aliases={"列出插件"}, priority=0)
|
||||
|
@ -1,7 +1,7 @@
|
||||
from nonebot import on_command
|
||||
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.language import get_user_lang
|
||||
|
||||
|
45
src/utils/message.py
Normal file
45
src/utils/message.py
Normal 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
|
@ -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
|
||||
else:
|
||||
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
|
||||
|
@ -5,5 +5,4 @@ T_Bot = v11.Bot | v12.Bot
|
||||
T_GroupMessageEvent = v11.GroupMessageEvent | v12.GroupMessageEvent
|
||||
T_PrivateMessageEvent = v11.PrivateMessageEvent | v12.PrivateMessageEvent
|
||||
T_MessageEvent = v11.MessageEvent | v12.MessageEvent
|
||||
|
||||
T_Message = v11.Message | v12.Message
|
Loading…
Reference in New Issue
Block a user