Errors encountered with Python Discord bot

I’m experiencing the following error while trying to execute my Discord bot:

TypeError: expected token to be a str, received NoneType instead

Here’s the complete traceback of the error:

Traceback (most recent call last):
  File "d:\Python\Projects\discord_bot\main", line 20, in <module>
    client.run(os.getenv('BOT_TOKEN'))
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 828, in run
    asyncio.run(executor())
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 817, in executor
    await self.start(token, reconnect=reconnect)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 745, in start
    await self.login(token)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 577, in login
    raise TypeError(f'expected token to be a str, received {token.__class__.__name__} instead')

Here’s my code snippet:

import discord
import os

options = discord.Intents.default()
options.typing = False
options.presences = False
bot = discord.Client(intents=options)

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

@bot.event
async def on_message(msg):
    if msg.author == bot.user:
        return
    if msg.content.startswith('$greet'):
        await msg.channel.send('Greetings!')

bot.run(os.getenv('BOT_TOKEN'))

I don’t have any other files associated with this code; it’s the only one in the project folder. How can I resolve this error? Thank you! I’ve checked various solutions but haven’t found anything that fixes the issue.

This error is often due to the BOT_TOKEN environment variable not being set correctly. Ensure that you have a .env file in the same directory as your script or have set the BOT_TOKEN environment variable in your system. The .env file should look like this:

BOT_TOKEN=your_discord_bot_token_here

Additionally, make sure you’re using a package like python-dotenv to load your environment variables properly. If you are not using it, you can install it with pip install python-dotenv and add from dotenv import load_dotenv; load_dotenv() at the top of your script to ensure the variables are loaded.

Sometimes the IDE you’re using might not detect environment vars correctly. Yo can try hard-coding the token just to check if its a setup issue. Also make sure ur python version and discord.py library are up to date. Random glitches can sometimes be resolved with a simple update!

I’ve run into similar issues in the past, and I’d suggest checking if your script is in the right folder when executed. It might sound obvious, but double-checking the directory from where you’re running the script can make a difference, especially if you’re using any automated processes or scripts to start your bot. Additionally, consider echoing os.getenv('BOT_TOKEN') before bot.run() to confirm it’s fetching a token at all. If nothing prints, that’s a sign your environment variable isn’t set, and you’ll want to revisit that setup.