Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord Bot - "Attribute Error: 'NoneType' object has no attribute 'strip.'

I'm a new coder, and I've been following atutorial on how to create a discord bot with the code below having been virtually copied the code straight out from the tutorial, and I've create a .env file to store my AuthToken. Every time I run the code, I get error below aforementioned code. Any tips? Thanks in advance!

Code:

import os 

import discord

from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()

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

Error:

Traceback (most recent call last):   File "/Users/XXXXXXXXXXXX/scratch/discordbot/app.py", line 16, in <module>
    client.run(TOKEN)   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 640, in run
    return future.result()   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 621, in runner
    await self.start(*args, **kwargs)   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 584, in start
    await self.login(*args, bot=bot)   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 442, in login
    await self.http.static_login(token.strip(), bot=bot) AttributeError: 'NoneType' object has no attribute 'strip' 

like image 575
Jake Avatar asked Jul 01 '20 18:07

Jake


2 Answers

I was following the same tutorial and ran into the same error. Issue for me was that I had created the ".env" file incorrectly. In the tutorial it says "Create a file named .env in the same directory as bot.py:" - this was my issue. If you create a new text document, paste in the code, then save it with the name ".env", what you'll actaully be creating is a text file called ".env.txt".

To get around this, go to the directory where you have the python script saved (for me this is C:\Thonny\DiscordBots), then right-click in that folder and select "New > Text Document". Don't change the filename yet, just leave it as "New Text Document". Open this file (should open in Notepad), then paste in the code from the tutorial (also, remember to substitute your bot's actual token in for the placeholder variable called {your-bot-token}). Now, go to "File > Save As" and in the File Name field, type ".env" just like the tutorial says to; BEFORE you hit save, also click the Save as type dropdown (should be right below File Name) and instead of leaving it as the default type (*.txt), change this to "All Files".

If you've done this correctly, you should see your ".env" file in file explorer, and the "Type" column will now show "ENV File" instead of "text document". Try to run the code again.

This is what helped me. My understanding is that "load_dotenv()" is looking for a file of the ENV type, not just any document called ".env" (of any type). As long as this file is placed in the same directory as the script you're running, it should work.

like image 103
KlyKy Avatar answered Sep 18 '22 02:09

KlyKy


This error arises due to the failure of fetching TOKEN value in. env file which can be resolved by-

from dotenv import load_dotenv
load_dotenv('---.env')

It worked for me !!!

like image 20
mscodi Avatar answered Sep 21 '22 02:09

mscodi