AttributeError: 'NoneType' 对象没有属性 'strip' 如何解决?

时间:2020-12-30 10:30:52

标签: python api discord bots strip

你好,我不知道为什么我会收到这个错误:/

完整代码:

import discord
import os

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

client.run(os.getenv('discord api'))



完整回溯:

Traceback (most recent call last):
  File "C:\Users\thoma\Documents\discordbot\bot.py", line 18, in <module>
    client.run(os.getenv('NzkzNzczNDUwODQ5NDE5Mjk2.X-xJIg.MoKUA7czhrWtFvu4FEc6M3JAQQM'))
  File "C:\Users\thoma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 708, in run
    return future.result()
  File "C:\Users\thoma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 687, in runner
    await self.start(*args, **kwargs)
  File "C:\Users\thoma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 650, in start
    await self.login(*args, bot=bot)
  File "C:\Users\thoma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 499, in login
    await self.http.static_login(token.strip(), bot=bot)

AttributeError: 'NoneType' 对象没有属性 'strip'

1 个答案:

答案 0 :(得分:0)

我是这样解决的

  1. 安装“pip install python-dotenv”
  2. 将其放入您的代码中:
from dotenv import load_dotenv
load_dotenv(os.path.join(os.getcwd(), '.env'))
SECRET_KEY = os.getenv("TOKEN")
client.run(SECRET_KEY)