mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-20 02:08:20 +08:00
⚡️ remove unnecessary code
This commit is contained in:
parent
603a63a629
commit
e3c3e370cb
@ -290,12 +290,7 @@ class Bot(BaseBot):
|
|||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Cannot guess `receive_id` and `receive_id_type` to reply!")
|
"Cannot guess `receive_id` and `receive_id_type` to reply!")
|
||||||
|
|
||||||
if isinstance(message, MessageSegment):
|
msg_type, content = MessageSerializer(msg).serialize()
|
||||||
msg_type = message.type
|
|
||||||
elif isinstance(message, Message):
|
|
||||||
msg_type = message[0].type
|
|
||||||
else:
|
|
||||||
msg_type = "text"
|
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
"query": {
|
"query": {
|
||||||
@ -303,7 +298,7 @@ class Bot(BaseBot):
|
|||||||
},
|
},
|
||||||
"body": {
|
"body": {
|
||||||
"receive_id": receive_id,
|
"receive_id": receive_id,
|
||||||
"content": MessageSerializer(Message(message)).serialize(),
|
"content": content,
|
||||||
"msg_type": msg_type
|
"msg_type": msg_type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from typing import Any, List, Literal, Optional, Type, Union
|
from typing import Any, List, Literal, Optional, Type
|
||||||
from pygtrie import StringTrie
|
from pygtrie import StringTrie
|
||||||
from pydantic import BaseModel, root_validator, Field
|
from pydantic import BaseModel, root_validator, Field
|
||||||
|
|
||||||
from nonebot.adapters import Event as BaseEvent
|
from nonebot.adapters import Event as BaseEvent
|
||||||
from nonebot.typing import overrides
|
from nonebot.typing import overrides
|
||||||
|
|
||||||
from .message import Message, MessageDeserializer
|
from .message import Message, MessageDeserializer, MessageSerializer
|
||||||
|
|
||||||
|
|
||||||
class EventHeader(BaseModel):
|
class EventHeader(BaseModel):
|
||||||
@ -143,7 +143,7 @@ class MessageEvent(Event):
|
|||||||
f"Message[{super().get_type()}]"
|
f"Message[{super().get_type()}]"
|
||||||
f" {self.event.message.message_id} from {self.get_user_id()}"
|
f" {self.event.message.message_id} from {self.get_user_id()}"
|
||||||
f"@[{self.event.message.chat_type}:{self.event.message.chat_id}]"
|
f"@[{self.event.message.chat_type}:{self.event.message.chat_id}]"
|
||||||
f" {self.event.message.content}")
|
f" {str(self.get_message()) and MessageSerializer(self.get_message()).serialize()}")
|
||||||
|
|
||||||
@overrides(Event)
|
@overrides(Event)
|
||||||
def get_message(self) -> Message:
|
def get_message(self) -> Message:
|
||||||
@ -155,7 +155,7 @@ class MessageEvent(Event):
|
|||||||
|
|
||||||
@overrides(Event)
|
@overrides(Event)
|
||||||
def get_user_id(self) -> str:
|
def get_user_id(self) -> str:
|
||||||
return self.event.sender.sender_id.union_id
|
return self.event.sender.sender_id.user_id
|
||||||
|
|
||||||
@overrides(Event)
|
@overrides(Event)
|
||||||
def get_session_id(self) -> str:
|
def get_session_id(self) -> str:
|
||||||
|
@ -186,12 +186,8 @@ class MessageSerializer:
|
|||||||
"""
|
"""
|
||||||
message: Message
|
message: Message
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self) -> Tuple[str, str]:
|
||||||
for segment in self.message:
|
return self.message[0].type, json.dumps(self.message[0].data)
|
||||||
if segment.type == "post":
|
|
||||||
raise NotImplementedError
|
|
||||||
else:
|
|
||||||
return json.dumps(segment.data)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -199,13 +195,13 @@ class MessageDeserializer:
|
|||||||
"""
|
"""
|
||||||
飞书 协议 Message 反序列化器。
|
飞书 协议 Message 反序列化器。
|
||||||
"""
|
"""
|
||||||
data: Dict[str, Any]
|
|
||||||
type: str
|
type: str
|
||||||
|
data: Dict[str, Any]
|
||||||
|
|
||||||
def deserialize(self):
|
def deserialize(self) -> Message:
|
||||||
print(self.type, self.data)
|
print(self.type, self.data)
|
||||||
if self.type == "post":
|
if self.type == "post":
|
||||||
return self._parse_rich_text(self.data)
|
return Message(self._parse_rich_text(self.data))
|
||||||
else:
|
else:
|
||||||
return Message(MessageSegment(self.type, self.data))
|
return Message(MessageSegment(self.type, self.data))
|
||||||
|
|
||||||
@ -221,8 +217,7 @@ class MessageDeserializer:
|
|||||||
tag = element.get("tag")
|
tag = element.get("tag")
|
||||||
yield tag, element
|
yield tag, element
|
||||||
|
|
||||||
temp = Message()
|
return [
|
||||||
for type_, data in _iter_message(message_data):
|
MessageSegment(type_, data)
|
||||||
temp += MessageSegment(type_, data)
|
for type_, data in _iter_message(message_data)
|
||||||
|
]
|
||||||
return temp
|
|
||||||
|
Loading…
Reference in New Issue
Block a user