問題描述
我是一名新程序員,我一直在關注一個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’.的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!