#Bot keeps disconnecting and reconnecting

1 messages · Page 1 of 1 (latest)

minor notch
#

Reasons unrelated to this, I needed to connect my bot to a webserver.

I use Sanic for my webserver client, and to run the bot I have the following code:

from thorny import bot # I import the bot from another file where all my listeners and events are configured

app = Sanic("thorny_bot_app")

@app.listener('after_server_start')
async def start_bot(application: Sanic):
    asyncio.get_event_loop().create_task(coro=bot.start(token=TOKEN, reconnect=True),
                                         name="Discord Client")

app.run(host="0.0.0.0")
#

So I set up for the bot to print to the console when it disconnects and then resumes the session, and heres what I found:

[2023-01-30 19:08:27 +0000] [7] [INFO] Sanic v22.9.0
[2023-01-30 19:08:27 +0000] [7] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2023-01-30 19:08:27 +0000] [7] [INFO] mode: production, single worker
[2023-01-30 19:08:27 +0000] [7] [INFO] server: sanic, HTTP/1.1
[2023-01-30 19:08:27 +0000] [7] [INFO] python: 3.10.8
[2023-01-30 19:08:27 +0000] [7] [INFO] platform: Linux-5.15.0-1028-aws-x86_64-with-glibc2.28
[2023-01-30 19:08:27 +0000] [7] [INFO] packages: sanic-routing==22.8.0
[2023-01-30 19:08:28 +0000] [21] [INFO] Starting worker [21]
     _____ _
    /__   \ |__   ___  _ __ _ __  _   _
      / /\/ '_ \ / _ \| '__| '_ \| | | |
     / /  | | | | (_) | |  | | | | |_| |
     \/   |_| |_|\___/|_|  |_| |_|\__, |
                                  |___/
[2023-01-30 19:08:30] [ONLINE] Thorny#3781
[2023-01-30 19:08:30] [SERVER] Running v1.8.9
[2023-01-30 19:08:30] [LOOP] Ran birthday checker loop
2023-01-30 19:09:25.880997 Disconnected at this time!
2023-01-30 19:09:27.271893 Resumed at this time!
2023-01-30 19:10:22.247442 Disconnected at this time!
2023-01-30 19:10:23.299208 Resumed at this time!
2023-01-30 19:11:18.304291 Disconnected at this time!
2023-01-30 19:11:23.843941 Resumed at this time!
2023-01-30 19:12:18.865782 Disconnected at this time!
2023-01-30 19:12:33.630933 Resumed at this time!
2023-01-30 19:15:37.665759 Resumed at this time!
2023-01-30 19:19:27.365024 Disconnected at this time!
2023-01-30 19:24:32.558473 Disconnected at this time!
     _____ _
    /__   \ |__   ___  _ __ _ __  _   _
      / /\/ '_ \ / _ \| '__| '_ \| | | |
     / /  | | | | (_) | |  | | | | |_| |
     \/   |_| |_|\___/|_|  |_| |_|\__, |
                                  |___/
[2023-01-30 19:24:39] [ONLINE] Thorny#3781
[2023-01-30 19:24:39] [SERVER] Running v1.8.9
2023-01-30 19:25:35.071935 Disconnected at this time!
#

The bot's on_ready event sends the ASCII stuff and the [ONLINE] message.
What essentially happens, is that the bot periodically disconnects and resumes. But sometimes it disconnects and just restarts. In the case that it does that, the bot also goes completely offline on discord.

I don't like that the bot disconnects at all, since during that period no Application Commands work and thats a bit annoying.

What I am trying to figure out is why it does this, and how to make it stop. I assume it has something to do with the fact that I am using bot.start() instead of the usual bot.run(), however, I can't use .run() as it'll block the event loop.

This is a very strange thing. There's no error messages, nothing to show why this is happening or whats causing it.

#

In this case, the last "Disconnect" happened, and the bot wouldnt go back online for another 10 or so minutes, and it then went back offline afterwards as it repeats the cycle

split swift
#

If you're going to run a web server alongside your bot, do it on the same asyncio loop as the bot

minor notch
#

I believe that is what I did with

asyncio.get_event_loop().create_task(coro=bot.start(token=TOKEN, reconnect=True), name="Discord Client")

It doesn't give me any errors to do with the loop, just that strange behaviour

split swift
#

You can just use bot.loop.create_task after you have started your bot to run tasks on the same loop

minor notch
#

Alright, so I run the bot like normal using bot.run() and then I run the webserver using bot.loop.create_task

I'll try that and see what happens

dark tapir
#

I rather suggest using asyncio.run along with bot.start and an async method to start your sanic webserver