From 6ab7ee0d4b367a98481e1864808fbe253ed8b43b Mon Sep 17 00:00:00 2001 From: Azide Date: Wed, 27 Dec 2023 22:43:20 +0800 Subject: [PATCH] =?UTF-8?q?:boom:=20=E5=B0=86Reply=E7=9A=84data=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E4=B8=BAdict?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_saa/adapters/dodo.py | 4 ++-- nonebot_plugin_saa/adapters/feishu.py | 10 ++++++---- nonebot_plugin_saa/adapters/kaiheila.py | 4 ++-- nonebot_plugin_saa/adapters/onebot_v11.py | 4 ++-- nonebot_plugin_saa/adapters/onebot_v12.py | 4 ++-- nonebot_plugin_saa/adapters/qq.py | 4 ++-- nonebot_plugin_saa/adapters/qqguild.py | 4 ++-- nonebot_plugin_saa/adapters/red.py | 8 ++++---- nonebot_plugin_saa/adapters/telegram.py | 10 ++++++---- nonebot_plugin_saa/registries/message_id.py | 3 ++- nonebot_plugin_saa/types/common_message_segment.py | 12 ++++++------ 11 files changed, 36 insertions(+), 31 deletions(-) diff --git a/nonebot_plugin_saa/adapters/dodo.py b/nonebot_plugin_saa/adapters/dodo.py index a39ea393..cfccaee4 100644 --- a/nonebot_plugin_saa/adapters/dodo.py +++ b/nonebot_plugin_saa/adapters/dodo.py @@ -90,8 +90,8 @@ async def _image(image: Image, bot: BaseBot) -> MessageSegment: @register_dodo(Reply) def _reply(reply: Reply) -> MessageSegment: - assert isinstance(reply.data, DodoMessageId) - return MessageSegment.reference(reply.data.message_id) + assert isinstance(mid := reply.data["message_id"], DodoMessageId) + return MessageSegment.reference(mid.message_id) @register_dodo(Mention) def _mention(mention: Mention) -> MessageSegment: diff --git a/nonebot_plugin_saa/adapters/feishu.py b/nonebot_plugin_saa/adapters/feishu.py index 60abb2c7..4f7c8cb4 100644 --- a/nonebot_plugin_saa/adapters/feishu.py +++ b/nonebot_plugin_saa/adapters/feishu.py @@ -77,8 +77,8 @@ def _mention(m: Mention) -> MessageSegment: @register_feishu(Reply) def _reply(r: Reply) -> MessageSegment: - assert isinstance(r.data, FeishuMessageId) - return MessageSegment("reply", {"message_id": r.data.message_id}) + assert isinstance(mid := r.data["message_id"], FeishuMessageId) + return MessageSegment("reply", {"message_id": mid.message_id}) @register_target_extractor(PrivateMessageEvent) def _extract_private_msg_event(event: Event) -> TargetFeishuPrivate: @@ -141,8 +141,10 @@ async def send( message_to_send = Message() for message_segment_factory in full_msg: if isinstance(message_segment_factory, Reply): - assert isinstance(message_segment_factory.data, FeishuMessageId) - reply_to_message_id = message_segment_factory.data.message_id + assert isinstance( + mid := message_segment_factory.data["message_id"], FeishuMessageId + ) + reply_to_message_id = mid.message_id continue message_segment = await message_segment_factory.build(bot) diff --git a/nonebot_plugin_saa/adapters/kaiheila.py b/nonebot_plugin_saa/adapters/kaiheila.py index 55064094..41d152ef 100644 --- a/nonebot_plugin_saa/adapters/kaiheila.py +++ b/nonebot_plugin_saa/adapters/kaiheila.py @@ -83,8 +83,8 @@ def _mention(m: Mention) -> MessageSegment: @register_kaiheila(Reply) def _reply(r: Reply) -> MessageSegment: - assert isinstance(r.data, KaiheilaMessageId) - return MessageSegment.quote(r.data.message_id) + assert isinstance(mid := r.data["message_id"], KaiheilaMessageId) + return MessageSegment.quote(mid.message_id) @register_target_extractor(PrivateMessageEvent) def _extract_private_msg_event(event: Event) -> TargetKaiheilaPrivate: diff --git a/nonebot_plugin_saa/adapters/onebot_v11.py b/nonebot_plugin_saa/adapters/onebot_v11.py index e079c423..223c4484 100644 --- a/nonebot_plugin_saa/adapters/onebot_v11.py +++ b/nonebot_plugin_saa/adapters/onebot_v11.py @@ -70,8 +70,8 @@ async def _mention(m: Mention) -> MessageSegment: @register_onebot_v11(Reply) async def _reply(r: Reply) -> MessageSegment: - assert isinstance(r.data, OB11MessageId) - return MessageSegment.reply(r.data.message_id) + assert isinstance(mid := r.data["message_id"], OB11MessageId) + return MessageSegment.reply(mid.message_id) @register_target_extractor(PrivateMessageEvent) def _extract_private_msg_event(event: Event) -> TargetQQPrivate: diff --git a/nonebot_plugin_saa/adapters/onebot_v12.py b/nonebot_plugin_saa/adapters/onebot_v12.py index 79c23eda..c8b28083 100644 --- a/nonebot_plugin_saa/adapters/onebot_v12.py +++ b/nonebot_plugin_saa/adapters/onebot_v12.py @@ -96,8 +96,8 @@ async def _mention(m: Mention) -> MessageSegment: @register_onebot_v12(Reply) async def _reply(r: Reply) -> MessageSegment: - assert isinstance(r.data, OB12MessageId) - return MessageSegment.reply(r.data.message_id) + assert isinstance(mid := r.data["message_id"], OB12MessageId) + return MessageSegment.reply(mid.message_id) @register_target_extractor(PrivateMessageEvent) def _extract_private_msg_event(event: Event) -> PlatformTarget: diff --git a/nonebot_plugin_saa/adapters/qq.py b/nonebot_plugin_saa/adapters/qq.py index aadc783b..d0e40093 100644 --- a/nonebot_plugin_saa/adapters/qq.py +++ b/nonebot_plugin_saa/adapters/qq.py @@ -72,8 +72,8 @@ def _mention(m: Mention) -> MessageSegment: @register_qq(Reply) def _reply(r: Reply) -> MessageSegment: - assert isinstance(r.data, QQMessageId) - return MessageSegment.reference(r.data.message_id) + assert isinstance(mid := r.data["message_id"], QQMessageId) + return MessageSegment.reference(mid.message_id) @register_target_extractor(GuildMessageEvent) def extract_message_event(event: Event) -> PlatformTarget: diff --git a/nonebot_plugin_saa/adapters/qqguild.py b/nonebot_plugin_saa/adapters/qqguild.py index 0cd6f374..74d1217a 100644 --- a/nonebot_plugin_saa/adapters/qqguild.py +++ b/nonebot_plugin_saa/adapters/qqguild.py @@ -62,8 +62,8 @@ def _mention(m: Mention) -> MessageSegment: @register_qqguild(Reply) def _reply(r: Reply) -> MessageSegment: - assert isinstance(r.data, QQGuildMessageId) - return MessageSegment.reference(r.data.message_id) + assert isinstance(mid := r.data["message_id"], QQGuildMessageId) + return MessageSegment.reference(mid.message_id) @register_target_extractor(MessageEvent) def extract_message_event(event: Event) -> PlatformTarget: diff --git a/nonebot_plugin_saa/adapters/red.py b/nonebot_plugin_saa/adapters/red.py index 8c49c734..96b1012c 100644 --- a/nonebot_plugin_saa/adapters/red.py +++ b/nonebot_plugin_saa/adapters/red.py @@ -71,11 +71,11 @@ async def _mention(m: Mention) -> MessageSegment: @register_red(Reply) async def _reply(r: Reply) -> MessageSegment: - assert isinstance(r.data, RedMessageId) + assert isinstance(mid := r.data["message_id"], RedMessageId) return MessageSegment.reply( - message_seq=r.data.message_seq, - message_id=r.data.message_id, - sender_uin=r.data.sender_uin, + message_seq=mid.message_seq, + message_id=mid.message_id, + sender_uin=mid.sender_uin, ) @register_target_extractor(PrivateMessageEvent) diff --git a/nonebot_plugin_saa/adapters/telegram.py b/nonebot_plugin_saa/adapters/telegram.py index b838d1cf..88d9b9bc 100644 --- a/nonebot_plugin_saa/adapters/telegram.py +++ b/nonebot_plugin_saa/adapters/telegram.py @@ -70,8 +70,8 @@ async def _mention(m: Mention) -> MessageSegment: @register_telegram(Reply) async def _reply(r: Reply) -> MessageSegment: - assert isinstance(r.data, TelegramMessageId) - return MessageSegment("reply", {"message_id": str(r.data.message_id)}) + assert isinstance(mid := r.data["message_id"], TelegramMessageId) + return MessageSegment("reply", {"message_id": str(mid.message_id)}) @register_target_extractor(PrivateMessageEvent) @register_target_extractor(GroupMessageEvent) @@ -165,8 +165,10 @@ async def send( message_to_send = Message() for message_segment_factory in full_msg: if isinstance(message_segment_factory, Reply): - assert isinstance(message_segment_factory.data, TelegramMessageId) - reply_to_message_id = message_segment_factory.data.message_id + assert isinstance( + mid := message_segment_factory.data["message_id"], TelegramMessageId + ) + reply_to_message_id = mid.message_id continue if ( diff --git a/nonebot_plugin_saa/registries/message_id.py b/nonebot_plugin_saa/registries/message_id.py index 2a88e536..6f3a8fc9 100644 --- a/nonebot_plugin_saa/registries/message_id.py +++ b/nonebot_plugin_saa/registries/message_id.py @@ -1,3 +1,4 @@ +from abc import ABC from typing_extensions import Annotated from typing import Dict, Type, Callable, Optional @@ -8,7 +9,7 @@ from ..utils import SupportedAdapters -class MessageId(SerializationMeta): +class MessageId(SerializationMeta, ABC): _index_key = "adapter_name" adapter_name: SupportedAdapters diff --git a/nonebot_plugin_saa/types/common_message_segment.py b/nonebot_plugin_saa/types/common_message_segment.py index ec1d6b9f..80c27597 100644 --- a/nonebot_plugin_saa/types/common_message_segment.py +++ b/nonebot_plugin_saa/types/common_message_segment.py @@ -84,11 +84,15 @@ def __init__(self, user_id: str): self.data = {"user_id": user_id} +class ReplyData(TypedDict): + message_id: MessageId + + class Reply(MessageSegmentFactory): """回复其他消息的消息段""" type: Literal["reply"] = "reply" - data: MessageId + data: ReplyData def __init__(self, message_id: MessageId): """回复其他消息的消息段 @@ -96,9 +100,5 @@ def __init__(self, message_id: MessageId): 参数: message_id: 需要回复消息的 MessageId """ - super().__init__() - self.data = message_id - - def __len__(self) -> int: - return 1 # 仅占一个消息段 FIXME: Reply的data应该统一为dict,而不是MessageId + self.data = {"message_id": message_id}