事件工厂111
This commit is contained in:
68
models/events/base.py
Normal file
68
models/events/base.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
基础事件模型模块
|
||||
|
||||
定义了所有 OneBot 11 事件的基类和事件类型枚举。
|
||||
"""
|
||||
from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.bot import Bot
|
||||
|
||||
|
||||
class EventType:
|
||||
"""
|
||||
事件类型枚举
|
||||
"""
|
||||
META = 'meta_event' # 元事件
|
||||
REQUEST = 'request' # 请求事件
|
||||
NOTICE = 'notice' # 通知事件
|
||||
MESSAGE = 'message' # 消息事件
|
||||
MESSAGE_SENT = 'message_sent' # 消息发送事件
|
||||
|
||||
|
||||
@dataclass
|
||||
class OneBotEvent(ABC):
|
||||
"""
|
||||
OneBot 事件基类
|
||||
所有具体的事件类型都应该继承自此类
|
||||
"""
|
||||
|
||||
time: int
|
||||
"""事件发生的时间戳"""
|
||||
|
||||
self_id: int
|
||||
"""收到事件的机器人 QQ 号"""
|
||||
|
||||
_bot: Optional["Bot"] = field(default=None, init=False)
|
||||
"""Bot 实例引用,用于快捷调用 API"""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def post_type(self) -> str:
|
||||
"""
|
||||
上报类型
|
||||
"""
|
||||
pass
|
||||
|
||||
@property
|
||||
def bot(self) -> "Bot":
|
||||
"""
|
||||
获取 Bot 实例
|
||||
|
||||
:return: Bot 实例
|
||||
:raises ValueError: 如果 Bot 实例未设置
|
||||
"""
|
||||
if self._bot is None:
|
||||
raise ValueError("Bot instance not set for this event")
|
||||
return self._bot
|
||||
|
||||
@bot.setter
|
||||
def bot(self, value: "Bot"):
|
||||
"""
|
||||
设置 Bot 实例
|
||||
|
||||
:param value: Bot 实例
|
||||
"""
|
||||
self._bot = value
|
||||
Reference in New Issue
Block a user