🚧 update messages

This commit is contained in:
yanyongyu 2020-12-10 02:13:25 +08:00
parent a57cd396fa
commit 72653c6116
6 changed files with 112 additions and 204 deletions

View File

@ -158,6 +158,10 @@ class Event(abc.ABC, BaseModel):
def get_log_string(self) -> str: def get_log_string(self) -> str:
return f"[{self.get_event_name()}]: {self.get_event_description()}" return f"[{self.get_event_name()}]: {self.get_event_description()}"
@abc.abstractmethod
def get_user_id(self) -> str:
raise NotImplementedError
@abc.abstractmethod @abc.abstractmethod
def get_session_id(self) -> str: def get_session_id(self) -> str:
raise NotImplementedError raise NotImplementedError
@ -170,169 +174,9 @@ class Event(abc.ABC, BaseModel):
def get_plaintext(self) -> str: def get_plaintext(self) -> str:
raise NotImplementedError raise NotImplementedError
@abc.abstractmethod
# T = TypeVar("T", bound=BaseModel) def is_tome(self) -> bool:
raise NotImplementedError
# class Event(abc.ABC, Generic[T]):
# """
# Event 基类。提供上报信息的关键信息,其余信息可从原始上报消息获取。
# """
# def __init__(self, raw_event: Union[dict, T]):
# """
# :参数:
# * ``raw_event: Union[dict, T]``: 原始上报消息
# """
# self._raw_event = raw_event
# def __repr__(self) -> str:
# return f"<Event {self.self_id}: {self.name} {self.time}>"
# @property
# def raw_event(self) -> Union[dict, T]:
# """原始上报消息"""
# return self._raw_event
# @property
# @abc.abstractmethod
# def id(self) -> int:
# """事件 ID"""
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def name(self) -> str:
# """事件名称"""
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def self_id(self) -> str:
# """机器人 ID"""
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def time(self) -> int:
# """事件发生时间"""
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def type(self) -> str:
# """事件主类型"""
# raise NotImplementedError
# @type.setter
# @abc.abstractmethod
# def type(self, value) -> None:
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def detail_type(self) -> str:
# """事件详细类型"""
# raise NotImplementedError
# @detail_type.setter
# @abc.abstractmethod
# def detail_type(self, value) -> None:
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def sub_type(self) -> Optional[str]:
# """事件子类型"""
# raise NotImplementedError
# @sub_type.setter
# @abc.abstractmethod
# def sub_type(self, value) -> None:
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def user_id(self) -> Optional[int]:
# """触发事件的主体 ID"""
# raise NotImplementedError
# @user_id.setter
# @abc.abstractmethod
# def user_id(self, value) -> None:
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def group_id(self) -> Optional[int]:
# """触发事件的主体群 ID"""
# raise NotImplementedError
# @group_id.setter
# @abc.abstractmethod
# def group_id(self, value) -> None:
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def to_me(self) -> Optional[bool]:
# """事件是否为发送给机器人的消息"""
# raise NotImplementedError
# @to_me.setter
# @abc.abstractmethod
# def to_me(self, value) -> None:
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def message(self) -> Optional["Message"]:
# """消息内容"""
# raise NotImplementedError
# @message.setter
# @abc.abstractmethod
# def message(self, value) -> None:
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def reply(self) -> Optional[dict]:
# """回复的消息"""
# raise NotImplementedError
# @reply.setter
# @abc.abstractmethod
# def reply(self, value) -> None:
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def raw_message(self) -> Optional[str]:
# """原始消息"""
# raise NotImplementedError
# @raw_message.setter
# @abc.abstractmethod
# def raw_message(self, value) -> None:
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def plain_text(self) -> Optional[str]:
# """纯文本消息"""
# raise NotImplementedError
# @property
# @abc.abstractmethod
# def sender(self) -> Optional[dict]:
# """消息发送者信息"""
# raise NotImplementedError
# @sender.setter
# @abc.abstractmethod
# def sender(self, value) -> None:
# raise NotImplementedError
@dataclass @dataclass
@ -350,12 +194,12 @@ class MessageSegment(abc.ABC):
""" """
@abc.abstractmethod @abc.abstractmethod
def __str__(self): def __str__(self) -> str:
"""该消息段所代表的 str在命令匹配部分使用""" """该消息段所代表的 str在命令匹配部分使用"""
raise NotImplementedError raise NotImplementedError
@abc.abstractmethod @abc.abstractmethod
def __add__(self, other): def __add__(self, other) -> "Message":
"""你需要在这里实现不同消息段的合并: """你需要在这里实现不同消息段的合并:
比如 比如
if isinstance(other, str): if isinstance(other, str):
@ -375,10 +219,9 @@ class MessageSegment(abc.ABC):
def get(self, key, default=None): def get(self, key, default=None):
return getattr(self, key, default) return getattr(self, key, default)
@classmethod
@abc.abstractmethod @abc.abstractmethod
def text(cls, text: str) -> "MessageSegment": def is_text(self) -> bool:
return cls("text", {"text": text}) raise NotImplementedError
class Message(list, abc.ABC): class Message(list, abc.ABC):
@ -405,6 +248,24 @@ class Message(list, abc.ABC):
def __str__(self): def __str__(self):
return ''.join((str(seg) for seg in self)) return ''.join((str(seg) for seg in self))
@classmethod
def __get_validator__(cls):
yield cls._validate
@classmethod
def __modify_schema__(cls, field_schema):
field_schema.update(
examples=["foo", {
"type": "text",
"data": {
"text": "bar"
}
}])
@classmethod
def _validate(cls, value):
return cls(value)
@staticmethod @staticmethod
@abc.abstractmethod @abc.abstractmethod
def _construct( def _construct(
@ -467,8 +328,8 @@ class Message(list, abc.ABC):
""" """
index = 0 index = 0
while index < len(self): while index < len(self):
if index > 0 and self[ if index > 0 and self[index -
index - 1].type == "text" and self[index].type == "text": 1].is_text() and self[index].is_text():
self[index - 1] += self[index] self[index - 1] += self[index]
del self[index] del self[index]
else: else:
@ -482,7 +343,7 @@ class Message(list, abc.ABC):
""" """
def _concat(x: str, y: MessageSegment) -> str: def _concat(x: str, y: MessageSegment) -> str:
return f"{x} {y}" if y.type == "text" else x return f"{x} {y}" if y.is_text() else x
plain_text = reduce(_concat, self, "") plain_text = reduce(_concat, self, "")
return plain_text[1:] if plain_text else plain_text return plain_text[1:] if plain_text else plain_text

View File

@ -237,10 +237,18 @@ class CQHTTPEvent(Event):
def get_plaintext(self) -> str: def get_plaintext(self) -> str:
raise ValueError("Event has no message!") raise ValueError("Event has no message!")
@overrides(Event)
def get_user_id(self) -> str:
raise ValueError("Event has no message!")
@overrides(Event) @overrides(Event)
def get_session_id(self) -> str: def get_session_id(self) -> str:
raise ValueError("Event has no message!") raise ValueError("Event has no message!")
@overrides(Event)
def is_tome(self) -> bool:
return False
# Models # Models
class Sender(BaseModel): class Sender(BaseModel):
@ -326,10 +334,18 @@ class MessageEvent(CQHTTPEvent):
def get_plaintext(self) -> str: def get_plaintext(self) -> str:
return self.message.extract_plain_text() return self.message.extract_plain_text()
@overrides(CQHTTPEvent)
def get_user_id(self) -> str:
return str(self.user_id)
@overrides(CQHTTPEvent) @overrides(CQHTTPEvent)
def get_session_id(self) -> str: def get_session_id(self) -> str:
return str(self.user_id) return str(self.user_id)
@overrides(CQHTTPEvent)
def is_tome(self) -> bool:
return self.to_me
class PrivateMessageEvent(MessageEvent): class PrivateMessageEvent(MessageEvent):
__event__ = "message.private" __event__ = "message.private"
@ -340,7 +356,7 @@ class PrivateMessageEvent(MessageEvent):
return (f'Message {self.message_id} from {self.user_id} "' + "".join( return (f'Message {self.message_id} from {self.user_id} "' + "".join(
map( map(
lambda x: escape_tag(str(x)) lambda x: escape_tag(str(x))
if x.type == "text" else f"<le>{escape_tag(str(x))}</le>", if x.is_text() else f"<le>{escape_tag(str(x))}</le>",
self.message)) + '"') self.message)) + '"')
@ -357,7 +373,7 @@ class GroupMessageEvent(MessageEvent):
+ "".join( + "".join(
map( map(
lambda x: escape_tag(str(x)) lambda x: escape_tag(str(x))
if x.type == "text" else f"<le>{escape_tag(str(x))}</le>", if x.is_text() else f"<le>{escape_tag(str(x))}</le>",
self.message)) + '"') self.message)) + '"')
@ -389,6 +405,10 @@ class GroupAdminNoticeEvent(NoticeEvent):
user_id: int user_id: int
group_id: int group_id: int
@overrides(CQHTTPEvent)
def is_tome(self) -> bool:
return self.user_id == self.self_id
class GroupDecreaseNoticeEvent(NoticeEvent): class GroupDecreaseNoticeEvent(NoticeEvent):
__event__ = "notice.group_decrease" __event__ = "notice.group_decrease"
@ -398,6 +418,10 @@ class GroupDecreaseNoticeEvent(NoticeEvent):
group_id: int group_id: int
operator_id: int operator_id: int
@overrides(CQHTTPEvent)
def is_tome(self) -> bool:
return self.user_id == self.self_id
class GroupIncreaseNoticeEvent(NoticeEvent): class GroupIncreaseNoticeEvent(NoticeEvent):
__event__ = "notice.group_increase" __event__ = "notice.group_increase"
@ -407,6 +431,10 @@ class GroupIncreaseNoticeEvent(NoticeEvent):
group_id: int group_id: int
operator_id: int operator_id: int
@overrides(CQHTTPEvent)
def is_tome(self) -> bool:
return self.user_id == self.self_id
class GroupBanNoticeEvent(NoticeEvent): class GroupBanNoticeEvent(NoticeEvent):
__event__ = "notice.group_ban" __event__ = "notice.group_ban"
@ -417,6 +445,10 @@ class GroupBanNoticeEvent(NoticeEvent):
operator_id: int operator_id: int
duration: int duration: int
@overrides(CQHTTPEvent)
def is_tome(self) -> bool:
return self.user_id == self.self_id
class FriendAddNoticeEvent(NoticeEvent): class FriendAddNoticeEvent(NoticeEvent):
__event__ = "notice.friend_add" __event__ = "notice.friend_add"
@ -432,6 +464,10 @@ class GroupRecallNoticeEvent(NoticeEvent):
operator_id: int operator_id: int
message_id: int message_id: int
@overrides(CQHTTPEvent)
def is_tome(self) -> bool:
return self.user_id == self.self_id
class FriendRecallNoticeEvent(NoticeEvent): class FriendRecallNoticeEvent(NoticeEvent):
__event__ = "notice.friend_recall" __event__ = "notice.friend_recall"
@ -453,18 +489,30 @@ class PokeNotifyEvent(NotifyEvent):
sub_type: Literal["poke"] sub_type: Literal["poke"]
target_id: int target_id: int
@overrides(CQHTTPEvent)
def is_tome(self) -> bool:
return self.target_id == self.self_id
class LuckyKingNotifyEvent(NotifyEvent): class LuckyKingNotifyEvent(NotifyEvent):
__event__ = "notice.notify.lucky_king" __event__ = "notice.notify.lucky_king"
sub_type: Literal["lucky_king"] sub_type: Literal["lucky_king"]
target_id: int target_id: int
@overrides(CQHTTPEvent)
def is_tome(self) -> bool:
return self.target_id == self.self_id
class HonorNotifyEvent(NotifyEvent): class HonorNotifyEvent(NotifyEvent):
__event__ = "notice.notify.honor" __event__ = "notice.notify.honor"
sub_type: Literal["honor"] sub_type: Literal["honor"]
honor_type: str honor_type: str
@overrides(CQHTTPEvent)
def is_tome(self) -> bool:
return self.user_id == self.self_id
# Request Events # Request Events
class RequestEvent(CQHTTPEvent): class RequestEvent(CQHTTPEvent):

