问题描述
我是一名新程序员,我一直在关注一个tutorial 关于如何使用下面的代码创建一个不和谐的机器人,这些代码实际上是直接从教程中复制出来的,我已经创建了一个 .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'
推荐答案
我按照同样的教程,遇到了同样的错误.对我来说,问题是我创建了.env".文件不正确.在教程中它说在与 bot.py 相同的目录中创建一个名为 .env 的文件:"- 这是我的问题.如果您创建一个新的文本文档,粘贴代码,然后使用名称.env"保存它,您将实际创建一个名为.env.txt"的文本文件;.
I was following the same tutorial and ran into the same error. Issue for me was that I had created the ".env" file incorrectly. In the tutorial it says "Create a file named .env in the same directory as bot.py:" - this was my issue. If you create a new text document, paste in the code, then save it with the name ".env", what you'll actaully be creating is a text file called ".env.txt".
要解决这个问题,请转到保存 Python 脚本的目录(对我来说,这是 C:ThonnyDiscordBots),然后右键单击该文件夹并选择新建".文本文档".不要更改文件名,只需将其保留为新建文本文档"即可.打开此文件(应在记事本中打开),然后粘贴教程中的代码(另外,请记住将您的机器人的实际令牌替换为名为 {your-bot-token} 的占位符变量).现在,转到文件"另存为"并在文件名字段中,输入.env";就像教程说的那样;在您点击保存之前,还请单击 另存为类型 下拉菜单(应该在 文件名 的正下方),而不是将其保留为默认类型(*.txt),将其更改为所有文件".
To get around this, go to the directory where you have the python script saved (for me this is C:ThonnyDiscordBots), then right-click in that folder and select "New > Text Document". Don't change the filename yet, just leave it as "New Text Document". Open this file (should open in Notepad), then paste in the code from the tutorial (also, remember to substitute your bot's actual token in for the placeholder variable called {your-bot-token}). Now, go to "File > Save As" and in the File Name field, type ".env" just like the tutorial says to; BEFORE you hit save, also click the Save as type dropdown (should be right below File Name) and instead of leaving it as the default type (*.txt), change this to "All Files".
如果您已正确完成此操作,您应该会看到您的.env".文件资源管理器中的文件,以及类型"列现在将显示ENV 文件";而不是文本文档".尝试再次运行代码.
If you've done this correctly, you should see your ".env" file in file explorer, and the "Type" column will now show "ENV File" instead of "text document". Try to run the code again.
这对我有帮助.我的理解是load_dotenv()";正在寻找 ENV type 的文件,而不仅仅是 any 名为.env"的文件;(任何类型).只要此文件与您正在运行的脚本位于同一目录中,它就应该可以工作.
This is what helped me. My understanding is that "load_dotenv()" is looking for a file of the ENV type, not just any document called ".env" (of any type). As long as this file is placed in the same directory as the script you're running, it should work.
这篇关于Discord Bot - “属性错误:‘NoneType’对象没有属性‘strip’.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!