From 598bff8c49a69982d3bd27773693ef108cfa0e54 Mon Sep 17 00:00:00 2001 From: snowy Date: Tue, 20 Aug 2024 20:38:10 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=E6=93=B4=E5=B1=95event=E5=AD=97?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteyuki/message/matcher.py | 2 +- src/liteyuki_main/core.py | 2 +- .../liteyuki_minigame/minesweeper.py | 8 ++--- src/nonebot_plugins/liteyuki_pacman/npm.py | 16 +++++----- src/nonebot_plugins/liteyuki_pacman/rpm.py | 2 +- .../liteyuki_user/profile_manager.py | 4 +-- .../nonebot-plugin-toliteyuki/__init__.py | 18 ----------- src/utils/message/message.py | 30 ++++++++----------- 8 files changed, 30 insertions(+), 52 deletions(-) delete mode 100644 src/nonebot_plugins/nonebot-plugin-toliteyuki/__init__.py diff --git a/liteyuki/message/matcher.py b/liteyuki/message/matcher.py index 4e54d0f9..5585fa98 100644 --- a/liteyuki/message/matcher.py +++ b/liteyuki/message/matcher.py @@ -45,7 +45,7 @@ class Matcher: self.handlers.append(handler) return handler - async def run(self, event: Event) -> None: + async def run(self, event: MessageEvent) -> None: """ 运行处理函数 Args: diff --git a/src/liteyuki_main/core.py b/src/liteyuki_main/core.py index 4c02a16a..01d1b384 100644 --- a/src/liteyuki_main/core.py +++ b/src/liteyuki_main/core.py @@ -64,7 +64,7 @@ async def _(bot: T_Bot, event: T_MessageEvent): btn_restart = md.btn_cmd(ulang.get("liteyuki.restart_now"), "reload-liteyuki") pip.main(["install", "-r", "requirements.txt"]) reply += f"{ulang.get('liteyuki.update_restart', RESTART=btn_restart)}" - await md.send_md(reply, bot, event=event, at_sender=False) + await md.send_md(reply, bot) @on_alconna( diff --git a/src/nonebot_plugins/liteyuki_minigame/minesweeper.py b/src/nonebot_plugins/liteyuki_minigame/minesweeper.py index 715bb7a6..1fb3ba27 100644 --- a/src/nonebot_plugins/liteyuki_minigame/minesweeper.py +++ b/src/nonebot_plugins/liteyuki_minigame/minesweeper.py @@ -64,7 +64,7 @@ async def _(event: T_MessageEvent, result: Arparma, bot: T_Bot): ) minesweeper_cache.append(new_game) await minesweeper.send("游戏开始") - await md.send_md(new_game.board_markdown(), bot, event=event) + await md.send_md(new_game.board_markdown(), bot) except AssertionError: await minesweeper.finish("参数错误") elif result.subcommands.get("end"): @@ -83,9 +83,9 @@ async def _(event: T_MessageEvent, result: Arparma, bot: T_Bot): await minesweeper.finish("参数错误") if not game.reveal(row, col): minesweeper_cache.remove(game) - await md.send_md(game.board_markdown(), bot, event=event) + await md.send_md(game.board_markdown(), bot) await minesweeper.finish("游戏结束") - await md.send_md(game.board_markdown(), bot, event=event) + await md.send_md(game.board_markdown(), bot) if game.is_win(): minesweeper_cache.remove(game) await minesweeper.finish("游戏胜利") @@ -98,6 +98,6 @@ async def _(event: T_MessageEvent, result: Arparma, bot: T_Bot): if not (0 <= row < game.rows and 0 <= col < game.cols): await minesweeper.finish("参数错误") game.board[row][col].flagged = not game.board[row][col].flagged - await md.send_md(game.board_markdown(), bot, event=event) + await md.send_md(game.board_markdown(), bot) else: await minesweeper.finish("参数错误") diff --git a/src/nonebot_plugins/liteyuki_pacman/npm.py b/src/nonebot_plugins/liteyuki_pacman/npm.py index 24e0dfad..3aaf08ca 100644 --- a/src/nonebot_plugins/liteyuki_pacman/npm.py +++ b/src/nonebot_plugins/liteyuki_pacman/npm.py @@ -292,7 +292,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher): reply += f"\n{ulang.get('npm.too_many_results', HIDE_NUM=len(rs) - max_show)}" else: reply = ulang.get("npm.search_no_result") - await md.send_md(reply, bot, event=event) + await md.send_md(reply, bot) elif sc.get("install") and perm_s: plugin_name: str = result.subcommands["install"].args.get("plugin_name") @@ -320,7 +320,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher): info = md.escape( ulang.get("npm.install_success", NAME=store_plugin.name) ) # markdown转义 - await md.send_md(f"{info}\n\n" f"```\n{log}\n```", bot, event=event) + await md.send_md(f"{info}\n\n" f"```\n{log}\n```", bot) else: await npm.finish( ulang.get( @@ -331,12 +331,12 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher): info = ulang.get( "npm.load_failed", NAME=plugin_name, HOMEPAGE=homepage_btn ).replace("_", r"\\_") - await md.send_md(f"{info}\n\n" f"```\n{log}\n```\n", bot, event=event) + await md.send_md(f"{info}\n\n" f"```\n{log}\n```\n", bot) else: info = ulang.get( "npm.install_failed", NAME=plugin_name, HOMEPAGE=homepage_btn ).replace("_", r"\\_") - await md.send_md(f"{info}\n\n" f"```\n{log}\n```", bot, event=event) + await md.send_md(f"{info}\n\n" f"```\n{log}\n```", bot) elif sc.get("uninstall") and perm_s: plugin_name: str = result.subcommands["uninstall"].args.get("plugin_name") # type: ignore @@ -464,7 +464,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher): else ulang.get("npm.next_page") ) reply += f"\n{btn_prev} {page}/{total} {btn_next}" - await md.send_md(reply, bot, event=event) + await md.send_md(reply, bot) else: if await SUPERUSER(bot, event): @@ -517,7 +517,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher): f"\n\n>page为页数,num为每页显示数量" f"\n\n>*{md.escape('npm list [page] [num]')}*" ) - await md.send_md(reply, bot, event=event) + await md.send_md(reply, bot) else: btn_list = md.btn_cmd( @@ -539,7 +539,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, npm: Matcher): f"\n\n>page为页数,num为每页显示数量" f"\n\n>*{md.escape('npm list [page] [num]')}*" ) - await md.send_md(reply, bot, event=event) + await md.send_md(reply, bot) @on_alconna( @@ -679,7 +679,7 @@ async def _(result: Arparma, matcher: Matcher, event: T_MessageEvent, bot: T_Bot else mdc.paragraph(ulang.get("npm.homepage")) ), ] - await md.send_md(compile_md(reply), bot, event=event) + await md.send_md(compile_md(reply), bot) else: await matcher.finish(ulang.get("npm.plugin_not_found", NAME=plugin_name)) else: diff --git a/src/nonebot_plugins/liteyuki_pacman/rpm.py b/src/nonebot_plugins/liteyuki_pacman/rpm.py index 35a47747..22417ed8 100644 --- a/src/nonebot_plugins/liteyuki_pacman/rpm.py +++ b/src/nonebot_plugins/liteyuki_pacman/rpm.py @@ -181,6 +181,6 @@ async def _(bot: T_Bot, event: T_MessageEvent, result: Arparma, matcher: Matcher else: pass if send_as_md: - await md.send_md(reply, bot, event=event) + await md.send_md(reply, bot) else: await matcher.finish(reply) diff --git a/src/nonebot_plugins/liteyuki_user/profile_manager.py b/src/nonebot_plugins/liteyuki_user/profile_manager.py index bdde211e..a79defb0 100644 --- a/src/nonebot_plugins/liteyuki_user/profile_manager.py +++ b/src/nonebot_plugins/liteyuki_user/profile_manager.py @@ -65,7 +65,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): # 未输入值,尝试呼出菜单 menu = get_profile_menu(result.args["key"], ulang) if menu: - await md.send_md(menu, bot, event=event) + await md.send_md(menu, bot) else: await profile_alc.finish(ulang.get("user.profile.input_value", ATTR=ulang.get(f"user.profile.{result.args['key']}"))) @@ -97,7 +97,7 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): reply += (f"\n**{key_text}** **{val}**\n" f"\n> {ulang.get(f'user.profile.{key}.desc')}" f"\n> {btn_set} \n\n***\n") - await md.send_md(reply, bot, event=event) + await md.send_md(reply, bot) def get_profile_menu(key: str, ulang: Language) -> Optional[str]: diff --git a/src/nonebot_plugins/nonebot-plugin-toliteyuki/__init__.py b/src/nonebot_plugins/nonebot-plugin-toliteyuki/__init__.py deleted file mode 100644 index 314b13ed..00000000 --- a/src/nonebot_plugins/nonebot-plugin-toliteyuki/__init__.py +++ /dev/null @@ -1,18 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved - -@Time : 2024/8/19 下午10:30 -@Author : snowykami -@Email : snowykami@outlook.com -@File : __init__.py.py -@Software: PyCharm -""" -from nonebot import require - -from liteyuki.comm.storage import shared_memory - -require("nonebot_plugin_alconna") - -from nonebot_plugin_alconna import UniMessage, Command, on_alconna - diff --git a/src/utils/message/message.py b/src/utils/message/message.py index 67ca76a1..7fde7ad6 100644 --- a/src/utils/message/message.py +++ b/src/utils/message/message.py @@ -22,9 +22,6 @@ config = load_from_yaml("config.yml") can_send_markdown = {} # 用于存储机器人是否支持发送markdown消息,id->bool -class TencentBannedMarkdownError(BaseException): - pass - async def broadcast_to_superusers(message: str | T_Message, markdown: bool = False): """广播消息给超级用户""" @@ -42,10 +39,7 @@ class MarkdownMessage: markdown: str, bot: T_Bot, *, message_type: str = None, - session_id: str | int = None, - event: T_MessageEvent = None, - retry_as_image: bool = True, - **kwargs + session_id: str | int = None ) -> dict[str, Any] | None: """ 发送Markdown消息,支持自动转为图片发送 @@ -54,20 +48,23 @@ class MarkdownMessage: bot: message_type: session_id: - event: - retry_as_image: 发送失败后是否尝试以图片形式发送,否则失败返回None - **kwargs: - Returns: """ - formatted_md = v11.unescape(markdown).replace("\n", r"\n").replace('"', r'\\\"') - plain_markdown = formatted_md.replace("[🔗", "[") + plain_markdown = markdown.replace("[🔗", "[") md_image_bytes = await md_to_pic( md=plain_markdown, width=540, device_scale_factor=4 ) + if md_image_bytes is None: + data = await bot.send_msg( + message_type=message_type, + group_id=session_id, + user_id=session_id, + message=markdown, + ) + return data data = await bot.send_msg( message_type=message_type, group_id=session_id, @@ -105,8 +102,8 @@ class MarkdownMessage: base64_string = base64.b64encode(image).decode("utf-8") data = await bot.call_api("upload_image", file=f"base64://{base64_string}") await MarkdownMessage.send_md(MarkdownMessage.image(data, Image.open(io.BytesIO(image)).size), bot, - event=event, message_type=message_type, - session_id=session_id, **kwargs) + message_type=message_type, + session_id=session_id) # 其他实现端方案 else: @@ -119,8 +116,7 @@ class MarkdownMessage: image_url = (await bot.get_msg(message_id=image_message_id))["message"][0]["data"]["url"] image_size = Image.open(io.BytesIO(image)).size image_md = MarkdownMessage.image(image_url, image_size) - return await MarkdownMessage.send_md(image_md, bot, message_type=message_type, session_id=session_id, - event=event, **kwargs) + return await MarkdownMessage.send_md(image_md, bot, message_type=message_type, session_id=session_id) if data is None: data = await bot.send_msg(