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 2a5123d4..79e7ccfa 100644 --- a/nonebot_plugin_saa/types/common_message_segment.py +++ b/nonebot_plugin_saa/types/common_message_segment.py @@ -112,10 +112,14 @@ def __init__(self, user_id: str): self.data = {"user_id": user_id} +class ReplyData(TypedDict): + message_id: MessageId + + class Reply(MessageSegmentFactory): """回复其他消息的消息段""" - data: MessageId + data: ReplyData def __init__(self, message_id: MessageId): """回复其他消息的消息段 @@ -123,9 +127,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} diff --git a/tests/test_message.py b/tests/test_message.py index 2ddd5319..f086cdc5 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -37,13 +37,12 @@ def test_message_segment_to_str(): assert str(i_bytesio) == f"[SAA:Image|name='image',image=]" assert repr(i_bytesio) == "Image(name='image', image=BytesIO(b'123'))" - # FIXME: Reply还不支持 - # r = Reply(MessageId(adapter_name=SupportedAdapters.fake)) - # assert r.get("data").get("message_id") == MessageId( - # adapter_name=SupportedAdapters.fake - # ) - # assert str(r) == "[SAA:Reply|message_id=MessageId(adapter_name='fake')]" - # assert repr(r) == "Reply(message_id=MessageId(adapter_name='fake'))" + r = Reply(MessageId(adapter_name=SupportedAdapters.fake)) + assert r.get("data").get("message_id") == MessageId( + adapter_name=SupportedAdapters.fake + ) + assert str(r).startswith("[SAA:Reply|message_id=MessageId(adapter_name=") + assert repr(r).startswith("Reply(message_id=MessageId(adapter_name=") m = Mention("123") assert m.get("data").get("user_id") == "123"