Skip to content

Commit

Permalink
✨ MessageChain支持转化为字符串(仅保留Text组件的内容)
Browse files Browse the repository at this point in the history
  • Loading branch information
XYCode-Kerman committed Jun 22, 2024
1 parent 739caa0 commit 0b3927c
Showing 1 changed file with 71 additions and 14 deletions.
85 changes: 71 additions & 14 deletions mirai_onebot/message/message_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,47 @@

from typing import List, Union

from mirai_onebot.message.message_components import (Audio, File, Image,
Location, Mention,
MentionAll,
MessageComponent, Reply,
Text, Video, Voice)
from mirai_onebot.message.message_components import (
Audio,
File,
Image,
Location,
Mention,
MentionAll,
MessageComponent,
Reply,
Text,
Video,
Voice,
)
from mirai_onebot.utils import logging

logger = logging.getLogger(__name__)

__all__ = [
'MessageChain'
]
__all__ = ["MessageChain"]


class MessageChain(object):
"""消息链"""

def __init__(self, components: List[Union[Audio, File, Image, Location, Mention, MentionAll, Reply, Text, Video, Voice, str]]) -> None:
def __init__(
self,
components: List[
Union[
Audio,
File,
Image,
Location,
Mention,
MentionAll,
Reply,
Text,
Video,
Voice,
str,
]
],
) -> None:
# str 自动转为 Text
for index, comp in enumerate(components):
if isinstance(comp, str):
Expand All @@ -29,10 +52,31 @@ def __init__(self, components: List[Union[Audio, File, Image, Location, Mention,

self._idx = 0

def append(self, comp: Union[Audio, File, Image, Location, Mention, MentionAll, Reply, Text, Video, Voice]):
def append(
self,
comp: Union[
Audio, File, Image, Location, Mention, MentionAll, Reply, Text, Video, Voice
],
):
self.components.append(comp)

def extend(self, comp: List[Union[Audio, File, Image, Location, Mention, MentionAll, Reply, Text, Video, Voice]]):
def extend(
self,
comp: List[
Union[
Audio,
File,
Image,
Location,
Mention,
MentionAll,
Reply,
Text,
Video,
Voice,
]
],
):
self.components.extend(comp)

@staticmethod
Expand Down Expand Up @@ -74,14 +118,19 @@ def load_from_dict(data: Union[list, dict]) -> MessageChain:
MessageChain: 加载后
"""
if isinstance(data, dict):
data = data['message']
data = data["message"]

return MessageChain([MessageComponent.load_from_dict(x) for x in data])

def to_dict(self):
return [x.to_dict() for x in self.components]

def has(self, comp: Union[Audio, File, Image, Location, Mention, MentionAll, Reply, Text, Video, Voice]) -> bool:
def has(
self,
comp: Union[
Audio, File, Image, Location, Mention, MentionAll, Reply, Text, Video, Voice
],
) -> bool:
"""检测是否有某一组件/某一组件类型。
Args:
Expand All @@ -108,7 +157,12 @@ def has(self, comp: Union[Audio, File, Image, Location, Mention, MentionAll, Rep
def __getitem__(self, idx: int):
return self.components[idx]

def __contains__(self, comp: Union[Audio, File, Image, Location, Mention, MentionAll, Reply, Text, Video, Voice]) -> bool:
def __contains__(
self,
comp: Union[
Audio, File, Image, Location, Mention, MentionAll, Reply, Text, Video, Voice
],
) -> bool:
return self.has(comp)

def __iter__(self):
Expand All @@ -123,3 +177,6 @@ def __next__(self):

self._idx += 1
return result

def __str__(self) -> str:
return "".join([str(x) for x in self.components if isinstance(x, Text)])

0 comments on commit 0b3927c

Please sign in to comment.