Skip to content

Commit

Permalink
✨ 新增 Text 组件的 str 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
XYCode-Kerman committed Jun 23, 2024
1 parent f00d0f1 commit 863fd4f
Showing 1 changed file with 79 additions and 56 deletions.
135 changes: 79 additions & 56 deletions mirai_onebot/message/message_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ def __init__(self) -> None:
self.data = {}

def to_dict(self) -> Dict[str, Any]:
return {
'type': self.message_type,
'data': self.data
}
return {"type": self.message_type, "data": self.data}

@staticmethod
def load_from_dict(data: dict) -> MessageComponentsType:
Expand All @@ -43,54 +40,63 @@ def load_from_dict(data: dict) -> MessageComponentsType:
MessageComponent: 加载后的消息组件
"""

message_type = data.get('type')
if message_type == 'text':
return Text(data['data']['text'])
elif message_type == 'mention':
return Mention(data['data']['user_id'])
elif message_type == 'mention_all':
message_type = data.get("type")
if message_type == "text":
return Text(data["data"]["text"])
elif message_type == "mention":
return Mention(data["data"]["user_id"])
elif message_type == "mention_all":
return MentionAll()
elif message_type == 'image':
return Image(data['data']['file_id'])
elif message_type == 'voice':
return Voice(data['data']['file_id'])
elif message_type == 'audio':
return Audio(data['data']['file_id'])
elif message_type == 'video':
return Video(data['data']['file_id'])
elif message_type == 'file':
return File(data['data']['file_id'])
elif message_type == 'location':
return Location(data['data']['latitude'], data['data']['longitude'], data['data']['title'], data['data']['content'])
elif message_type == 'reply':
return Reply(data['data']['message_id'], data['data'].get('user_id', None))
elif message_type == "image":
return Image(data["data"]["file_id"])
elif message_type == "voice":
return Voice(data["data"]["file_id"])
elif message_type == "audio":
return Audio(data["data"]["file_id"])
elif message_type == "video":
return Video(data["data"]["file_id"])
elif message_type == "file":
return File(data["data"]["file_id"])
elif message_type == "location":
return Location(
data["data"]["latitude"],
data["data"]["longitude"],
data["data"]["title"],
data["data"]["content"],
)
elif message_type == "reply":
return Reply(data["data"]["message_id"], data["data"].get("user_id", None))
else:
raise ValueError(f"Unknown message type: {message_type}")

def __repr__(self) -> str:
return f'{self.message_type}: {self.data}'
return f"{self.message_type}: {self.data}"

def __eq__(self, __value: MessageComponent) -> bool: # type: ignore
if isinstance(__value, MessageComponent): # 实例化的
return self.message_type == __value.message_type and self.data == __value.data
return (
self.message_type == __value.message_type and self.data == __value.data
)
elif isinstance(__value, type): # 未实例化
return isinstance(self, __value)
else:
raise ValueError("Can't compare MessageComponent with non-MessageComponent object")
raise ValueError(
"Can't compare MessageComponent with non-MessageComponent object"
)


class Text(MessageComponent):
"""纯文本"""

message_type: str = 'text'
message_type: str = "text"
"""消息类型(为避免与built-in函数type冲突,命名为message_type)"""
text: str
"""纯文本内容"""

def __init__(self, text: str) -> None:
super().__init__()
self.text = text
self.data['text'] = self.text
self.data["text"] = self.text

def __eq__(self, __value: object) -> bool:
if isinstance(__value, str):
Expand All @@ -100,25 +106,31 @@ def __eq__(self, __value: object) -> bool:
else:
return False

def __repr__(self) -> str:
return self.text

def __str__(self) -> str:
return self.text


class Mention(MessageComponent):
"""提及"""

message_type: str = 'mention'
message_type: str = "mention"
"""消息类型(为避免与built-in函数type冲突,命名为message_type)"""
user_id: str
"""提及的用户 ID"""

def __init__(self, user_id: str) -> None:
super().__init__()
self.user_id = user_id
self.data['user_id'] = self.user_id
self.data["user_id"] = self.user_id


class MentionAll(MessageComponent):
"""提及所有人"""

message_type: str = 'mention_all'
message_type: str = "mention_all"
"""消息类型(为避免与built-in函数type冲突,命名为message_type)"""

def __init__(self) -> None:
Expand All @@ -128,29 +140,29 @@ def __init__(self) -> None:
class Image(MessageComponent):
"""图片"""

message_type: str = 'image'
message_type: str = "image"
"""消息类型(为避免与built-in函数type冲突,命名为message_type)"""
file_id: str
"""图片文件 ID"""

def __init__(self, file_id: str) -> None:
super().__init__()
self.file_id = file_id
self.data['file_id'] = self.file_id
self.data["file_id"] = self.file_id


class Voice(MessageComponent):
"""语音"""

message_type: str = 'voice'
message_type: str = "voice"
"""消息类型(为避免与built-in函数type冲突,命名为message_type)"""
file_id: str
"""语音文件 ID"""

def __init__(self, file_id: str) -> None:
super().__init__()
self.file_id = file_id
self.data['file_id'] = self.file_id
self.data["file_id"] = self.file_id


class Audio(MessageComponent):
Expand All @@ -159,49 +171,49 @@ class Audio(MessageComponent):
提示:音频消息段和语音消息段的区别是:语音消息段在聊天软件中表现为用户当场录制的声音,而音频消息段可能是直接发送的一个音乐文件,在消息列表中显示为可播放。
"""

