Discord机器人基本示例因PrivilegedIntentsRequired(exc.shard_id)错误失败(Python)

0 投票
1 回答
26 浏览
提问于 2025-04-14 15:59

我知道我这边肯定哪里搞错了,因为我复制了Discord给我们的示例代码(在Discord的快速入门网站上找到的第一个代码块)。但是我就是找不到问题出在哪里。

我运行的代码是:

(是的,我知道导入不应该直接运行代码……我只是想测试最基本的代码,但就是无法让它工作。)
import discord
from config import __BOTTOKEN__ as __TOKEN__

intents = discord.Intents.default()
intents.message_content = True # this is where the error happens even when the bot has admin privs
                               # if I remove this line, I won't be able to read messages from other users
                               # in more complex versions of this code

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 logging in using static token
Traceback (most recent call last):
  File "/home/runner/Discord-bot/main.py", line 9, in <module>
    main()
  File "/home/runner/Discord-bot/main.py", line 4, in main
    import example
  File "/home/runner/Discord-bot/example.py", line 21, in <module>
    client.run(__TOKEN__)
  File "/home/runner/Discord-bot/.pythonlibs/lib/python3.10/site-packages/discord/client.py", line 860, in run
    asyncio.run(runner())
  File "/nix/store/8w6mm5q1n7i7cs1933im5vkbgvjlglfn-python3-3.10.13/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/nix/store/8w6mm5q1n7i7cs1933im5vkbgvjlglfn-python3-3.10.13/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/runner/Discord-bot/.pythonlibs/lib/python3.10/site-packages/discord/client.py", line 849, in runner
    await self.start(token, reconnect=reconnect)
  File "/home/runner/Discord-bot/.pythonlibs/lib/python3.10/site-packages/discord/client.py", line 778, in start
    await self.connect(reconnect=reconnect)
  File "/home/runner/Discord-bot/.pythonlibs/lib/python3.10/site-packages/discord/client.py", line 704, 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.

一开始我想,也许我这个机器人的权限在Discord的开发者门户里需要更高一些,所以我把它设置成了管理员,这样就能获得所有权限……但我还是收到了同样的消息。

如果有任何见解或建议,我会非常感激!

一开始我想,也许我这个机器人的权限在Discord的开发者门户里需要更高一些,所以我把它设置成了管理员,这样就能获得所有权限……但我还是收到了同样的消息。

  • 是的,我在更改权限后尝试重置所有链接和令牌,以防需要新的令牌来获得权限。

1 个回答

1

抱歉大家……原来我真的是有点“特别”(而且不是那种好的特别)

我忘了在开发者门户上开启读取消息的功能(我发誓我之前已经做过了……但显然直到现在都没有)——你可以在“机器人”标签里找到这个设置。(是的,现在我明白上面那个错误信息在说什么了……)discord开发者门户的特权网关意图截图

谢谢大家的耐心‍♂️

撰写回答