2021-08-29 00:24:28 +08:00
|
|
|
|
import abc
|
2024-04-16 00:33:48 +08:00
|
|
|
|
from typing import Any, TypeVar
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
from nonebot.utils import DataclassEncoder
|
2024-01-26 11:12:57 +08:00
|
|
|
|
from nonebot.compat import PYDANTIC_V2, ConfigDict
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from .message import Message
|
2022-01-15 21:27:43 +08:00
|
|
|
|
|
2022-05-22 19:42:30 +08:00
|
|
|
|
E = TypeVar("E", bound="Event")
|
|
|
|
|
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
|
|
|
|
class Event(abc.ABC, BaseModel):
|
|
|
|
|
"""Event 基类。提供获取关键信息的方法,其余信息可直接获取。"""
|
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
if PYDANTIC_V2: # pragma: pydantic-v2
|
|
|
|
|
model_config = ConfigDict(extra="allow")
|
|
|
|
|
else: # pragma: pydantic-v1
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
class Config(ConfigDict):
|
|
|
|
|
extra = "allow" # type: ignore
|
2024-03-07 14:57:26 +08:00
|
|
|
|
json_encoders = {Message: DataclassEncoder} # noqa: RUF012
|
2024-01-26 11:12:57 +08:00
|
|
|
|
|
|
|
|
|
if not PYDANTIC_V2: # pragma: pydantic-v1
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def validate(cls: type["E"], value: Any) -> "E":
|
2024-01-26 11:12:57 +08:00
|
|
|
|
if isinstance(value, Event) and not isinstance(value, cls):
|
|
|
|
|
raise TypeError(f"{value} is incompatible with Event type {cls}")
|
|
|
|
|
return super().validate(value)
|
2022-05-22 19:42:30 +08:00
|
|
|
|
|
2021-08-29 00:24:28 +08:00
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def get_type(self) -> str:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""获取事件类型的方法,类型通常为 NoneBot 内置的四种类型。"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def get_event_name(self) -> str:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""获取事件名称的方法。"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def get_event_description(self) -> str:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""获取事件描述的方法,通常为事件具体内容。"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
|
return f"[{self.get_event_name()}]: {self.get_event_description()}"
|
|
|
|
|
|
|
|
|
|
def get_log_string(self) -> str:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""获取事件日志信息的方法。
|
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
通常你不需要修改这个方法,只有当希望 NoneBot 隐藏该事件日志时,
|
|
|
|
|
可以抛出 `NoLogException` 异常。
|
2021-08-29 00:24:28 +08:00
|
|
|
|
|
2022-01-12 18:45:35 +08:00
|
|
|
|
异常:
|
2023-06-24 14:47:35 +08:00
|
|
|
|
NoLogException: 希望 NoneBot 隐藏该事件日志
|
2021-08-29 00:24:28 +08:00
|
|
|
|
"""
|
|
|
|
|
return f"[{self.get_event_name()}]: {self.get_event_description()}"
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def get_user_id(self) -> str:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""获取事件主体 id 的方法,通常是用户 id 。"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def get_session_id(self) -> str:
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""获取会话 id 的方法,用于判断当前事件属于哪一个会话,
|
|
|
|
|
通常是用户 id、群组 id 组合。
|
|
|
|
|
"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def get_message(self) -> "Message":
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""获取事件消息内容的方法。"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
def get_plaintext(self) -> str:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""获取消息纯文本的方法。
|
|
|
|
|
|
|
|
|
|
通常不需要修改,默认通过 `get_message().extract_plain_text` 获取。
|
2021-08-29 00:24:28 +08:00
|
|
|
|
"""
|
|
|
|
|
return self.get_message().extract_plain_text()
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def is_tome(self) -> bool:
|
2022-01-20 14:49:46 +08:00
|
|
|
|
"""获取事件是否与机器人有关的方法。"""
|
2021-08-29 00:24:28 +08:00
|
|
|
|
raise NotImplementedError
|