mirror of
https://github.com/snowy-yang/qqbotsdk.git
synced 2026-10-21 11:04:48 +00:00
refactor!: Bot 门面接管启动,移除 main/run_loop 与控制台脚本入口
bot.run() 同步启动、bot.start() 嵌入外部 asyncio 应用; bot.call_api 通用 REST、bot.api 类型化 API 面、bot.services 自定义组件注入; services 登记扩到 8 项(含 Bot 自身,可标注注入)。
This commit is contained in:
1 parent
1f91b1acca
commit
0515c6e4c0
10 files changed
+317
-697
No files matched your search
@@ -10,7 +10,7 @@
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from qqbotsdk import EventEmitter, main
|
||||
from qqbotsdk import Bot
|
||||
from qqbotsdk.api import BotApi, button, keyboard
|
||||
from qqbotsdk.events import Event
|
||||
from qqbotsdk.payloads import (
|
||||
@@ -21,14 +21,24 @@ from qqbotsdk.payloads import (
|
||||
Interaction,
|
||||
)
|
||||
|
||||
ee = EventEmitter()
|
||||
bot = Bot()
|
||||
|
||||
|
||||
@ee.on("GROUP_AT_MESSAGE_CREATE")
|
||||
async def on_group_message(msg: GroupAtMessage, api: BotApi) -> None:
|
||||
@bot.on("GROUP_AT_MESSAGE_CREATE")
|
||||
async def on_group_message(msg: GroupAtMessage, api: BotApi, bot: Bot) -> None:
|
||||
if not (msg.group_openid and msg.id):
|
||||
return
|
||||
|
||||
# "/me":经 bot.call_api 调通用 REST 接口(未封装接口也走这里)
|
||||
if (msg.content or "").strip() == "/me":
|
||||
me = await bot.call_api("GET", "/users/@me")
|
||||
await api.post_group_message(
|
||||
msg.group_openid,
|
||||
content=f"我是 {me.get('username', '机器人')}",
|
||||
msg_id=msg.id,
|
||||
)
|
||||
return
|
||||
|
||||
# "/img <图片url>":上传富媒体后随消息发出(msg_type 自动置为 7)
|
||||
if (msg.content or "").startswith("/img "):
|
||||
url = (msg.content or "").removeprefix("/img ").strip()
|
||||
@@ -64,7 +74,7 @@ async def on_group_message(msg: GroupAtMessage, api: BotApi) -> None:
|
||||
)
|
||||
|
||||
|
||||
@ee.on("INTERACTION_CREATE")
|
||||
@bot.on("INTERACTION_CREATE")
|
||||
async def on_button_click(inter: Interaction, event: Event, api: BotApi) -> None:
|
||||
if inter.chat_type != 1 or not inter.group_openid:
|
||||
return # 本示例只处理群聊的按钮回调
|
||||
@@ -79,7 +89,7 @@ async def on_button_click(inter: Interaction, event: Event, api: BotApi) -> None
|
||||
)
|
||||
|
||||
|
||||
@ee.on("GROUP_ADD_ROBOT")
|
||||
@bot.on("GROUP_ADD_ROBOT")
|
||||
async def on_robot_added(added: GroupAddRobot, event: Event, api: BotApi) -> None:
|
||||
# 入群是事件而非消息:用信封顶层的事件 id 作 event_id 被动回复(拿不到则跳过)
|
||||
if not (added.group_openid and event.event_id):
|
||||
@@ -92,7 +102,7 @@ async def on_robot_added(added: GroupAddRobot, event: Event, api: BotApi) -> Non
|
||||
)
|
||||
|
||||
|
||||
@ee.on("C2C_MESSAGE_CREATE")
|
||||
@bot.on("C2C_MESSAGE_CREATE")
|
||||
async def on_c2c_message(msg: C2CMessage, api: BotApi) -> None:
|
||||
user_openid = msg.user_openid
|
||||
if not (user_openid and msg.id):
|
||||
@@ -103,10 +113,10 @@ async def on_c2c_message(msg: C2CMessage, api: BotApi) -> None:
|
||||
)
|
||||
|
||||
|
||||
@ee.on("FRIEND_ADD")
|
||||
@bot.on("FRIEND_ADD")
|
||||
async def on_friend_add(added: FriendAdd) -> None:
|
||||
logger.info(f"新增用户:{added.openid}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(ee)
|
||||
bot.run()
|
||||
Reference in new issue
Block a user