Discord Bot-“属性错误:'NoneType'对象没有属性'strip'。 [英] Discord Bot - "Attribute Error: 'NoneType' object has no attribute 'strip.'

查看:140
本文介绍了Discord Bot-“属性错误:'NoneType'对象没有属性'strip'。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新编码员,并且一直在关注教程,其中介绍了如何使用下面的代码创建虚拟的bot机器人,并已从教程中直接复制了这些代码,而我创建一个.env文件来存储我的AuthToken。每次我运行代码,都会在上述代码下面出现错误。有小费吗?

I'm a new coder, and I've been following atutorial on how to create a discord bot with the code below having been virtually copied the code straight out from the tutorial, and I've create a .env file to store my AuthToken. Every time I run the code, I get error below aforementioned code. Any tips? Thanks in advance!

代码:

import os 

import discord

from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')
client.run(TOKEN)

错误:

Traceback (most recent call last):   File "/Users/XXXXXXXXXXXX/scratch/discordbot/app.py", line 16, in <module>
    client.run(TOKEN)   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 640, in run
    return future.result()   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 621, in runner
    await self.start(*args, **kwargs)   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 584, in start
    await self.login(*args, bot=bot)   File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 442, in login
    await self.http.static_login(token.strip(), bot=bot) AttributeError: 'NoneType' object has no attribute 'strip' 


推荐答案

错误是由于 TOKEN 设置为 None ,这是 os.getenv('DISCORD_TOKEN') retu的内容如果变量不存在或存在且设置为 None

The error is due to TOKEN being set to None, which is what os.getenv('DISCORD_TOKEN') returns if the variable doesn't exist or it exists and is set to None.

请确保您的.env文件位于同一目录,例如:

Make sure your .env file is in the same directory, for example:

.
├── .env
└── bot.py

令牌是环境变量,而不是python变量。环境变量的分配遵循您所使用的Shell的语法。这意味着 = 符号周围没有空格。

The token is an environment variable, not a python variable. Assignment of environment variables follow the syntax of the shell you are using. This means no spaces around the = sign.

请注意使用 dotenv 可以将变量导出到您的shell中(请参见自述文件此处):

Note the order of operations when using dotenv to export variables to your shell (see readme here):


Python-dotenv可以使用POSIX变量
扩展来插值变量。

Python-dotenv can interpolate variables using POSIX variable expansion.

变量的值是第一个以下列表中的
中定义的值:

The value of a variable is the first of the values defined in the following list:


  • 该变量在环境中的值。


  • 默认值(如果提供)。

  • 空字符串。

  • Value of that variable in the environment.
  • Value of that variable in the .env file.
  • Default value, if provided.
  • Empty string.

请确保
变量被{}像$ {HOME}包围,因为裸变量(例如
如$ HOME)不会展开。

Ensure that variables are surrounded with {} like ${HOME} as bare variables such as $HOME are not expanded.

给出的示例是:

CONFIG_PATH=${HOME}/.config/foo
DOMAIN=example.org
EMAIL=admin@${DOMAIN}
DEBUG=${DEBUG:-false

由于这个原因,您可能需要在继续操作之前清除相关的Shell变量,因为 dotenv 要做的第一件事是使用已经定义的变量,该变量可能在初始设置中设置为空字符串(例如取消DISCORD_TOKEN ,或使用 source〜/ .bashrc 或类似的东西重启外壳。

For this reason, you may need to clear your relevant shell variables before proceeding, as the first thing dotenv will try is to use the already defined variable, which was probably set to an empty string on your initial setup (e.g. unset DISCORD_TOKEN, or restarting your shell with something like source ~/.bashrc or similar).

出于调试目的,我建议使用 print(os.getenv('DISCORD_TOKEN'))来确切了解此变量的设置。您还可以尝试在环境设置过程中查看 load_dotenv(verbose = True)的输出。

For debugging purposes, I would recommend print(os.getenv('DISCORD_TOKEN')) to see exactly what this variable is set to. You might also try seeing the output of load_dotenv(verbose=True) during the environment setup.

这篇关于Discord Bot-“属性错误:'NoneType'对象没有属性'strip'。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