Discord Bot示例代码触发PrivilegedIntentsRequired错误求助
解决Discord Bot的PrivilegedIntentsRequired错误
问题详情
运行discord.py官方示例代码时,开启message_content Intent后触发PrivilegedIntentsRequired错误,移除该Intent则无法读取用户消息。已将Bot设为管理员权限并重置令牌,问题仍未解决。
运行代码
import discord from config import __BOTTOKEN__ as __TOKEN__ intents = discord.Intents.default() intents.message_content = True # 开启此Intent后触发错误,移除则无法读取用户消息 client = discord.Client(intents=intents) @client.event async def on_ready(): print(f'We have logged in as {client.user}') @client.event async def on_message(message): if message.author == client.user: return if message.content.startswith('$hello'): await message.channel.send('Hello!') client.run(__TOKEN__)
报错信息(中文翻译)
2024-03-14 18:34:51 INFO discord.client 使用静态令牌登录 回溯(最近的调用最后): 文件 "/home/runner/Discord-bot/main.py",第9行,在<module>中 main() 文件 "/home/runner/Discord-bot/main.py",第4行,在main中 import example 文件 "/home/runner/Discord-bot/example.py",第21行,在<module>中 client.run(__TOKEN__) 文件 "/home/runner/Discord-bot/.pythonlibs/lib/python3.10/site-packages/discord/client.py",第860行,在run中 asyncio.run(runner()) 文件 "/nix/store/8w6mm5q1n7i7cs1933im5vkbgvjlglfn-python3-3.10.13/lib/python3.10/asyncio/runners.py",第44行,在run中 return loop.run_until_complete(main) 文件 "/nix/store/8w6mm5q1n7i7cs1933im5vkbgvjlglfn-python3-3.10.13/lib/python3.10/asyncio/base_events.py",第649行,在run_until_complete中 return future.result() 文件 "/home/runner/Discord-bot/.pythonlibs/lib/python3.10/site-packages/discord/client.py",第849行,在runner中 await self.start(token, reconnect=reconnect) 文件 "/home/runner/Discord-bot/.pythonlibs/lib/python3.10/site-packages/discord/client.py",第778行,在start中 await self.connect(reconnect=reconnect) 文件 "/home/runner/Discord-bot/.pythonlibs/lib/python3.10/site-packages/discord/client.py",第704行,在connect中 raise PrivilegedIntentsRequired(exc.shard_id) from None discord.errors.PrivilegedIntentsRequired:分片ID None正在请求未在开发者门户中明确启用的特权Intent。建议前往开发者应用页面明确启用应用的特权Intent。如果无法启用,请考虑禁用这些特权Intent。
解决步骤
- 开启后台的Message Content特权Intent:进入你的Bot应用页面,切换到「Bot」标签页,找到「特权网关Intent」区域,开启「消息内容Intent」并保存设置。
- 确认操作的应用与代码令牌匹配:检查后台操作的应用是否对应代码中使用的Bot令牌,避免操作错误应用。
- 验证代码Intent配置:确保代码中仅开启
message_content这一个特权Intent,不要启用其他未授权的特权Intent。 - 等待设置同步后重启Bot:后台设置变更可能需要几分钟同步,修改后等待片刻再重启Bot程序。
内容的提问来源于stack exchange,提问作者Jacob Glik
相关产品推荐
相关产品推荐

