当前位置:优学网  >  在线题库

Discord Bot无法在VSCode上运行

发表时间:2022-07-29 00:47:55 阅读:134

机器人.py

#机器人.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 = {token}

遵循来自的教程https://realpython.com/how-to-make-a-discord-bot-python/,尝试启动机器人,但它无法运行,出现错误,似乎与无法登录(事件循环已关闭)有关.也不确定我应该如何将我的令牌包含在{}中或什么都不包含.我知道用我的实际令牌替换token关键字.

Traceback (most recent call last):
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 300, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 254, 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 "c:\Users\joels\Desktop\DiscordBot\机器人.py", line 16, in <module>
    client.run(TOKEN)
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 723, in run
    return future.result()
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 702, in runner
    await self.start(*args, **kwargs)
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 665, in start
    await self.login(*args, bot=bot)
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 511, in login
    await self.http.static_login(token.strip(), bot=bot)
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 304, 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 0x000001BA31A02050>
Traceback (most recent call last):
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 750, in call_soon
    self._check_closed()
  File "C:\Users\joels\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
🎖️ 优质答案
  • 您不必将您的令牌包含在{}中,只需将其像DISCORD_token=yourtoken一样写入您的.env.错误表明您无法使用此令牌登录,原因是其中包含这些{}.

  • 相关问题