0

我在这里使用python。

当我为我正在使用 replit 制作的不和谐机器人运行代码时,我收到此错误:AttributeError: 'NoneType' object has no attribute 'strip'

我正在使用本教程制作一个不和谐的机器人: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'))

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

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

回溯(最后一次调用):文件“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' 对象没有属性 'strip'

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

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

2 回答 2

0

该文件应该被称为.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)
于 2022-02-05T11:03:28.533 回答
0

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

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

于 2022-02-05T03:47:14.993 回答