Anurag
Anurag

Reputation: 31

Python discord bot -- AttributeError: 'NoneType' object has no attribute 'strip'

I am developing a Discord Bot using Python. And getting the following error (AttributeError: 'NoneType' object has no attribute 'strip'). Here is my code.

import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('O.......')
GUILD = os.getenv('CodeUP')

client = discord.Client()

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(
        f'{client.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )

client.run(TOKEN)

Upvotes: 1

Views: 649

Answers (2)

Makiyu
Makiyu

Reputation: 421

The thing you're supposed to get as the "token" is the variable name of your bot token stored in the .env file, like this!

Contents of .env file:

BOT_TOKEN=ODMyMTUxNjQ4OTAxMjY3NTA2.YHfnnQ.r_rQ2mmo8HFvaBAl9rry28VM4Nk

Token variable in the python file:

TOKEN = os.getenv('BOT_TOKEN')

Upvotes: 2

mousetail
mousetail

Reputation: 8010

os.getenv gets a enviroment variable with the given name. You are using your token as your name. Replace the code with this:

TOKEN = os.getenv('DISCORD_TOKEN', 'ODMyMTUxNjQ4OTAxMjY3NTA2.YHfnnQ.r_rQ2mmo8HFvaBAl9rry28VM4Nk')

This find an environment variable named DISCORD_TOKEN, and if none exists, use 'ODM...'

I hope this is not your real token you are posting on the internet though, if so make sure to cancel it immediately.

Upvotes: 0

Related Questions