Discord.py: Bot automaticly stops after a few minutes.

Created on 24 Jul 2018  路  23Comments  路  Source: Rapptz/discord.py

I have an (almost) perfectly working bot, the only problem was that it closes after a few minutes. I tried to fix it by replacing client.run(TOKEN) with while True: client.run(TOKEN) but that didn't work because now I keep getting the error:
Traceback (most recent call last):
File "C:\Users\kiron\Desktop\AI\Discord.py", line 11, in <module>
api.discord.run(TOKEN)
File "C:\Users\kiron\Desktop\AI\api\discord.py", line 57, in run
client.run(TOKEN)
File "C:\Users\kiron\AppData\Local\Programs\Python\Python35-32\lib\site-packages\discord\client.py", line 519, in run
self.loop.run_until_complete(self.start(*args, **kwargs))
File "C:\Users\kiron\AppData\Local\Programs\Python\Python35-32\lib\asyncio\base_events.py", line 364, in run_until_complete
self._check_closed()
File "C:\Users\kiron\AppData\Local\Programs\Python\Python35-32\lib\asyncio\base_events.py", line 334, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

Somebody know how to fix this?

Most helpful comment

ravi2005

All 23 comments

A particular Client instance can not be run more than once.

This is because of a few particular details about how the Client operates. For reference, a typical Client lifecycle goes something like this:

  • Upon instantiation, the Client acquires an event loop from either the loop kwarg or the default event loop for the current thread. A connection state and HTTP client is created as well as flags indicating what part in the lifecycle the Client is in.
  • Upon being run, the Client begins running the event loop, during which it logins into Discord and begins listening to the websocket (which it continues doing indefinitely until it is interrupted).
  • Upon the client closing, flags are set to indicate the client is no longer running, pending tasks on the event loop are cancelled and any exceptions raised, and finally the event loop is closed.

The error you're experiencing is directly related to the last point - the event loop the Client has established itself upon is no longer usable, all of its tasks have been cancelled and it has been closed.

Even if we were able to replace the event loop, many integral parts of the client such as its ConnectionState and HTTPClient hold hard references back to the old event loop and it still wouldn't function correctly, and even if we could replace all references, the client has established through its lifecycle state that it is closed and no longer usable.

This leaves one sensible option - which is to recreate our Client instance entirely on a new event loop, or in other words to do something like this:

while True:
    client = discord.Client()
    ...
    client.run(...)

    asyncio.set_event_loop(asyncio.new_event_loop())  # create a new default event loop so it is fresh for next time
    # a delay or other condition should probably be put here to avoid login spam

This rectifies all of our issues by starting with a clean slate on a new event loop every time, so we don't have stale references or confusing states.

It should also be mentioned that if the purpose of this while loop is merely to restart the bot application should it fail or exit such that it runs constantly, that there are there are various service/process managers available like systemd services or openrc that can help keep the script running without any extra code bloat.

client.run() will close the loop. Doing a simple while: True like this is almost certainly the wrong way to go about this, since you may end up spamming discord with login requests etc.

Are you on 0.16.x or 1.0.0a? The latter should handle autoreconnect due to network issues.

Does any error show up when your not disconnects? Try enabling INFO or DEBUG logging for discord module and looking through that.

Joining discord.gg/discord-api or the discord.py server and asking /providing that info may be helpful

I am on 0.16.12

and I have my client.run() in an function... I could give you the source code:
Discord bot

First of all, you've leaked your bot's token. You'll want to reset it now.
You've also leaked your KironOS API Admin License code. I'm not exactly sure what this is, but if you can reset it, you'll probably want to as well.

The cause of your bot "closing after a few minutes" is likely because you're doing something blocking.
urllib.request.urlopen, and urllib.request in general, is blocking, and you should use aiohttp instead.

For further help using the library, you should join either the official discord.py server or the Discord API server, as the README recommends.

i don't really care about my KironOS API Admin License code, I own the website I got it from, I could just edit the License code any time

And I don't use urllib.request.urlopen except for the KironOS Python API 2018 but unfortunately removing urllib.request.urlopen from the code didn't work

Its fine if you don't care about the license code, but people have already started to dissect your code and find vulnerabilities in you website. I suggest if you actually care about it you shut it down before they find a way to break it too much and read up on some security techniques.

Can confirm, we were able to get code execution on your website, find your password, and log in to your github account. Please change your password ASAP and consider using a password manager :)

So? What's my password then?

ravi2005

If you want me to remove the message above just say the word 馃憤

My license code doesn't even leak my password. It only md5 encrypted for KironDevCoder and the KironOS Python API 2018 doesn't need login to an account. So you must have used some md5 and sha1 decrypter and maybe an view-source tool too. Else explain me please how you guys were able to edit my code.

MD5 is not encryption sadly. It is a cryptographic hash algorithm, and whilst the plaintext is unrecoverable, your password appears in some very common wordlists. Look up what rainbow tables are.

Dude go explain how a H1 tag that says "fix your damn site" got plastered on nearly every page of your website from that one link, your shitty code base gave plenty people plenty of hints how to mess up your site don't bother defending its quality or security

Easy mental :^)

I recommend reading through the OWASP top 10-2017 and understanding each of the potential vulnerabilities outlined there, so you know what to change in your code to make it secure.

And i strongly suggest closing this issue and just asking in the discord.py support server if you have an issue with the library

Ok, and whatever you guys do with my website, as long as you don't edit my private webserver I didn't leak because I crypted it with my own crypt software you'll be send to the homepage of KironWeb if anything works correctly. But first tell me how you did this

Do you speak Dutch? If not and you don't translate it you won't understand what it is

oh is this your famously secure crypt software? https://hastebin.com/ekovovayoz.xml
You literally concatenate the strings, can you spell attack surface?

a t t a c k s u r f a c e

Take this elsewhere.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TomOrth picture TomOrth  路  15Comments

imayhaveborkedit picture imayhaveborkedit  路  58Comments

SakiiR picture SakiiR  路  17Comments

ams2990 picture ams2990  路  14Comments

Scoder12 picture Scoder12  路  14Comments