Skip to content

Commit

Permalink
✨ 补充Message__add__等方法
Browse files Browse the repository at this point in the history
  • Loading branch information
CMHopeSunshine committed Nov 10, 2023
1 parent 4b522be commit e7600f3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
8 changes: 3 additions & 5 deletions nonebot/adapters/dodo/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,15 @@ class MessageEvent(Event):
message_type: MessageType = Field(alias="messageType")
message_body: MessageBody = Field(alias="messageBody")

to_me: bool = Field(default=False, alias="toMe")
to_me: bool = False

@property
def message(self) -> Message:
return self.get_message()

@property
def origin_message(self) -> Message:
if not hasattr(self, "_origin_message"):
return self.get_message()
return getattr(self, "_origin_message")
return getattr(self, "_origin_message", self.get_message())

@property
def reply(self) -> Optional[Reference]:
Expand Down Expand Up @@ -367,7 +365,7 @@ class PersonalMessageEvent(MessageEvent):
default=EventType.PERSONAL_MESSAGE, alias="eventType"
)

to_me: bool = Field(default=True, alias="toMe")
to_me: bool = True


EventClass = Union[
Expand Down
33 changes: 33 additions & 0 deletions nonebot/adapters/dodo/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Tuple,
Type,
TypedDict,
Union,
)
from typing_extensions import Self, override

Expand Down Expand Up @@ -147,6 +148,22 @@ def card(
# },
# )

@override
def __add__(
self, other: Union[str, "MessageSegment", Iterable["MessageSegment"]]
) -> "Message":
return Message(self) + (
MessageSegment.text(other) if isinstance(other, str) else other
)

@override
def __radd__(
self, other: Union[str, "MessageSegment", Iterable["MessageSegment"]]
) -> "Message":
return (
MessageSegment.text(other) if isinstance(other, str) else Message(other)
) + self


class _TextData(TypedDict):
text: str
Expand Down Expand Up @@ -352,6 +369,22 @@ class Message(BaseMessage[MessageSegment]):
def get_segment_class(cls) -> Type[MessageSegment]:
return MessageSegment

@override
def __add__(
self, other: Union[str, MessageSegment, Iterable[MessageSegment]]
) -> "Message":
return super().__add__(
MessageSegment.text(other) if isinstance(other, str) else other
)

@override
def __radd__(
self, other: Union[str, MessageSegment, Iterable[MessageSegment]]
) -> "Message":
return super().__radd__(
MessageSegment.text(other) if isinstance(other, str) else other
)

@staticmethod
@override
def _construct(msg: str) -> Iterable[MessageSegment]:
Expand Down

0 comments on commit e7600f3

Please sign in to comment.