mirror of
https://github.com/snowy-yang/qqbotsdk.git
synced 2026-10-21 10:08:50 +00:00
删除 Event 的 user_id/group_id/content/message_id 四个属性:它们是 "兼容群(openid)与频道两套字段名"的映射层,其中 user_id 还带着 旧版群消息 payload 顶层 from_user_id 的回退(现行格式发言人标识在 author 里,该回退永远轮不到)。Event 只保留信封语义: .raw/.data/.typed/.type/.event_id,事件体字段一律标注 payload dataclass(如 msg: GroupAtMessage)获取解析结果。 破坏性变更:event.group_id → msg.group_openid、event.message_id → msg.id,以此类推;被动回复凭据分工不变——消息事件用事件体 d.id (msg_id),其余事件用信封顶层 event_id。 迁移:examples/group_bot 入群欢迎改标注 GroupAddRobot(已确认 MusicBot 未使用被删属性);GUIDE/API 同步删除对应属性行;测试 重写为信封语义断言 + typed 解析断言。 107 测试全绿,ruff/pyright 干净。
46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
"""Event 封装:信封顶层 event_id 与事件体 d 的访问(.data/.typed)。"""
|
||
|
||
from qqbotsdk.events import Event
|
||
from qqbotsdk.payloads import GroupAddRobot
|
||
|
||
|
||
def make_event() -> Event:
|
||
return Event(
|
||
{
|
||
"op": 0,
|
||
"id": "evt-1", # 信封顶层,与 op/t/d 平级
|
||
"t": "GROUP_ADD_ROBOT",
|
||
"d": {"group_openid": "G1", "op_member_openid": "M1"},
|
||
}
|
||
)
|
||
|
||
|
||
def test_event_id_from_envelope():
|
||
event = make_event()
|
||
assert event.event_id == "evt-1"
|
||
|
||
|
||
def test_message_id_from_event_body():
|
||
event = Event(
|
||
{
|
||
"op": 0,
|
||
"id": "evt-2",
|
||
"t": "GROUP_AT_MESSAGE_CREATE",
|
||
"d": {"id": "msgid-1", "group_openid": "G1", "content": "hi"},
|
||
}
|
||
)
|
||
assert event.data["id"] == "msgid-1" # 消息事件回复用 msg_id(事件体 d.id)
|
||
assert event.event_id == "evt-2" # 信封顶层 id 另有其值
|
||
|
||
|
||
def test_typed_parses_event_body():
|
||
event = make_event()
|
||
typed = event.typed
|
||
assert isinstance(typed, GroupAddRobot)
|
||
assert typed.group_openid == "G1"
|
||
|
||
|
||
def test_event_id_defaults_to_none():
|
||
event = Event({"op": 0, "t": "READY", "d": {}})
|
||
assert event.event_id is None
|