Discord机器人启动后崩溃:特权意图未启用问题求助
问题:Discord机器人启动触发PrivilegedIntentsRequired错误
运行下述Discord机器人代码时,启动即刻触发严重错误,代码逻辑看似正常。
代码示例
from discord import Intents from discord.ext import commands from dotenv import load_dotenv import os import replicate os.environ["DISCORD_TOKEN"] = "TOKEN" load_dotenv() intents = Intents.default() intents.message_content = True bot = commands.Bot( command_prefix="!", description="Runs models on Replicate!", intents=intents, ) @bot.command(aliases=["sd"]) async def stable_diffusion(ctx, *, prompt): """Generate an image from a text prompt using the stable-diffusion model""" msg = await ctx.send(f"“{prompt}”\n> Generating...") model = replicate.models.get("stability-ai/stable-diffusion") image = model.predict(prompt=prompt)[0] await msg.edit(content=f"“{prompt}”\n{image}") bot.run(os.environ["DISCORD_TOKEN"])
错误信息
[2022-11-17 20:33:21] [INFO ] discord.client: logging in using static token Traceback (most recent call last): File "bot.py", line 34, in <module> bot.run(os.environ["DISCORD_TOKEN"]) File "C:\Users\Roman\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 828, in run asyncio.run(runner()) File "C:\Users\Roman\AppData\Local\Programs\Python\Python38\lib\asyncio\runners.py", line 43, in run return loop.run_until_complete(main) File "C:\Users\Roman\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 608, in run_until_complete return future.result() File "C:\Users\Roman\AppData\Local\Programs\Python38\lib\site-packages\discord\client.py", line 817, in runner await self.start(token, reconnect=reconnect) File "C:\Users\Roman\AppData\Local\Programs\Python38\lib\site-packages\discord\client.py", line 746, in start await self.connect(reconnect=reconnect) File "C:\Users\Roman\AppData\Local\Programs\Python38\lib\site-packages\discord\client.py", line 672, in connect raise PrivilegedIntentsRequired(exc.shard_id) from None discord.errors.PrivilegedIntentsRequired: Shard ID None is requesting privileged intents that have not been explicitly enabled in the developer portal. It is recommended to go to https://discord.com/developers/applications/ and explicitly enable the privileged intents within your application's page. If this is not possible, then consider disabling the privileged intents instead. Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001CBA8AA73A0> Traceback (most recent call last): File "C:\Users\Roman\AppData\Local\Programs\Python38\lib\asyncio\proactor_events.py", line 116, in __del__ self.close() File "C:\Users\Roman\AppData\Local\Programs\Python38\lib\asyncio\proactor_events.py", line 108, in close self._loop.call_soon(self._call_connection_lost, None) File "C:\Users\Roman\AppData\Local\Programs\Python38\lib\asyncio\base_events.py", line 711, in call_soon self._check_closed() File "C:\Users\Roman\AppData\Local\Programs\Python38\lib\asyncio\base_events.py", line 504, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed
核心错误说明
discord.errors.PrivilegedIntentsRequired:机器人请求的特权意图未在开发者门户中明确启用。
解决步骤
- 进入Discord开发者平台,找到你的机器人应用
- 切换到「Bot」页面,在「特权网关意图」区域,启用「消息内容意图」(代码中开启了
message_content意图,该意图属于特权范畴) - 保存设置后重启机器人
内容的提问来源于stack exchange,提问作者Roman Hoffmann
相关产品推荐
相关产品推荐

