From 15a659cfd11884622ceac6cbda05be314baff96e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Tue, 3 Jul 2018 16:54:48 +0200 Subject: [PATCH] Fix bot event loop --- bot.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/bot.py b/bot.py index 2c6f436..4ba259e 100644 --- a/bot.py +++ b/bot.py @@ -37,22 +37,30 @@ def main(): commands = Commands(config, commands_logger) - discord_client = DiscordClient(config, discord_logger, commands) - async def run_twitch_client(): twitch_client = TwitchClient(config, twitch_logger, commands) twitch_client.connect_() while True: - twitch_client.process_data() - await asyncio.sleep(TIMEOUT) + try: + twitch_client.process_data() + await asyncio.sleep(TIMEOUT) + except Exception as e: + twitch_logger.info('Exception', exc_info=e) - asyncio.ensure_future(run_twitch_client()) + async def run_discord_client(): + discord_client = DiscordClient(config, discord_logger, commands) + while True: + try: + await discord_client.start_() + except Exception as e: + discord_logger.info('Exception', exc_info=e) + await discord_client.logout() loop = asyncio.get_event_loop() try: - loop.run_until_complete(discord_client.start_()) - except: - loop.run_until_complete(discord_client.logout()) + asyncio.ensure_future(run_twitch_client()) + asyncio.ensure_future(run_discord_client()) + loop.run_forever() finally: loop.close()