2021-01-30 06:10:04 +08:00
|
|
|
|
import json
|
2021-01-29 17:38:39 +08:00
|
|
|
|
from enum import Enum
|
|
|
|
|
from typing_extensions import Literal
|
2021-10-02 15:07:32 +08:00
|
|
|
|
from typing import Any, Dict, Type, Optional
|
|
|
|
|
|
|
|
|
|
from pydantic import Field, BaseModel, ValidationError
|
2021-01-29 17:38:39 +08:00
|
|
|
|
|
2021-01-29 21:19:13 +08:00
|
|
|
|
from nonebot.log import logger
|
2021-01-30 06:10:04 +08:00
|
|
|
|
from nonebot.typing import overrides
|
2021-08-05 23:11:57 +08:00
|
|
|
|
from nonebot.utils import escape_tag
|
2021-10-02 15:07:32 +08:00
|
|
|
|
from nonebot.adapters import Event as BaseEvent
|
|
|
|
|
from nonebot.adapters import Message as BaseMessage
|
2021-01-29 17:38:39 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-01 00:01:31 +08:00
|
|
|
|
class UserPermission(str, Enum):
|
|
|
|
|
"""
|
2021-02-01 13:50:14 +08:00
|
|
|
|
:说明:
|
|
|
|
|
|
2021-06-18 01:23:13 +08:00
|
|
|
|
用户权限枚举类
|
|
|
|
|
|
2021-10-02 15:07:32 +08:00
|
|
|
|
* ``OWNER``: 群主
|
|
|
|
|
* ``ADMINISTRATOR``: 群管理
|
|
|
|
|
* ``MEMBER``: 普通群成员
|
2021-02-01 00:01:31 +08:00
|
|
|
|
"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
|
|
|
|
|
OWNER = "OWNER"
|
|
|
|
|
ADMINISTRATOR = "ADMINISTRATOR"
|
|
|
|
|
MEMBER = "MEMBER"
|
2021-01-29 17:38:39 +08:00
|
|
|
|
|
|
|
|
|
|
2021-10-02 14:30:02 +08:00
|
|
|
|
class NudgeSubjectKind(str, Enum):
|
|
|
|
|
"""
|
|
|
|
|
:说明:
|
|
|
|
|
|
|
|
|
|
戳一戳类型枚举类
|
|
|
|
|
|
2021-10-02 15:07:32 +08:00
|
|
|
|
* ``Group``: 群
|
|
|
|
|
* ``Friend``: 好友
|
2021-10-02 14:30:02 +08:00
|
|
|
|
"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
|
|
|
|
|
Group = "Group"
|
|
|
|
|
Friend = "Friend"
|
2021-10-02 14:30:02 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-01 00:01:31 +08:00
|
|
|
|
class GroupInfo(BaseModel):
|
2021-01-29 17:38:39 +08:00
|
|
|
|
id: int
|
|
|
|
|
name: str
|
2021-02-01 00:01:31 +08:00
|
|
|
|
permission: UserPermission
|
2021-01-29 17:38:39 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-01 00:01:31 +08:00
|
|
|
|
class GroupChatInfo(BaseModel):
|
2021-01-29 17:38:39 +08:00
|
|
|
|
id: int
|
2021-11-22 23:21:26 +08:00
|
|
|
|
name: str = Field(alias="memberName")
|
2021-02-01 00:01:31 +08:00
|
|
|
|
permission: UserPermission
|
|
|
|
|
group: GroupInfo
|
2021-01-29 17:38:39 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-01 00:01:31 +08:00
|
|
|
|
class PrivateChatInfo(BaseModel):
|
2021-01-29 21:19:13 +08:00
|
|
|
|
id: int
|
|
|
|
|
nickname: str
|
|
|
|
|
remark: str
|
|
|
|
|
|
|
|
|
|
|
2021-10-02 14:30:02 +08:00
|
|
|
|
class NudgeSubject(BaseModel):
|
|
|
|
|
id: int
|
|
|
|
|
kind: NudgeSubjectKind
|
|
|
|
|
|
|
|
|
|
|
2021-01-29 17:38:39 +08:00
|
|
|
|
class Event(BaseEvent):
|
2021-02-01 00:01:31 +08:00
|
|
|
|
"""
|
2021-02-01 01:04:30 +08:00
|
|
|
|
mirai-api-http 协议事件,字段与 mirai-api-http 一致。各事件字段参考 `mirai-api-http 事件类型`_
|
2021-02-01 00:01:31 +08:00
|
|
|
|
|
2021-02-01 01:04:30 +08:00
|
|
|
|
.. _mirai-api-http 事件类型:
|
2021-02-01 00:01:31 +08:00
|
|
|
|
https://github.com/project-mirai/mirai-api-http/blob/master/docs/EventType.md
|
|
|
|
|
"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
|
2021-01-30 19:11:17 +08:00
|
|
|
|
self_id: int
|
2021-01-29 17:38:39 +08:00
|
|
|
|
type: str
|
|
|
|
|
|
2021-01-29 21:19:13 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
def new(cls, data: Dict[str, Any]) -> "Event":
|
2021-02-01 00:01:31 +08:00
|
|
|
|
"""
|
|
|
|
|
此事件类的工厂函数, 能够通过事件数据选择合适的子类进行序列化
|
|
|
|
|
"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
type = data["type"]
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
|
|
|
|
def all_subclasses(cls: Type[Event]):
|
|
|
|
|
return set(cls.__subclasses__()).union(
|
2021-11-22 23:21:26 +08:00
|
|
|
|
[s for c in cls.__subclasses__() for s in all_subclasses(c)]
|
|
|
|
|
)
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
|
|
|
|
event_class: Optional[Type[Event]] = None
|
|
|
|
|
for subclass in all_subclasses(cls):
|
|
|
|
|
if subclass.__name__ != type:
|
|
|
|
|
continue
|
|
|
|
|
event_class = subclass
|
|
|
|
|
|
|
|
|
|
if event_class is None:
|
|
|
|
|
return Event.parse_obj(data)
|
|
|
|
|
|
2021-07-07 14:09:03 +08:00
|
|
|
|
while event_class and issubclass(event_class, Event):
|
2021-01-29 21:19:13 +08:00
|
|
|
|
try:
|
|
|
|
|
return event_class.parse_obj(data)
|
|
|
|
|
except ValidationError as e:
|
|
|
|
|
logger.info(
|
2021-11-22 23:21:26 +08:00
|
|
|
|
f"Failed to parse {data} to class {event_class.__name__}: "
|
|
|
|
|
f"{e.errors()!r}. Fallback to parent class."
|
|
|
|
|
)
|
2021-07-07 14:09:03 +08:00
|
|
|
|
event_class = event_class.__base__ # type: ignore
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
raise ValueError(f"Failed to serialize {data}.")
|
2021-01-29 21:19:13 +08:00
|
|
|
|
|
2021-01-29 17:38:39 +08:00
|
|
|
|
@overrides(BaseEvent)
|
|
|
|
|
def get_type(self) -> Literal["message", "notice", "request", "meta_event"]:
|
2021-10-02 15:07:32 +08:00
|
|
|
|
from . import meta, notice, message, request
|
2021-11-22 23:21:26 +08:00
|
|
|
|
|
2021-01-29 21:19:13 +08:00
|
|
|
|
if isinstance(self, message.MessageEvent):
|
2021-11-22 23:21:26 +08:00
|
|
|
|
return "message"
|
2021-01-29 21:19:13 +08:00
|
|
|
|
elif isinstance(self, notice.NoticeEvent):
|
2021-11-22 23:21:26 +08:00
|
|
|
|
return "notice"
|
2021-01-29 21:19:13 +08:00
|
|
|
|
elif isinstance(self, request.RequestEvent):
|
2021-11-22 23:21:26 +08:00
|
|
|
|
return "request"
|
2021-01-29 21:19:13 +08:00
|
|
|
|
else:
|
2021-11-22 23:21:26 +08:00
|
|
|
|
return "meta_event"
|
2021-01-29 17:38:39 +08:00
|
|
|
|
|
|
|
|
|
@overrides(BaseEvent)
|
|
|
|
|
def get_event_name(self) -> str:
|
|
|
|
|
return self.type
|
|
|
|
|
|
|
|
|
|
@overrides(BaseEvent)
|
|
|
|
|
def get_event_description(self) -> str:
|
2021-08-05 23:11:57 +08:00
|
|
|
|
return escape_tag(str(self.normalize_dict()))
|
2021-01-29 17:38:39 +08:00
|
|
|
|
|
|
|
|
|
@overrides(BaseEvent)
|
|
|
|
|
def get_message(self) -> BaseMessage:
|
|
|
|
|
raise ValueError("Event has no message!")
|
|
|
|
|
|
|
|
|
|
@overrides(BaseEvent)
|
|
|
|
|
def get_plaintext(self) -> str:
|
|
|
|
|
raise ValueError("Event has no message!")
|
|
|
|
|
|
|
|
|
|
@overrides(BaseEvent)
|
|
|
|
|
def get_user_id(self) -> str:
|
|
|
|
|
raise ValueError("Event has no message!")
|
|
|
|
|
|
|
|
|
|
@overrides(BaseEvent)
|
|
|
|
|
def get_session_id(self) -> str:
|
|
|
|
|
raise ValueError("Event has no message!")
|
|
|
|
|
|
|
|
|
|
@overrides(BaseEvent)
|
|
|
|
|
def is_tome(self) -> bool:
|
|
|
|
|
return False
|
2021-01-30 06:10:04 +08:00
|
|
|
|
|
|
|
|
|
def normalize_dict(self, **kwargs) -> Dict[str, Any]:
|
2021-02-01 00:01:31 +08:00
|
|
|
|
"""
|
|
|
|
|
返回可以被json正常反序列化的结构体
|
|
|
|
|
"""
|
2021-01-30 06:10:04 +08:00
|
|
|
|
return json.loads(self.json(**kwargs))
|