Skip to content

Commit

Permalink
feat: 添加了对markdown的简单封装
Browse files Browse the repository at this point in the history
  • Loading branch information
snowykami committed Mar 19, 2024
1 parent 15c751b commit edf390f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 81 deletions.
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ nonebot2[fastapi]==2.2.1
nonebot-adapter-onebot==2.4.3
nonebot-plugin-alconna==0.41.0
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
87 changes: 10 additions & 77 deletions src/plugins/liteyuki_markdowntest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
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)
Expand All @@ -14,84 +15,16 @@
"]": "]",
"&": "&",
",": ",",
"\n" : r"\n",
"\"" : r'\\\"'
"\n": r"\n",
"\"": 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()
@md_test.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 changes: 1 addition & 1 deletion src/plugins/liteyuki_plugin_npm/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/liteyuki_plugin_user/profile_manager.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
45 changes: 45 additions & 0 deletions src/utils/message.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions src/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion src/utils/adapter.py → src/utils/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
T_GroupMessageEvent = v11.GroupMessageEvent | v12.GroupMessageEvent
T_PrivateMessageEvent = v11.PrivateMessageEvent | v12.PrivateMessageEvent
T_MessageEvent = v11.MessageEvent | v12.MessageEvent

T_Message = v11.Message | v12.Message

0 comments on commit edf390f

Please sign in to comment.