Skip to content

Commit

Permalink
💥 将 Reply 的 data 字段统一为 dict (#129)
Browse files Browse the repository at this point in the history
* 💥 将Reply的data统一为dict

* ✅ 添加Reply的__str__, __repr__测试
  • Loading branch information
AzideCupric authored Jan 11, 2024
1 parent e13a3ed commit 130e840
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 38 deletions.
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/dodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions nonebot_plugin_saa/adapters/feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/kaiheila.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/onebot_v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/onebot_v12.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/qqguild.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions nonebot_plugin_saa/adapters/red.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions nonebot_plugin_saa/adapters/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion nonebot_plugin_saa/registries/message_id.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from abc import ABC
from typing_extensions import Annotated
from typing import Dict, Type, Callable, Optional

Expand All @@ -8,7 +9,7 @@
from ..utils import SupportedAdapters


class MessageId(SerializationMeta):
class MessageId(SerializationMeta, ABC):
_index_key = "adapter_name"

adapter_name: SupportedAdapters
Expand Down
12 changes: 6 additions & 6 deletions nonebot_plugin_saa/types/common_message_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ 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):
"""回复其他消息的消息段
参数:
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}
13 changes: 6 additions & 7 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ def test_message_segment_to_str():
assert str(i_bytesio) == f"[SAA:Image|name='image',image=<BytesIO {len(b'123')}>]"
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"
Expand Down

0 comments on commit 130e840

Please sign in to comment.