简体   繁体   中英

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)

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')

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM