mirror of
https://github.com/snowy-yang/qqbotsdk.git
synced 2026-10-21 09:05:21 +00:00
fix: 示例与文档跟上 handler 注入规则——FRIEND_ADD 改标注 FriendAdd
0f3ed09 后 dict 标注不可注入,示例 handler 每次触发都被异常隔离吞掉;
官方 FRIEND_ADD 字段名为 openid,原 d.get('user_openid') 写法在重构
前也取不到值。payloads 模块 docstring 同步改为现行语义(未知事件只能
经 Event.typed 拿 dict)。
This commit is contained in:
1 parent
0f3ed09233
commit
e5dd805d19
2 files changed
+12
-6
No files matched your search
@@ -13,7 +13,13 @@ from loguru import logger
|
||||
from qqbotsdk import EventEmitter, main
|
||||
from qqbotsdk.api import BotApi, button, keyboard
|
||||
from qqbotsdk.events import Event
|
||||
from qqbotsdk.payloads import C2CMessage, GroupAddRobot, GroupAtMessage, Interaction
|
||||
from qqbotsdk.payloads import (
|
||||
C2CMessage,
|
||||
FriendAdd,
|
||||
GroupAddRobot,
|
||||
GroupAtMessage,
|
||||
Interaction,
|
||||
)
|
||||
|
||||
ee = EventEmitter()
|
||||
|
||||
@@ -98,8 +104,8 @@ async def on_c2c_message(msg: C2CMessage, api: BotApi) -> None:
|
||||
|
||||
|
||||
@ee.on("FRIEND_ADD")
|
||||
async def on_friend_add(d: dict) -> None:
|
||||
logger.info(f"新增用户:{d.get('user_openid')}")
|
||||
async def on_friend_add(added: FriendAdd) -> None:
|
||||
logger.info(f"新增用户:{added.openid}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
"""事件 payload 的 dataclass 定义与解析。
|
||||
|
||||
handler 直接标注具体类型即可拿到解析后的对象(如
|
||||
`async def on_msg(msg: GroupAtMessage)`);未收录的事件类型
|
||||
由 emitter 回退传原始 dict,新增事件只需在这里加 dataclass
|
||||
并登记到 EVENT_TYPES。
|
||||
`async def on_msg(msg: GroupAtMessage)`);未收录的事件类型没有可标注的
|
||||
dataclass,只能经 `Event.typed` 拿到原始 dict——新增事件只需在这里加
|
||||
dataclass 并登记到 EVENT_TYPES。
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, fields
|
||||
|
||||
Reference in new issue
Block a user