'Im making a discord bot and i got an error (python)

im going off a website (https://realpython.com/how-to-make-a-discord-bot-python/#how-to-make-a-discord-bot-in-python) and im following there steps but i keep getting an error msg

Traceback (most recent call last): File "C:\Users\Bryce.Persello346\Desktop\bot.py", line 15, in client.run(TOKEN) File "C:\Users\Bryce.Persello346\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 718, in run return future.result() File "C:\Users\Bryce.Persello346\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 697, in runner await self.start(*args, **kwargs) File "C:\Users\Bryce.Persello346\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 660, in start await self.login(*args, bot=bot) File "C:\Users\Bryce.Persello346\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 509, in login await self.http.static_login(token.strip(), bot=bot) AttributeError: 'NoneType' object has no attribute 'strip'

my code:


import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('"token here"')

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)



Solution 1:[1]

In the example on real python you may have made the mistake of putting the key in curly braces. so basically just format your env file like this...

DISCORD_TOKEN = token_goes_here

instead of...

DISCORD_TOKEN = {token_goes_here}

Solution 2:[2]

The problem seems to be in the way the token is stored

As the error says , discord.py is unable to use the token because it is not stored correctly

If you are running the code on your own local system , try not using environment variables

import os
import discord
TOKEN = "ABC.123.XYZ"
client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)

If you are running the code on online ide's (replit or others) , then do

import os
import discord
TOKEN = os.environ["token"]
client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)

Hope this helps :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nial Dixon
Solution 2 Global-Occult