-
Notifications
You must be signed in to change notification settings - Fork 22
Open
Description
今天跟着教程做的时候发现固定token已经被禁用,现在需要access token做鉴权,再进一步搜索发现是qqbot的新版本通过AppID + AppSecret进行鉴权,需要使用者进行适配。
可能需要
# 卸载旧的
python3.9 -m pip uninstall qq-bot
# 安装官方新的
python3.9 -m pip install qq-botpy
重写代码:
新的config.yaml:
appid: "你的AppID"
secret: "你的AppSecret"
新的robot.py:
import asyncio
import os
import botpy
from botpy import logging
from botpy.ext.cog_yaml import read
from botpy.message import Message
# 读取 yaml 配置
test_config = read(os.path.join(os.path.dirname(__file__), "config.yaml"))
class MyClient(botpy.Client):
async def on_at_message_create(self, message: Message):
"""
监听 @机器人的消息
"""
# 打印日志
botpy.logger.info(f"收到消息: {message.content}")
# 回复消息 (注意:新版直接通过 message 对象的方法回复)
# content 填回复内容,message_reference 引用原消息(形成回复关系)
await message.reply(content="你好,我是新版机器人!", message_reference=Reference(message_id=message.id))
if __name__ == "__main__":
# 使用 AppID 和 Secret 初始化
# 这一步 SDK 内部会自动处理 AccessToken 的获取和刷新,无需手动干预
intents = botpy.Intents(public_guild_messages=True)
client = MyClient(intents=intents)
client.run(appid=test_config["appid"], secret=test_config["secret"])
Metadata
Metadata
Assignees
Labels
No labels