0
votes

I'm trying to make a discord bot using python and when I run basic code it returns "AttributeError: 'NoneType' object has no attribute 'strip'" I'm following this tutorial. I have 2 files - bot.py and bot.env(to store the token) Here are the 2 files: bot.py:

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)

and bot.env:

DISCORD_TOKEN={token}

It should display "{bot name} has connected to Discord!" I googled the error but I couldn't find a solution.

2
I recommend storing your token in your code (fewer problems for example if you would like to host it somewhere). Also never show your discord token to anyone - RiveN
If you would like to store the token in your code just write TOKEN = "your token" (at the beginning of the script). Then you can delete: 1. import os 2. from dotenv import load_dotenv 3. load_dotenv() 4. TOKEN = os.getenv('DISCORD_TOKEN') or try looking for the solution in the question @LeoGaunt linked - RiveN
Thank you for the help! I don't know how I missed that same question but storing the token in the code helped. - Noob
If you solved your answer, it'll be good if you share the solution to other people. - Nurqm

2 Answers

0
votes

You just have to add a new line and load the env as what @RiveN has mentioned

import discord
import os
from dotenv import load_dotenv
load_dotenv()
0
votes

Many problems occur while storing token in enviroment variables, So I'd recommend storing it inside a file and reading it or making a python file with function that returns token and then importing it.
When you host your bot on an external hosting server, you can't access these variables. So, You will at some point, need to change this method of getting token.