message_type: str = 'audio'
message_type: str = "audio"
"""消息类型(为避免与built-in函数type冲突,命名为message_type)"""
file_id: str
"""音频文件 ID"""

def __init__(self, file_id: str) -> None:
super().__init__()
self.file_id = file_id
self.data['file_id'] = self.file_id
self.data["file_id"] = self.file_id


class Video(MessageComponent):
"""视频"""

message_type: str = 'video'
message_type: str = "video"
"""消息类型(为避免与built-in函数type冲突,命名为message_type)"""
file_id: str
"""视频文件 ID"""

def __init__(self, file_id: str) -> None:
super().__init__()
self.file_id = file_id
self.data['file_id'] = self.file_id
self.data["file_id"] = self.file_id


class File(MessageComponent):
"""文件"""

message_type: str = 'file'
message_type: str = "file"
"""消息类型(为避免与built-in函数type冲突,命名为message_type)"""
file_id: str
"""文件 ID"""

def __init__(self, file_id: str) -> None:
super().__init__()
self.file_id = file_id
self.data['file_id'] = self.file_id
self.data["file_id"] = self.file_id


class Location(MessageComponent):
"""位置"""

message_type: str = 'location'
message_type: str = "location"
"""消息类型(为避免与built-in函数type冲突,命名为message_type)"""
latitude: float
"""纬度"""
Expand All @@ -212,24 +224,26 @@ class Location(MessageComponent):
content: str
"""地址内容"""

def __init__(self, latitude: float, longitude: float, title: str, content: str) -> None:
def __init__(
self, latitude: float, longitude: float, title: str, content: str
) -> None:
super().__init__()
self.latitude = latitude
self.longitude = longitude
self.title = title
self.content = content
self.data = {
'latitude': self.latitude,
'longitude': self.longitude,
'title': self.title,
'content': self.content
"latitude": self.latitude,
"longitude": self.longitude,
"title": self.title,
"content": self.content,
}


class Reply(MessageComponent):
"""回复"""

message_type: str = 'reply'
message_type: str = "reply"
"""消息类型(为避免与built-in函数type冲突,命名为message_type)"""
message_id: str
"""回复的消息 ID"""
Expand All @@ -240,15 +254,24 @@ def __init__(self, message_id: str, user_id: Optional[str] = None) -> None:
super().__init__()
self.message_id = message_id
self.user_id = user_id
self.data['message_id'] = self.message_id
self.data['user_id'] = self.user_id
self.data["message_id"] = self.message_id
self.data["user_id"] = self.user_id


MessageComponentsType = Union[Text, Mention, MentionAll, Image, Voice, Audio, Video, File, Location, Reply]
MessageComponentsType = Union[
Text, Mention, MentionAll, Image, Voice, Audio, Video, File, Location, Reply
]

__all__ = [
'Text', 'Mention', 'MentionAll', 'Image',
'Voice', 'Audio', 'Video', 'File',
'Location', 'Reply',
'MessageComponentsType'
"Text",
"Mention",
"MentionAll",
"Image",
"Voice",
"Audio",
"Video",
"File",
"Location",
"Reply",
"MessageComponentsType",
]

0 comments on commit 863fd4f

Please sign in to comment.