View File

@ -19,7 +19,7 @@ class MessageSegment(BaseMessageSegment):
super().__init__(type=type, data=data) super().__init__(type=type, data=data)
@overrides(BaseMessageSegment) @overrides(BaseMessageSegment)
def __str__(self): def __str__(self) -> str:
type_ = self.type type_ = self.type
data = self.data.copy() data = self.data.copy()
@ -37,6 +37,10 @@ class MessageSegment(BaseMessageSegment):
def __add__(self, other) -> "Message": def __add__(self, other) -> "Message":
return Message(self) + other return Message(self) + other
@overrides(BaseMessageSegment)
def is_text(self) -> bool:
return self.type == "text"
@staticmethod @staticmethod
def anonymous(ignore_failure: Optional[bool] = None) -> "MessageSegment": def anonymous(ignore_failure: Optional[bool] = None) -> "MessageSegment":
return MessageSegment("anonymous", {"ignore": _b2s(ignore_failure)}) return MessageSegment("anonymous", {"ignore": _b2s(ignore_failure)})

View File

@ -201,14 +201,16 @@ def USER(*user: str, perm: Permission = Permission()):
# - **说明**: 匹配任意群主群聊消息类型事件 # - **说明**: 匹配任意群主群聊消息类型事件
# """ # """
# async def _superuser(bot: "Bot", event: "Event") -> bool:
# return event.get_type(
# ) == "message" and event.user_id in bot.config.superusers
# SUPERUSER = Permission(_superuser) async def _superuser(bot: "Bot", event: "Event") -> bool:
# """ return event.get_type() == "message" and event.get_user_id(
# - **说明**: 匹配任意超级用户消息类型事件 ) in bot.config.superusers
# """
SUPERUSER = Permission(_superuser)
"""
- **说明**: 匹配任意超级用户消息类型事件
"""
# EVERYBODY = MESSAGE # EVERYBODY = MESSAGE
# """ # """
# - **说明**: 匹配任意消息类型事件 # - **说明**: 匹配任意消息类型事件

