开始学习用 Python 编写一个简单的 Discord 机器人。我的机器人令牌在 .env 文件中,但出现错误 discord.errors.LoginFailure: Improper token has been passed.
将令牌放入 client.run(TOKEN_HERE)
对我来说并不像对某些人那样有效。
我正在使用 Discord Developer Portal 上的 Bot 页面中的令牌,而不是 General Information 页面中的 Client Secret。
我正在使用 Visual Studio 代码。
机器人.py:
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)
.env:
DISCORD_TOKEN=secret_token_here
完整的错误输出:
Eds-MacBook-Pro:~ ed$ python3 /users/ed/documents/supremesauce/supremesauce.py
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 261, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 225, in request
raise HTTPException(r, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/users/ed/documents/supremesauce/supremesauce.py", line 15, in <module>
client.run(TOKEN)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 640, in run
return future.result()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 621, in runner
await self.start(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 584, in start
await self.login(*args, bot=bot)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 442, in login
await self.http.static_login(token.strip(), bot=bot)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 265, in static_login
raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
Eds-MacBook-Pro:~ ed$
谢谢 :)