How to retry connecting discord bot after some time if there is no internet connection?

Viewed 424

I want my discord bot to wait for some time and retry to connect if it failed to connect at first try.

I tried this:

[Other codes]
.
.
.

while True:
    client = MyClient()
    try:                            
        client.run(botToken)
    except aiohttp.client_exceptions.ClientConnectorError:
        print('Error, Trying again after 10 sec')
        time.sleep(10)

I run the code by turning off the internet but this give me another error.

RuntimeError: Event loop is closed                                                                                
sys:1: RuntimeWarning: coroutine 'Client.run.<locals>.runner' was never awaited
1 Answers

You'll have to start a new Client like this:

[Bot Code]
while True:
    try:
        client = MyClient(loop=asyncio.new_event_loop())                    
        client.run(botToken)
    except Exception as e:
        print(f'Restarting in 10s\nError: {e}')
        sleep(10)
Related