【问题标题】:HTTPS error 401 when running discord bot in python在 python 中运行 discord bot 时出现 HTTPS 错误 401
【发布时间】:2022-11-27 08:51:53
【问题描述】:

每当我运行我的机器人时,我都会收到此错误:

Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 801, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 744, in request
    raise HTTPException(response, 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 "d:\Python\Projects\disco bot\ppap", line 21, in <module>
    client.run(os.getenv("K"))
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 828, in run
    asyncio.run(runner())
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 647, in run_until_complete
    return future.result()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 817, in runner
    await self.start(token, reconnect=reconnect)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 745, in start
    await self.login(token)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 580, in login
    data = await self.http.static_login(token)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 805, 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 0x000001C2A7BA30D0>
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 751, in call_soon
    self._check_closed()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

这是代码:

import discord
import os
from dotenv import load_dotenv
load_dotenv()
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')
client.run(os.getenv("K"))

我将我的机器人密钥存储在一个 .env 文件中并且该密钥是正确的。

我已经多次重置密钥并为机器人启用了所有意图设置,但没有任何帮助。我将如何解决这个问题?

【问题讨论】:

    标签: python discord discord.py bots python-3.9


    【解决方案1】:

    根据this question,你应该尝试:

    1. 正在重置您的令牌,它可能无效
    2. 在开发人员门户上启用意图

    【讨论】:

      猜你喜欢
      • 2020-09-27
      • 2019-04-27
      • 1970-01-01
      • 2018-05-14
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多