抽象send方法,添加注释

This commit is contained in:
2026-01-01 17:58:17 +08:00
parent 9146ffbb1a
commit 046dd0860f
9 changed files with 366 additions and 38 deletions

View File

@@ -1 +1,11 @@
# TODO 数据类型
"""
基础数据模型模块
"""
class BaseModel:
"""
基础模型类
"""
pass

View File

@@ -1,23 +1,54 @@
"""
事件模型模块
定义了 Event 类和 MessageSegment 类,用于封装 OneBot 11 的上报事件和消息段。
"""
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, TYPE_CHECKING
from .sender import Sender
if TYPE_CHECKING:
from core.bot import Bot
@dataclass
class MessageSegment:
"""
消息段,对应 OneBot 11 标准中的消息段对象
"""
type: str
"""消息段类型,如 text, image, at 等"""
data: Dict[str, Any]
"""消息段数据"""
@property
def text(self) -> str:
"""
获取文本内容(仅当 type 为 text 时有效)
:return: 文本内容
"""
return self.data.get("text", "") if self.type == "text" else ""
@property
def image_url(self) -> str:
"""
获取图片 URL仅当 type 为 image 时有效)
:return: 图片 URL
"""
return self.data.get("url", "") if self.type == "image" else ""
def is_at(self, user_id: int = None) -> bool:
"""
判断是否为 @某人
:param user_id: 指定的 QQ 号,如果为 None 则只判断是否为 at 类型
:return: 是否匹配
"""
if self.type != "at":
return False
if user_id is None:
@@ -30,31 +61,93 @@ class MessageSegment:
@dataclass
class Event:
"""
事件类,封装了 OneBot 11 的上报事件
"""
post_type: str
"""上报类型: message, notice, request, meta_event"""
self_id: int
"""收到消息的机器人 QQ 号"""
time: int
"""事件发生的时间戳"""
# --- 消息事件字段 ---
message_type: Optional[str] = None
"""消息类型: group, private"""
sub_type: Optional[str] = None
"""消息子类型"""
message_id: Optional[int] = None
"""消息 ID"""
user_id: Optional[int] = None
"""发送者 QQ 号"""
raw_message: Optional[str] = None
"""原始消息内容"""
message: List[MessageSegment] = field(default_factory=list)
"""消息内容列表"""
sender: Optional[Sender] = None
"""发送者信息"""
group_id: Optional[int] = None
"""群号"""
target_id: Optional[int] = None
"""目标 QQ 号"""
# --- 通知事件字段 ---
notice_type: Optional[str] = None
operator_id: Optional[int] = None
duration: Optional[int] = None
honor_type: Optional[str] = None
"""通知类型"""
operator_id: Optional[int] = None
"""操作者 QQ 号"""
duration: Optional[int] = None
"""时长"""
honor_type: Optional[str] = None
"""荣誉类型"""
# --- 请求事件字段 ---
request_type: Optional[str] = None
"""请求类型"""
flag: Optional[str] = None
"""请求 flag"""
comment: Optional[str] = None
"""验证信息"""
# 注入的 Bot 实例
bot: Optional["Bot"] = field(default=None, init=False)
"""关联的 Bot 实例"""
async def reply(self, message: str) -> dict:
"""
快捷回复消息
:param message: 回复内容
:return: API 响应结果
"""
if not self.bot:
return {"status": "failed", "msg": "Bot instance not attached to event"}
return await self.bot.send(self, message)
@classmethod
def from_dict(cls, data: dict):
"""
从字典创建 Event 对象
:param data: 原始事件数据字典
:return: Event 对象
"""
raw_msg_array = data.get("message")
segments = []
if isinstance(raw_msg_array, list):
@@ -84,12 +177,15 @@ class Event:
# --- 快捷判断工具 ---
@property
def is_message(self) -> bool:
"""是否为消息事件"""
return self.post_type == "message"
@property
def is_notice(self) -> bool:
"""是否为通知事件"""
return self.post_type == "notice"
@property
def is_request(self) -> bool:
"""是否为请求事件"""
return self.post_type == "request"

View File

@@ -1,17 +1,42 @@
"""
发送者模型模块
定义了 Sender 类,用于封装 OneBot 11 的发送者信息。
"""
from dataclasses import dataclass
from typing import Optional
@dataclass
class Sender:
"""
发送者信息类,对应 OneBot 11 标准中的 sender 字段
"""
user_id: int
"""发送者 QQ 号"""
nickname: str
"""昵称"""
sex: str = "unknown"
"""性别male 或 female 或 unknown"""
age: int = 0
"""年龄"""
# 群聊特有字段
card: Optional[str] = None # 群名片
area: Optional[str] = None # 地区
level: Optional[str] = None # 等级
role: Optional[str] = None # 角色: owner/admin/member
title: Optional[str] = None # 专属头衔
card: Optional[str] = None
"""群名片/备注"""
area: Optional[str] = None
"""地区"""
level: Optional[str] = None
"""成员等级"""
role: Optional[str] = None
"""角色owner 或 admin 或 member"""
title: Optional[str] = None
"""专属头衔"""