Forums

Discord bot : RuntimeError: Event loop is closed

Hello, I have a 5$ account and I want to run my discord bot 24/7. I have the "classic" error

RuntimeError: Event loop is closed

I saw I need to use a uwsgi webserver instead of an eventloop webserver. I don't know what awsgi webserver is (idk what is eventloop webserver either...)

Can someone help me solve my problem ? :D Here is the code

import discord 
from discord.ext import commands

token = 'token'
prefix = '+' 
client = commands.Bot(command_prefix=prefix)
client.remove_command('help')
async def on_message(ctx):

..

SOME STUFF

..

client.run(token)

Thank you in advance :D

[edit by admin: code formatting]

Normally you wouldn't need to run a Discord bot inside a webserver; your bot code can just be run as a normal script. For debugging, you would just run it from a console, and then when it's working properly you would set it up as an always-on task.

So my first question -- how are you running the code right now? Is it from a console? And if so, what is the full error message?

I solved my problem of event loop . Atm, i just run it as a classic file in a console but i will set it up as an always on task ;) I don't know why there were an error. Just execute it a lot of time and it worked xD Thank you for your time :D I hope i will not have problem with always-on task.

Glad to hear it's working now! If it runs happily in a console it should work well in an always-on task, but it would be worth your while to read through the always-on task documentation before setting it up -- there are hints and tips there on how to avoid common problems.

Hello! I have the same problem as you, but I don`t understand, how to solve it. Can you tell me, what means run as a classic file in console? Thank you in advance!

Just start a console and then run the script. For example, if your script is called "mybot.py", is in a directory called "mydirectory", and you want to run it using Python 3.8, you would start a Bash console and enter these commands:

cd mydirectory
python3.8 mybot.py

I should warn you, though, that a Discord bot is unlikely to run from a free account -- the discord API library uses WebSockets but is not proxy-aware, so it won't be able to connect to Discord's servers.

Thank you for your answer! And just like you said my free account can`t connect to anything )