"RuntimeError: Event loop is closed" with Discord.py API

Question:

I decided to pick up coding in python again as I wasnt to code a Discordbot in python which lets me see the Value of a certain Stock by sending a text message. I copied this codeblock from a website

import discord
import os

client = discord.Client()

@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("my_token")

(I replaced the placeholder with my real bot token) and when I try to execute this code I get the error message stated above.

Edit: Here the whole error traceback:

File "C:UserschrisAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscordhttp.py", line 300, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "C:UserschrisAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscordhttp.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:UserschrisOneDrivePythonBotCodeFinanzbotbot.py", line 18, in <module>
    client.run("KiX-TtMr8ToGsitsv6GvD8bfc9eA7_G3")
  File "C:UserschrisAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscordclient.py", line 723, in run
    return future.result()
  File "C:UserschrisAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscordclient.py", line 702, in runner
    await self.start(*args, **kwargs)
  File "C:UserschrisAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscordclient.py", line 665, in start
    await self.login(*args, bot=bot)
  File "C:UserschrisAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscordclient.py", line 511, in login
    await self.http.static_login(token.strip(), bot=bot)
  File "C:UserschrisAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscordhttp.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 0x00000203C93F80D0>
Traceback (most recent call last):
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0libasyncioproactor_events.py", line 116, in __del__
    self.close()
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0libasyncioproactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0libasynciobase_events.py", line 746, in call_soon
    self._check_closed()
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0libasynciobase_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Asked By: PokemasterLink

||

Answers:

You have inserted an improper token.
Take a look at the error message:

    raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.

Make sure to insert the right one. You can find it here:

Token of your bot

Answered By: Dominik
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.