Issue
This Content is from Stack Overflow. Question asked by Dominic Miller
I’m wanting to make a simple discord bot. What it does isn’t too important other than the fact that I want it to send messages at certain times. The code below is very basic and is not the finished product.
# bot.py
import os
import discord
from dotenv import load_dotenv
intents = discord.Intents().all()
intents.messages = True
load_dotenv()
TOKEN = os.getenv("FAKETOKENBLAHBLAHBLAH")
client = discord.Client(command_prefix=',', intents=discord.Intents().all())
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
Am I doing something wrong? If so, please tell me. I’ve been stumped for hours now and all I have for reference is this error:
TypeError: expected token to be a str, received <class 'NoneType'> instead
I’ve read so many articles about it, but none of them seem to work! Please tell me why!
Solution
While having the full traceback would be useful in general, the error
TypeError: expected token to be a str, received <class ‘NoneType’> instead
hints that the token
passed in is None
, not a string.
Your code
load_dotenv()
TOKEN = os.getenv("FAKETOKENBLAHBLAHBLAH")
tries to load a .env file (to load variables from it to the process environment) and then accesses the environment variable FAKETOKENBLAHBLAHBLAH
.
If you don’t have FAKETOKENBLAHBLAHBLAH=...
in your .env
file or the environment you’re running your program in, then os.getenv()
will return None
, and TOKEN
will be None.
It sounds like you might want os.getenv('DISCORD_TOKEN')
or similar, so you’d use DISCORD_TOKEN
as an environment variable name.
Answered by AKX
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 4.0.