HTTPS error 401 when running discord bot in python

Question:

I get this error whenever i run my bot:

Traceback (most recent call last):
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordhttp.py", line 801, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordhttp.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:PythonProjectsdisco botppap", line 21, in <module>
    client.run(os.getenv("K"))
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordclient.py", line 828, in run
    asyncio.run(runner())
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libasynciorunners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libasynciobase_events.py", line 647, in run_until_complete
    return future.result()
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordclient.py", line 817, in runner
    await self.start(token, reconnect=reconnect)
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordclient.py", line 745, in start
    await self.login(token)
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordclient.py", line 580, in login
    data = await self.http.static_login(token)
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordhttp.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:UsersAdminAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 116, in __del__
    self.close()
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libasynciobase_events.py", line 751, in call_soon
    self._check_closed()
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libasynciobase_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

This is the code:

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"))

I have my bot key stored in an .env file and the key is correct.

I have reset the key multiple times and enabled all intent settings for the bot but nothing has helped. How would i fix this?

Asked By: turtlesXD

||

Answers:

According to this question, you should try:

  1. Resetting your token, it could be invalid
  2. Enabling intents on the developer portal
Answered By: siIverfish