View File

@ -11,14 +11,14 @@ say = on_command("say", to_me(), permission=SUPERUSER)
@say.handle() @say.handle()
async def say_unescape(bot: Bot, event: Event, state: State): async def say_unescape(bot: Bot, event: Event, state: State):
Message = event.message.__class__ Message = event.get_message().__class__
def _unescape(message: Message, segment: MessageSegment): def _unescape(message: Message, segment: MessageSegment):
if segment.type == "text": if segment.is_text():
return message.append(str(segment)) return message.append(str(segment))
return message.append(segment) return message.append(segment)
message = reduce(_unescape, event.message, Message()) # type: ignore message = reduce(_unescape, event.get_message(), Message()) # type: ignore
await bot.send(message=message, event=event) await bot.send(message=message, event=event)
@ -27,9 +27,4 @@ echo = on_command("echo", to_me())
@echo.handle() @echo.handle()
async def echo_escape(bot: Bot, event: Event, state: State): async def echo_escape(bot: Bot, event: Event, state: State):
# Message = event.message.__class__ await bot.send(message=event.get_message(), event=event)
# MessageSegment = event.message[0].__class__
# message = Message().append( # type: ignore
# MessageSegment.text(str(event.message)))
await bot.send(message=event.message, event=event)

View File

@ -134,10 +134,10 @@ class TrieRule:
suffix = None suffix = None
message = event.get_message() message = event.get_message()
message_seg = message[0] message_seg = message[0]
if message_seg.type == "text": if message_seg.is_text():
prefix = cls.prefix.longest_prefix(str(message_seg).lstrip()) prefix = cls.prefix.longest_prefix(str(message_seg).lstrip())
message_seg_r = message[-1] message_seg_r = message[-1]
if message_seg_r.type == "text": if message_seg_r.is_text():
suffix = cls.suffix.longest_prefix( suffix = cls.suffix.longest_prefix(
str(message_seg_r).rstrip()[::-1]) str(message_seg_r).rstrip()[::-1])
@ -309,20 +309,18 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
return Rule(_regex) return Rule(_regex)
# def to_me() -> Rule: def to_me() -> Rule:
# """ """
# :说明: :说明:
# 通过 ``event.to_me`` 判断消息是否是发送给机器人 通过 ``event.to_me`` 判断事件是否与机器人有关
# :参数: :参数:
# * *
# """ """
# async def _to_me(bot: "Bot", event: "Event", state: State) -> bool: async def _to_me(bot: "Bot", event: "Event", state: State) -> bool:
# if event.get_type() != "message": return event.is_tome()
# return False
# return bool(event.to_me)
# return Rule(_to_me) return Rule(_to_me)