From a83e27c7ed3237afa305fc4e0996d5c150cff386 Mon Sep 17 00:00:00 2001 From: Kami Date: Sun, 2 Feb 2025 12:39:53 +0100 Subject: [PATCH] - Added automatic reconnect after refreshing Twitch bot token. - Added reattempt limit to prevent Twitch ratelimiting. --- bot_twitch.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/bot_twitch.py b/bot_twitch.py index 9e609c8..e247561 100644 --- a/bot_twitch.py +++ b/bot_twitch.py @@ -120,15 +120,23 @@ class TwitchBot(commands.Bot): """ Run the Twitch bot, refreshing tokens if needed. """ - try: - await self.start() - #while True: - # await self.refresh_access_token() - # await asyncio.sleep(10800) # Refresh every 3 hours - except Exception as e: - self.log(f"Twitch bot failed to start: {e}", "CRITICAL") - if "Invalid or unauthorized Access Token passed." in str(e): - try: - await self.refresh_access_token() - except Exception as e: - self.log(f"Unable to refresh Twitch token! Twitch bot will be offline!", "CRITICAL") + retries = 0 + while True: + if retries > 3: + self.log(f"Twitch bot failed to connect after {retries} attempts.", "CIRITCAL") + break # Break loop if repeatedly failing to connect to Twitch + try: + await self.start() + #while True: + # await self.refresh_access_token() + # await asyncio.sleep(10800) # Refresh every 3 hours + except Exception as e: + retries += 1 + self.log(f"Twitch bot failed to start: {e}", "CRITICAL") + if "Invalid or unauthorized Access Token passed." in str(e): + try: + await self.refresh_access_token() + except Exception as e: + self.log(f"Unable to refresh Twitch token! Twitch bot will be offline!", "CRITICAL") + await asyncio.sleep(5) # Wait before retrying to authenticate +