如果没有互联网连接,如何在一段时间后重试连接 discord bot?

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

我希望我的 discord bot 等待一段时间,如果第一次连接失败,我会重试连接。

我试过这个:

[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)

我通过关闭互联网 运行 代码,但这给了我另一个错误。

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

您必须像这样启动一个新客户端:

[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)