所以我有这个代码:
# bot.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)
“ DISCORD_TOKEN”存储在同一目录中的另一个文件“ .env”中:
#. env file
DISCORD_TOKEN={SSS}
现在,每当我尝试运行此代码时,我都会遇到这些错误:
Traceback (most recent call last):
File "C:\Users\HP\Python\lib\site-packages\discord\http.py", line 258, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "C:\Users\HP\Python\lib\site-packages\discord\http.py", line 222, 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 "bot.py", line 18, in <module>
client.run(token)
File "C:\Users\HP\Python\lib\site-packages\discord\client.py", line 598, in run
return future.result()
File "C:\Users\HP\Python\lib\site-packages\discord\client.py", line 579, in runner
await self.start(*args, **kwargs)
File "C:\Users\HP\Python\lib\site-packages\discord\client.py", line 542, in start
await self.login(*args, bot=bot)
File "C:\Users\HP\Python\lib\site-packages\discord\client.py", line 400, in login
await self.http.static_login(token, bot=bot)
File "C:\Users\HP\Python\lib\site-packages\discord\http.py", line 262, in static_login
raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
但是当我简单地使用
token = 'DSDS'
代替.getenv,代码可以正常工作,并且我的机器人已在线。
对此有任何解决办法吗?
答案 0 :(得分:0)
发现我错了。我应该在dotenv文件中的令牌外部使用”而不是{}。