Skip to content

Commit

Permalink
💥 将Reply的data统一为dict
Browse files Browse the repository at this point in the history
  • Loading branch information
AzideCupric committed Dec 27, 2023
1 parent 879fa01 commit 6ab7ee0
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 31 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)

Check warning on line 76 in nonebot_plugin_saa/adapters/qq.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/qq.py#L75-L76

Added lines #L75 - L76 were not covered by tests

@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)

Check warning on line 66 in nonebot_plugin_saa/adapters/qqguild.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/qqguild.py#L65-L66

Added lines #L65 - L66 were not covered by tests

@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 @@ -84,21 +84,21 @@ 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):
"""回复其他消息的消息段
参数:
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}

0 comments on commit 6ab7ee0

Please sign in to comment.