From edf390ff439bd958443d09db3effe7d742e9366c Mon Sep 17 00:00:00 2001 From: snowyServer Date: Tue, 19 Mar 2024 21:56:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=AF=B9mar?= =?UTF-8?q?kdown=E7=9A=84=E7=AE=80=E5=8D=95=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 5 +- src/plugins/liteyuki_markdowntest.py | 87 +++---------------- src/plugins/liteyuki_plugin_npm/manager.py | 2 +- .../liteyuki_plugin_user/profile_manager.py | 2 +- src/utils/message.py | 45 ++++++++++ src/utils/tools.py | 13 +++ src/utils/{adapter.py => typing.py} | 1 - 7 files changed, 74 insertions(+), 81 deletions(-) create mode 100644 src/utils/message.py rename src/utils/{adapter.py => typing.py} (99%) diff --git a/requirements.txt b/requirements.txt index 4c3012d..90a3ea8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file +pydantic==2.6.4 +PyYAML~=6.0.1 +typing_extensions~=4.10.0 +starlette~=0.36.3 \ No newline at end of file diff --git a/src/plugins/liteyuki_markdowntest.py b/src/plugins/liteyuki_markdowntest.py index 72b73c9..1476b92 100644 --- a/src/plugins/liteyuki_markdowntest.py +++ b/src/plugins/liteyuki_markdowntest.py @@ -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) @@ -14,84 +15,16 @@ placeholder = { "]": "]", "&": "&", ",": ",", - "\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() -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 ) \ No newline at end of file diff --git a/src/plugins/liteyuki_plugin_npm/manager.py b/src/plugins/liteyuki_plugin_npm/manager.py index 8bd1519..ea4b8ac 100644 --- a/src/plugins/liteyuki_plugin_npm/manager.py +++ b/src/plugins/liteyuki_plugin_npm/manager.py @@ -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) diff --git a/src/plugins/liteyuki_plugin_user/profile_manager.py b/src/plugins/liteyuki_plugin_user/profile_manager.py index ba765a8..90347fe 100644 --- a/src/plugins/liteyuki_plugin_user/profile_manager.py +++ b/src/plugins/liteyuki_plugin_user/profile_manager.py @@ -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 diff --git a/src/utils/message.py b/src/utils/message.py new file mode 100644 index 0000000..200b57a --- /dev/null +++ b/src/utils/message.py @@ -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 diff --git a/src/utils/tools.py b/src/utils/tools.py index 190b63a..7b147ba 100644 --- a/src/utils/tools.py +++ b/src/utils/tools.py @@ -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 diff --git a/src/utils/adapter.py b/src/utils/typing.py similarity index 99% rename from src/utils/adapter.py rename to src/utils/typing.py index 5043ee7..d48c982 100644 --- a/src/utils/adapter.py +++ b/src/utils/typing.py @@ -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