Discord.py: How to fix "event loop is closed"
Asked Answered
T

6

11

I am new to programming. I am trying to have my discord bot open up command prompt to confirm it can run, but I am getting this error:

  File "C:\Users\---\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\---\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\---\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 719, in call_soon
    self._check_closed()
  File "C:\Users\---\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 508, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
[Finished in 0.871s]

Code:

import discord
from discord.ext import commands

client=commands.Bot(command_prefix = '.')

@client.event
async def on_ready():
    print('ok')

client.run(token)

What causes this error, and how can I fix this?

Edit: after some testing, I believe "client.run(token)" is what is causing "event loop is closed", not sure why.

Tonsil answered 12/6, 2020 at 8:15 Comment(3)
Are you sure that authorization is successful?Dynel
I believe so, I didn't see anything about it being unauthorizedTonsil
Are you running python 3.8+ on windows?Interphone
C
3

try enabling intents on the developer portal it worked for me when i had the same problem

Culver answered 10/8, 2021 at 16:40 Comment(0)
O
2

I guess your bot has no function, so it just starts and then shuts down again, but I'm not sure. You would have to test this out by for example adding async def on_message(message).

Normally the Event loop is closed "Error" is caused by client.close() or client.logout()

Oneeyed answered 5/3, 2021 at 7:55 Comment(0)
V
2

Open Discord Developers Portal https://discord.com/developers/applications

Select your application

Open 'Bot' section in the settings

Enable SERVER MEMBERS INTENT.

now save changes and try :)

Vicinity answered 2/3, 2022 at 18:25 Comment(0)
A
1

This is because you didn't set the "token" variable, in your last line you use it but it isn't defined.

Or maybe did you set it but didn't write it here?

Averse answered 30/7, 2021 at 7:6 Comment(0)
W
0

This is caused by a wrong or invalid token. Try to reset your token in the developer portal and start the bot with the new one.

Winterkill answered 20/4, 2022 at 9:44 Comment(0)
C
0
  • Enable Privileged Intents

  • Add these lines :

    intents = discord.Intents.default()
    intents.typing = False
    intents.presences = False

Casimir answered 9/9, 2023 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.