Discord.py – Bot token failing

Question:

I’m currently trying to create a bot for Discord.
For some reason the token gives me this error:

2022-11-02 04:42:10 INFO     discord.client logging in using static token
Traceback (most recent call last):
  File "main.py", line 26, in <module>
    client.run(os.getenv("bot_key"))
  File "/home/runner/discord-moni-bot-thing/venv/lib/python3.8/site-packages/discord/client.py", line 828, in run
    asyncio.run(runner())
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/home/runner/discord-moni-bot-thing/venv/lib/python3.8/site-packages/discord/client.py", line 817, in runner
    await self.start(token, reconnect=reconnect)
  File "/home/runner/discord-moni-bot-thing/venv/lib/python3.8/site-packages/discord/client.py", line 746, in start
    await self.connect(reconnect=reconnect)
  File "/home/runner/discord-moni-bot-thing/venv/lib/python3.8/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.

I’ve checked if the token is the same as on the developer portal, and it is.

I’ve changed the token multiple times, as well as replacing os.getenv("bot_key") with the token directly.
I have another bot that uses the same method, and that works fine. This is the full code for anybody who wants it: (The on_message(message) function is copied and pasted from my other bot)


import discord
import os

intents = discord.Intents.default()
intents.members = True

client = discord.Client(intents=intents)

@client.event
async def on_ready():
print("Logged in as {0.user}".format(client))

@client.event
async def on_message(message):
if message.author == client.user:
return

    if message.content.startswith("$hello"):
        await message.channel.send("Hello!")
    elif message.content.startswith("$dmuser"):
        if message.content.find(" "):
            if len(message.content.split(" ")) == 2:
                await client.get_user(int(message.content.split(" ")[1])).send("You Were Sent A DM By " + str(message.author))

try:
client.run(os.getenv("bot_key")) # <--- THIS HERE
except discord.HTTPException as e:
if e.status == 429:
print("The Discord servers denied the connection for making too many requests")
print("Get help from https://stackoverflow.com/questions/66724687/in-discord-py-how-to-solve-the-error-for-toomanyrequests")
else:
raise e
Asked By: Xamrey

||

Answers:

It is recommended to go to https://discord.com/developers/applications/ and explicitly enable the privileged intents within your application’s page.
*Part of the error that you shown from the code.

The problem is you haven’t enabled some permissions in the developer portal, not because of the TOKEN.

First you go to the dev portal and click BOT,

Discord Developer Portal Menu

since you specified intents.members = True,
you need the server members intent enabled in the portal,

Server members intent

So just scroll down and turn this on and you code should work fine.

Answered By: Annoymous
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.