Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

去除MessageFactory和MessageSegmentFactory的泛型,补充成员方法 #128

Merged
merged 8 commits into from
Dec 29, 2023
307 changes: 291 additions & 16 deletions nonebot_plugin_saa/abstract_factories.py

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions nonebot_plugin_saa/adapters/dodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from ..utils import SupportedAdapters, SupportedPlatform
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -184,7 +183,7 @@ def raw(self) -> str:
@register_sender(adapter)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
3 changes: 1 addition & 2 deletions nonebot_plugin_saa/adapters/feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from ..types import Text, Image, Reply, Mention
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -113,7 +112,7 @@ def _(event: Event) -> FeishuMessageId:
@register_sender(adapter)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
3 changes: 1 addition & 2 deletions nonebot_plugin_saa/adapters/kaiheila.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ..utils import SupportedAdapters, SupportedPlatform
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -133,7 +132,7 @@ def _(event: Event) -> KaiheilaMessageId:
@register_sender(SupportedAdapters.kaiheila)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
3 changes: 1 addition & 2 deletions nonebot_plugin_saa/adapters/onebot_v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from ..utils import SupportedAdapters, SupportedPlatform
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
AggregatedMessageFactory,
register_ms_adapter,
assamble_message_factory,
Expand Down Expand Up @@ -173,7 +172,7 @@ def raw(self) -> Any:
@register_sender(SupportedAdapters.onebot_v11)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
3 changes: 1 addition & 2 deletions nonebot_plugin_saa/adapters/onebot_v12.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ..utils import SupportedAdapters, SupportedPlatform
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -257,7 +256,7 @@ def raw(self):
@register_sender(SupportedAdapters.onebot_v12)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
3 changes: 1 addition & 2 deletions nonebot_plugin_saa/adapters/qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ..auto_select_bot import register_list_targets
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -141,7 +140,7 @@ def raw(self):
@register_sender(SupportedAdapters.qq)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target: PlatformTarget,
event: Optional[Event],
at_sender: bool,
Expand Down
3 changes: 1 addition & 2 deletions nonebot_plugin_saa/adapters/qqguild.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ..auto_select_bot import register_list_targets
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -111,7 +110,7 @@ def raw(self):
@register_sender(SupportedAdapters.qqguild)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
3 changes: 1 addition & 2 deletions nonebot_plugin_saa/adapters/red.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ..utils import SupportedAdapters, SupportedPlatform
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
AggregatedMessageFactory,
register_ms_adapter,
assamble_message_factory,
Expand Down Expand Up @@ -126,7 +125,7 @@ def raw(self) -> MessageModel:
@register_sender(SupportedAdapters.red)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
3 changes: 1 addition & 2 deletions nonebot_plugin_saa/adapters/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ..types import Text, Image, Reply, Mention
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -137,7 +136,7 @@ def _(event: Event):
@register_sender(SupportedAdapters.telegram)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
37 changes: 37 additions & 0 deletions nonebot_plugin_saa/types/common_message_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def __init__(self, text: str) -> None:
def __str__(self) -> str:
return self.data["text"]

def __len__(self) -> int:
return len(self.data["text"])


MessageFactory.register_text_ms(lambda text: Text(text))

Expand Down Expand Up @@ -57,6 +60,37 @@ def __init__(
super().__init__()
self.data = {"image": image, "name": name}

def __str__(self) -> str:
image = self.data["image"]
format_template = "{0}={1}"
if isinstance(image, bytes):
image_str = format_template.format("image", f"<bytes {len(image)}>")
elif isinstance(image, BytesIO):
image_str = format_template.format(
"image", f"<BytesIO {len(image.getvalue())}>"
)
else:
image_str = format_template.format("image", repr(image))

kv_list = list(f"{k}={v!r}" for k, v in self.data.items() if k != "image")
kv_list.append(image_str)
return f"[SAA:{self.__class__.__name__}|{','.join(kv_list)}]"

def __repr__(self) -> str:
image = self.data["image"]
format_template = "{0}={1}"
if isinstance(image, bytes):
image_str = format_template.format("image", repr(image))
elif isinstance(image, BytesIO):
image_str = format_template.format(
"image", f"BytesIO({repr(image.getvalue())})"
)
else:
image_str = format_template.format("image", repr(image))
kv_list = list(f"{k}={v!r}" for k, v in self.data.items() if k != "image")
kv_list.append(image_str)
return f"{self.__class__.__name__}({', '.join(kv_list)})"


class MentionData(TypedDict):
user_id: str
Expand Down Expand Up @@ -92,3 +126,6 @@ def __init__(self, message_id: MessageId):

super().__init__()
self.data = message_id

def __len__(self) -> int:
return 1 # 仅占一个消息段 FIXME: Reply的data应该统一为dict,而不是MessageId
6 changes: 5 additions & 1 deletion tests/test_feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import httpx
from nonebug import App
from nonebot import get_driver
from nonebot.adapters.feishu.bot import BotInfo
from nonebot.adapters.feishu import Bot, Message
from nonebot.adapters.feishu.models import BotInfo
from nonebot.adapters.feishu.config import BotConfig

from nonebot_plugin_saa.utils import SupportedAdapters
Expand Down Expand Up @@ -86,6 +86,8 @@ def mock_feishu_message_event(message: Message, group=False):
message=PrivateEventMessage(chat_type="p2p", **event_message_dict),
),
reply=None,
_message=message,
original_message=message,
)
else:
return GroupMessageEvent(
Expand All @@ -96,6 +98,8 @@ def mock_feishu_message_event(message: Message, group=False):
message=GroupEventMessage(chat_type="group", **event_message_dict),
),
reply=None,
_message=message,
original_message=message,
)


Expand Down
Loading