【发布时间】:2020-06-07 04:22:33
【问题描述】:
我正在学习使用 python 编写一个不和谐的机器人。 最近创建了一个机器人并成功将其添加到我的不和谐服务器。 我使用 pip 安装了 discord.py 和 python-dotenv。 得到错误:
Traceback (most recent call last):
File "C:\Users\jjjf9\AppData\Roaming\Python\Python38\site-packages\discord\http.py", line 261, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "C:\Users\jjjf9\AppData\Roaming\Python\Python38\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 "e:/discord/bot.py", line 15, in <module>
client.run(token)
File "C:\Users\jjjf9\AppData\Roaming\Python\Python38\site-packages\discord\client.py", line 640, in run
return future.result()
File "C:\Users\jjjf9\AppData\Roaming\Python\Python38\site-packages\discord\client.py", line 621, in runner
await self.start(*args, **kwargs)
File "C:\Users\jjjf9\AppData\Roaming\Python\Python38\site-packages\discord\client.py", line 584, in start
await self.login(*args, bot=bot)
File "C:\Users\jjjf9\AppData\Roaming\Python\Python38\site-packages\discord\client.py", line 442, in login
await self.http.static_login(token.strip(), bot=bot)
File "C:\Users\jjjf9\AppData\Roaming\Python\Python38\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.
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001C7B373F700>
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\asyncio\proactor_events.py", line 116, in __del__
self.close()
File "C:\Program Files\Python38\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Program Files\Python38\lib\asyncio\base_events.py", line 715, in call_soon
self._check_closed()
File "C:\Program Files\Python38\lib\asyncio\base_events.py", line 508, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
代码: 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)
.env
DISCORD_TOKEN={XXXXXXXXXXXX}
【问题讨论】:
-
问题可能是您使用 DISCORD_TOKEN={XXXXX} 而不是 DISCORD_TOKEN=XXXXX
-
尝试删除括号得到了同样的错误。还尝试添加 'token' 而不是括号并得到 . .第 228 行,在请求中引发 HTTPException(r, data) discord.errors.HTTPException: 429 TOO MANY REQUESTS (error code: 0): You are rate limited.
-
请求过多错误可能表明您使用了错误的令牌。经过几次尝试后,discord 将阻止请求。您是否尝试过执行以下操作: print(token) 你看到你的令牌了吗?
标签: python python-3.x discord discord.py