2020-12-09 19:57:49 +08:00
|
|
|
|
import inspect
|
2020-12-09 13:20:14 +08:00
|
|
|
|
from typing_extensions import Literal
|
2020-12-09 19:57:49 +08:00
|
|
|
|
from typing import Type, List, Optional
|
2020-12-06 02:30:19 +08:00
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
from pydantic import BaseModel
|
2020-12-09 19:57:49 +08:00
|
|
|
|
from pygtrie import StringTrie
|
2020-12-09 14:39:25 +08:00
|
|
|
|
from nonebot.utils import escape_tag
|
2020-12-06 02:30:19 +08:00
|
|
|
|
from nonebot.typing import overrides
|
2020-12-09 14:39:25 +08:00
|
|
|
|
from nonebot.exception import NoLogException
|
2020-12-13 12:53:16 +08:00
|
|
|
|
from nonebot.adapters import Event as BaseEvent
|
2020-12-02 19:52:45 +08:00
|
|
|
|
|
|
|
|
|
from .message import Message
|
|
|
|
|
|
2020-12-09 14:39:25 +08:00
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
class Event(BaseEvent):
|
|
|
|
|
"""
|
2020-12-31 17:58:09 +08:00
|
|
|
|
CQHTTP 协议事件,字段与 CQHTTP 一致。各事件字段参考 `CQHTTP 文档`_
|
2020-12-13 12:53:16 +08:00
|
|
|
|
|
|
|
|
|
.. _CQHTTP 文档:
|
|
|
|
|
https://github.com/howmanybots/onebot/blob/master/README.md
|
|
|
|
|
"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = ""
|
2020-12-09 11:13:37 +08:00
|
|
|
|
time: int
|
|
|
|
|
self_id: int
|
2020-12-09 17:51:24 +08:00
|
|
|
|
post_type: Literal["message", "notice", "request", "meta_event"]
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(BaseEvent)
|
2020-12-09 14:39:25 +08:00
|
|
|
|
def get_type(self) -> Literal["message", "notice", "request", "meta_event"]:
|
2020-12-09 17:51:24 +08:00
|
|
|
|
return self.post_type
|
2020-12-09 14:39:25 +08:00
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(BaseEvent)
|
2020-12-09 14:39:25 +08:00
|
|
|
|
def get_event_name(self) -> str:
|
|
|
|
|
return self.post_type
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(BaseEvent)
|
2020-12-09 14:39:25 +08:00
|
|
|
|
def get_event_description(self) -> str:
|
|
|
|
|
return str(self.dict())
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(BaseEvent)
|
2020-12-09 19:57:49 +08:00
|
|
|
|
def get_message(self) -> Message:
|
|
|
|
|
raise ValueError("Event has no message!")
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(BaseEvent)
|
2020-12-09 19:57:49 +08:00
|
|
|
|
def get_plaintext(self) -> str:
|
|
|
|
|
raise ValueError("Event has no message!")
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(BaseEvent)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
raise ValueError("Event has no message!")
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(BaseEvent)
|
2020-12-10 00:39:43 +08:00
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
raise ValueError("Event has no message!")
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(BaseEvent)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return False
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
# Models
|
2020-12-09 11:13:37 +08:00
|
|
|
|
class Sender(BaseModel):
|
|
|
|
|
user_id: Optional[int] = None
|
|
|
|
|
nickname: Optional[str] = None
|
|
|
|
|
sex: Optional[str] = None
|
|
|
|
|
age: Optional[int] = None
|
|
|
|
|
card: Optional[str] = None
|
|
|
|
|
area: Optional[str] = None
|
|
|
|
|
level: Optional[str] = None
|
|
|
|
|
role: Optional[str] = None
|
|
|
|
|
title: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
|
extra = "allow"
|
|
|
|
|
|
|
|
|
|
|
2020-12-09 17:51:24 +08:00
|
|
|
|
class Reply(BaseModel):
|
|
|
|
|
time: int
|
|
|
|
|
message_type: str
|
|
|
|
|
message_id: int
|
|
|
|
|
real_id: int
|
|
|
|
|
sender: Sender
|
|
|
|
|
message: Message
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
|
extra = "allow"
|
|
|
|
|
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
class Anonymous(BaseModel):
|
|
|
|
|
id: int
|
|
|
|
|
name: str
|
|
|
|
|
flag: str
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
|
extra = "allow"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class File(BaseModel):
|
|
|
|
|
id: str
|
|
|
|
|
name: str
|
|
|
|
|
size: int
|
|
|
|
|
busid: int
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
|
extra = "allow"
|
|
|
|
|
|
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
class Status(BaseModel):
|
|
|
|
|
online: bool
|
|
|
|
|
good: bool
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
|
extra = "allow"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Message Events
|
2020-12-13 12:53:16 +08:00
|
|
|
|
class MessageEvent(Event):
|
|
|
|
|
"""消息事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "message"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
post_type: Literal["message"]
|
2020-12-09 17:51:24 +08:00
|
|
|
|
sub_type: str
|
|
|
|
|
user_id: int
|
2020-12-09 11:13:37 +08:00
|
|
|
|
message_type: str
|
2020-12-09 17:51:24 +08:00
|
|
|
|
message_id: int
|
|
|
|
|
message: Message
|
|
|
|
|
raw_message: str
|
|
|
|
|
font: int
|
|
|
|
|
sender: Sender
|
|
|
|
|
to_me: bool = False
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明: 消息是否与机器人有关
|
|
|
|
|
|
|
|
|
|
:类型: ``bool``
|
|
|
|
|
"""
|
2020-12-09 17:51:24 +08:00
|
|
|
|
reply: Optional[Reply] = None
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明: 消息中提取的回复消息,内容为 ``get_msg`` API 返回结果
|
|
|
|
|
|
|
|
|
|
:类型: ``Optional[Reply]``
|
|
|
|
|
"""
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-09 14:39:25 +08:00
|
|
|
|
def get_event_name(self) -> str:
|
|
|
|
|
sub_type = getattr(self, "sub_type", None)
|
|
|
|
|
return f"{self.post_type}.{self.message_type}" + (f".{sub_type}"
|
|
|
|
|
if sub_type else "")
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-09 19:57:49 +08:00
|
|
|
|
def get_message(self) -> Message:
|
|
|
|
|
return self.message
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-09 19:57:49 +08:00
|
|
|
|
def get_plaintext(self) -> str:
|
|
|
|
|
return self.message.extract_plain_text()
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-10 00:39:43 +08:00
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return self.to_me
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
|
|
|
|
class PrivateMessageEvent(MessageEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""私聊消息"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "message.private"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
message_type: Literal["private"]
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-09 14:39:25 +08:00
|
|
|
|
def get_event_description(self) -> str:
|
|
|
|
|
return (f'Message {self.message_id} from {self.user_id} "' + "".join(
|
|
|
|
|
map(
|
|
|
|
|
lambda x: escape_tag(str(x))
|
2020-12-10 02:13:25 +08:00
|
|
|
|
if x.is_text() else f"<le>{escape_tag(str(x))}</le>",
|
2020-12-09 14:39:25 +08:00
|
|
|
|
self.message)) + '"')
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
|
|
|
|
class GroupMessageEvent(MessageEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""群消息"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "message.group"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
message_type: Literal["group"]
|
2020-12-09 14:39:25 +08:00
|
|
|
|
group_id: int
|
2021-01-01 15:16:55 +08:00
|
|
|
|
anonymous: Optional[Anonymous] = None
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-09 14:39:25 +08:00
|
|
|
|
def get_event_description(self) -> str:
|
|
|
|
|
return (
|
|
|
|
|
f'Message {self.message_id} from {self.user_id}@[群:{self.group_id}] "'
|
|
|
|
|
+ "".join(
|
|
|
|
|
map(
|
|
|
|
|
lambda x: escape_tag(str(x))
|
2020-12-10 02:13:25 +08:00
|
|
|
|
if x.is_text() else f"<le>{escape_tag(str(x))}</le>",
|
2020-12-09 14:39:25 +08:00
|
|
|
|
self.message)) + '"')
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
# Notice Events
|
2020-12-13 12:53:16 +08:00
|
|
|
|
class NoticeEvent(Event):
|
|
|
|
|
"""通知事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
post_type: Literal["notice"]
|
|
|
|
|
notice_type: str
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-09 14:39:25 +08:00
|
|
|
|
def get_event_name(self) -> str:
|
|
|
|
|
sub_type = getattr(self, "sub_type", None)
|
|
|
|
|
return f"{self.post_type}.{self.notice_type}" + (f".{sub_type}"
|
|
|
|
|
if sub_type else "")
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
class GroupUploadNoticeEvent(NoticeEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""群文件上传事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.group_upload"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
notice_type: Literal["group_upload"]
|
|
|
|
|
user_id: int
|
|
|
|
|
group_id: int
|
|
|
|
|
file: File
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
class GroupAdminNoticeEvent(NoticeEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""群管理员变动"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.group_admin"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
notice_type: Literal["group_admin"]
|
|
|
|
|
sub_type: str
|
|
|
|
|
user_id: int
|
|
|
|
|
group_id: int
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return self.user_id == self.self_id
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
class GroupDecreaseNoticeEvent(NoticeEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""群成员减少事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.group_decrease"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
notice_type: Literal["group_decrease"]
|
|
|
|
|
sub_type: str
|
|
|
|
|
user_id: int
|
|
|
|
|
group_id: int
|
|
|
|
|
operator_id: int
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return self.user_id == self.self_id
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
class GroupIncreaseNoticeEvent(NoticeEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""群成员增加事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.group_increase"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
notice_type: Literal["group_increase"]
|
|
|
|
|
sub_type: str
|
|
|
|
|
user_id: int
|
|
|
|
|
group_id: int
|
|
|
|
|
operator_id: int
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return self.user_id == self.self_id
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
class GroupBanNoticeEvent(NoticeEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""群禁言事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.group_ban"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
notice_type: Literal["group_ban"]
|
|
|
|
|
sub_type: str
|
|
|
|
|
user_id: int
|
|
|
|
|
group_id: int
|
|
|
|
|
operator_id: int
|
|
|
|
|
duration: int
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return self.user_id == self.self_id
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
class FriendAddNoticeEvent(NoticeEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""好友添加事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.friend_add"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
notice_type: Literal["friend_add"]
|
|
|
|
|
user_id: int
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
class GroupRecallNoticeEvent(NoticeEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""群消息撤回事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.group_recall"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
notice_type: Literal["group_recall"]
|
|
|
|
|
user_id: int
|
|
|
|
|
group_id: int
|
|
|
|
|
operator_id: int
|
|
|
|
|
message_id: int
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return self.user_id == self.self_id
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
class FriendRecallNoticeEvent(NoticeEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""好友消息撤回事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.friend_recall"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
notice_type: Literal["friend_recall"]
|
|
|
|
|
user_id: int
|
|
|
|
|
message_id: int
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
|
|
|
|
class NotifyEvent(NoticeEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""提醒事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.notify"
|
2020-12-09 11:13:37 +08:00
|
|
|
|
notice_type: Literal["notify"]
|
|
|
|
|
sub_type: str
|
|
|
|
|
user_id: int
|
|
|
|
|
group_id: int
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(NoticeEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
|
|
|
|
class PokeNotifyEvent(NotifyEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""戳一戳提醒事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.notify.poke"
|
|
|
|
|
sub_type: Literal["poke"]
|
2020-12-09 11:13:37 +08:00
|
|
|
|
target_id: int
|
2021-01-01 18:01:05 +08:00
|
|
|
|
group_id: Optional[int] = None
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return self.target_id == self.self_id
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
|
|
|
|
class LuckyKingNotifyEvent(NotifyEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""群红包运气王提醒事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.notify.lucky_king"
|
|
|
|
|
sub_type: Literal["lucky_king"]
|
2020-12-09 11:13:37 +08:00
|
|
|
|
target_id: int
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return self.target_id == self.self_id
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(NotifyEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.target_id)
|
|
|
|
|
|
|
|
|
|
@overrides(NotifyEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.target_id)
|
|
|
|
|
|
2020-12-09 11:13:37 +08:00
|
|
|
|
|
|
|
|
|
class HonorNotifyEvent(NotifyEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""群荣誉变更提醒事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "notice.notify.honor"
|
|
|
|
|
sub_type: Literal["honor"]
|
2020-12-09 11:13:37 +08:00
|
|
|
|
honor_type: str
|
2020-12-09 13:20:14 +08:00
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-10 02:13:25 +08:00
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return self.user_id == self.self_id
|
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
|
|
|
|
|
# Request Events
|
2020-12-13 12:53:16 +08:00
|
|
|
|
class RequestEvent(Event):
|
|
|
|
|
"""请求事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "request"
|
2020-12-09 13:20:14 +08:00
|
|
|
|
post_type: Literal["request"]
|
|
|
|
|
request_type: str
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-09 14:39:25 +08:00
|
|
|
|
def get_event_name(self) -> str:
|
|
|
|
|
sub_type = getattr(self, "sub_type", None)
|
|
|
|
|
return f"{self.post_type}.{self.request_type}" + (f".{sub_type}"
|
|
|
|
|
if sub_type else "")
|
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
|
|
|
|
|
class FriendRequestEvent(RequestEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""加好友请求事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "request.friend"
|
2020-12-09 13:20:14 +08:00
|
|
|
|
request_type: Literal["friend"]
|
|
|
|
|
user_id: int
|
|
|
|
|
comment: str
|
|
|
|
|
flag: str
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(RequestEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(RequestEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
|
|
|
|
|
class GroupRequestEvent(RequestEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""加群请求/邀请事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "request.group"
|
2020-12-09 13:20:14 +08:00
|
|
|
|
request_type: Literal["group"]
|
|
|
|
|
sub_type: str
|
|
|
|
|
group_id: int
|
|
|
|
|
user_id: int
|
|
|
|
|
comment: str
|
|
|
|
|
flag: str
|
|
|
|
|
|
2021-01-16 11:10:54 +08:00
|
|
|
|
@overrides(RequestEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
|
|
|
|
@overrides(RequestEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
return str(self.user_id)
|
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
|
2020-12-09 14:39:25 +08:00
|
|
|
|
# Meta Events
|
2020-12-13 12:53:16 +08:00
|
|
|
|
class MetaEvent(Event):
|
|
|
|
|
"""元事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "meta_event"
|
2020-12-09 13:20:14 +08:00
|
|
|
|
post_type: Literal["meta_event"]
|
|
|
|
|
meta_event_type: str
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-09 14:39:25 +08:00
|
|
|
|
def get_event_name(self) -> str:
|
|
|
|
|
sub_type = getattr(self, "sub_type", None)
|
|
|
|
|
return f"{self.post_type}.{self.meta_event_type}" + (f".{sub_type}" if
|
|
|
|
|
sub_type else "")
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
@overrides(Event)
|
2020-12-09 14:39:25 +08:00
|
|
|
|
def get_log_string(self) -> str:
|
|
|
|
|
raise NoLogException
|
|
|
|
|
|
2020-12-09 13:20:14 +08:00
|
|
|
|
|
|
|
|
|
class LifecycleMetaEvent(MetaEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""生命周期元事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "meta_event.lifecycle"
|
2020-12-09 13:20:14 +08:00
|
|
|
|
meta_event_type: Literal["lifecycle"]
|
|
|
|
|
sub_type: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HeartbeatMetaEvent(MetaEvent):
|
2020-12-13 12:53:16 +08:00
|
|
|
|
"""心跳元事件"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
__event__ = "meta_event.heartbeat"
|
2020-12-09 13:20:14 +08:00
|
|
|
|
meta_event_type: Literal["heartbeat"]
|
|
|
|
|
status: Status
|
|
|
|
|
interval: int
|
2020-12-09 19:57:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_t = StringTrie(separator=".")
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
# define `model` first to avoid globals changing while `for`
|
2020-12-09 19:57:49 +08:00
|
|
|
|
model = None
|
|
|
|
|
for model in globals().values():
|
2020-12-13 12:53:16 +08:00
|
|
|
|
if not inspect.isclass(model) or not issubclass(model, Event):
|
2020-12-09 19:57:49 +08:00
|
|
|
|
continue
|
|
|
|
|
_t["." + model.__event__] = model
|
|
|
|
|
|
|
|
|
|
|
2020-12-13 12:53:16 +08:00
|
|
|
|
def get_event_model(event_name) -> List[Type[Event]]:
|
|
|
|
|
"""
|
|
|
|
|
:说明:
|
|
|
|
|
|
|
|
|
|
根据事件名获取对应 ``Event Model`` 及 ``FallBack Event Model`` 列表
|
|
|
|
|
|
|
|
|
|
:返回:
|
|
|
|
|
|
|
|
|
|
- ``List[Type[Event]]``
|
|
|
|
|
"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
return [model.value for model in _t.prefixes("." + event_name)][::-1]
|
2020-12-13 12:53:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
"Event", "MessageEvent", "PrivateMessageEvent", "GroupMessageEvent",
|
|
|
|
|
"NoticeEvent", "GroupUploadNoticeEvent", "GroupAdminNoticeEvent",
|
|
|
|
|
"GroupDecreaseNoticeEvent", "GroupIncreaseNoticeEvent",
|
|
|
|
|
"GroupBanNoticeEvent", "FriendAddNoticeEvent", "GroupRecallNoticeEvent",
|
|
|
|
|
"FriendRecallNoticeEvent", "NotifyEvent", "PokeNotifyEvent",
|
|
|
|
|
"LuckyKingNotifyEvent", "HonorNotifyEvent", "RequestEvent",
|
|
|
|
|
"FriendRequestEvent", "GroupRequestEvent", "MetaEvent",
|
|
|
|
|
"LifecycleMetaEvent", "HeartbeatMetaEvent", "get_event_model"
|
|
|
|
|
]
|