繁体   English   中英

我不断收到错误:AttributeError: 'NoneType' object has no attribute 'strip'

[英]I keep getting the error: AttributeError: 'NoneType' object has no attribute 'strip'

我在这里使用 python。

当我为正在使用 repli 制作的 discord 机器人运行代码时,出现此错误: AttributeError: 'NoneType' object has no attribute 'strip'

我正在使用本教程制作 discord 机器人: https://www.freecodecamp.org/news/create-a-discord-bot-with-python/

代码:

import discord
import os

client = discord.Client()

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

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

    if message.content.startswith('$IHaveNoBobux'):
        await message.channel.send('haha sucks to be you B)')
   
    if message.content.startswith('$GiveMeFreeBobuxNow'):
        await message.channel.send('no stupid noob B)')

client.run(os.getenv('TOKEN'))

有人可以帮我解决这个错误吗? 我的大脑很小,几乎没有 python 所以有些事情可能需要大量解释。 :/

这是我运行代码时的完整控制台:

回溯(最后一次调用):文件“main.py”,第 21 行,在 client.run(os.getenv('TOKEN')) 文件“/home/runner/go-away-this-isnt-for-you /venv/lib/python3.8/site-packages/discord/client.py”,第 723 行,在运行中返回 future.result() 文件“/home/runner/go-away-this-isnt-for-you/ venv/lib/python3.8/site-packages/discord/client.py”,第 702 行,在运行器中等待 self.start(*args, **kwargs) 文件“/home/runner/go-away-this-isnt -for-you/venv/lib/python3.8/site-packages/discord/client.py”,第 665 行,在开始等待 self.login(*args, bot=bot) 文件“/home/runner/go- away-this-isnt-for-you/venv/lib/python3.8/site-packages/discord/client.py",第 511 行,登录等待 self.http.static_login(token.strip(), bot=bot ) AttributeError: 'NoneType' object 没有属性 'strip'

我还有一个名为“env”的环境,其中包含以下内容:

TOKEN=[the discord bots token is would be right here but im not going to show it because its the password]

请按照上述步骤获取令牌,它是空的。

当您在 Discord 上创建机器人用户时,您复制了一个令牌。 现在我们将创建一个 .env 文件来存储令牌。 如果您在本地运行代码,则不需要 .env 文件。 只需将 os.getenv('TOKEN') 替换为令牌。

该文件应该被称为.env而不仅仅是env 假设它已经命名并且它仍然对您不起作用,如果您需要将其上传到远程存储库,您可以使用 json 文件(不要忘记在.gitignore 中添加 json 文件)。

import json
import discord 

client = discord.Client()

env_json = json.loads("env.json")
key = env_json['TOKEN']

@client.event
async def on_ready():
    print(f'We have logged in as {client.username}')

client.run(key)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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