nonebot2/nonebot/adapters/mirai/event/message.py

79 lines
1.8 KiB
Python
Raw Normal View History

2021-01-30 20:40:00 +08:00
from typing import Any
from pydantic import Field
from nonebot.typing import overrides
2021-01-30 06:10:04 +08:00
from ..message import MessageChain
from .base import Event, GroupChatInfo, PrivateChatInfo
2021-01-30 06:10:04 +08:00
class MessageEvent(Event):
"""消息事件基类"""
message_chain: MessageChain = Field(alias='messageChain')
2021-01-30 20:40:00 +08:00
sender: Any
@overrides(Event)
def get_message(self) -> MessageChain:
return self.message_chain
@overrides(Event)
def get_plaintext(self) -> str:
return self.message_chain.extract_plain_text()
@overrides(Event)
def get_user_id(self) -> str:
2021-01-30 20:40:00 +08:00
raise NotImplementedError
@overrides(Event)
def get_session_id(self) -> str:
2021-01-30 20:40:00 +08:00
raise NotImplementedError
class GroupMessage(MessageEvent):
"""群消息事件"""
sender: GroupChatInfo
to_me: bool = False
2021-01-30 20:40:00 +08:00
@overrides(MessageEvent)
def get_session_id(self) -> str:
return f'group_{self.sender.group.id}_' + self.get_user_id()
@overrides(MessageEvent)
def get_user_id(self) -> str:
return str(self.sender.id)
@overrides(MessageEvent)
def is_tome(self) -> bool:
return self.to_me
class FriendMessage(MessageEvent):
"""好友消息事件"""
sender: PrivateChatInfo
2021-01-30 20:40:00 +08:00
@overrides(MessageEvent)
def get_user_id(self) -> str:
return str(self.sender.id)
@overrides(MessageEvent)
2021-01-30 20:40:00 +08:00
def get_session_id(self) -> str:
return 'friend_' + self.get_user_id()
@overrides(MessageEvent)
def is_tome(self) -> bool:
return True
class TempMessage(MessageEvent):
"""临时会话消息事件"""
sender: GroupChatInfo
2021-01-30 20:40:00 +08:00
@overrides(MessageEvent)
2021-01-30 20:40:00 +08:00
def get_session_id(self) -> str:
return f'temp_{self.sender.group.id}_' + self.get_user_id()
@overrides(MessageEvent)
def is_tome(self) -> bool:
return True