'How to quit discord bot without Event loop is closed

I am trying to stop a discord bot python code, The code ends correctly but i have a problem witch is the Event Loop Exception RuntimeError: Event loop is closed

Full Exeption :

Traceback (most recent call last):
  File "C:\Users\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\Okba\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\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 750, in call_soon
    self._check_closed()
  File "C:\Users\Okba\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
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000014B7B25E4D0>
Traceback (most recent call last):
  File "C:\Users\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Users\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Users\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 750, in call_soon
  File "C:\Users\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
RuntimeError: Event loop is closed
PS D:\Okba\Programming\tokenchecker> & C:/Users/Okba/AppData/Local/Programs/Python/Python310/python.exe d:/Okba/Programming/tokenchecker/temp/hOTM5MjQ3NzkyNjQyMjExODkw.Yf2FOg.tkjSmTj48g8FJTWPYvkdn6dKMu4.py
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000002462CB68310>
Traceback (most recent call last):
  File "C:\Users\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\Okba\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\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 750, in call_soon
    self._check_closed()
  File "C:\Users\Okba\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
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000002462CB68310>
Traceback (most recent call last):
  File "C:\Users\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Users\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Users\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 750, in call_soon
  File "C:\Users\Okba\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
RuntimeError: Event loop is closed

The code :

    import discord
    client = discord.Client()
    @client.event
    async def on_ready():
        try :await client.close()
        except : None


    client.run("token here" , bot=False) #Using None-Bot account

I tried :

  • handling on client.close() .
  • stopping the event loop before quit but i failed .

Note : The token is correct and the bot runs correctly and raises the exception after the await bot.close() directly .



Solution 1:[1]

It seems to be the Windows EventLoopPolicy that raise this error.

You can do the following:

import asyncio
import discord
import platform

client = discord.Client()

@client.event
async def on_ready():
    # Set the current process-wide policy
    if platform.system().lower() == 'windows':
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    
    try: await client.close()
    except: None


client.run("token here")

For more information you should have a look about policies on the asyncio/Policies documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul