Skip to content

Commit

Permalink
modified: bot.py
Browse files Browse the repository at this point in the history
	modified:   cogs/dvp.py
	modified:   cogs/rate.py
	modified:   logger.py
	modified:   twitch_helix_client.py
  • Loading branch information
Revulate committed Oct 15, 2024
1 parent daeca0d commit 8c16998
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 214 deletions.
29 changes: 28 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def __init__(self):
self.twitch_api.refresh_token = self.refresh_token_string
self.logger.info("TwitchAPI instance created and tokens saved")

self._connection_retries = 0
self._max_retries = 5

async def event_ready(self):
self.logger.info(f"Logged in as | {self.nick}")
self.load_tokens_from_file()
Expand Down Expand Up @@ -102,6 +105,31 @@ async def event_ready(self):
)
self.logger.info(f"Token expiry: {self.twitch_api.token_expiry}")

async def start(self):
while self._connection_retries < self._max_retries:
try:
await super().start()
break
except Exception as e:
self._connection_retries += 1
self.logger.error(f"Connection attempt {self._connection_retries} failed: {e}")
if self._connection_retries < self._max_retries:
await asyncio.sleep(5 * self._connection_retries) # Exponential backoff
else:
self.logger.error("Max retries reached. Unable to connect.")
raise

async def close(self):
try:
if self._connection and hasattr(self._connection, "_close"):
await self._connection._close()
if hasattr(self, "_http") and self._http:
await self._http.close()
except Exception as e:
self.logger.error(f"Error during close: {e}")
finally:
await super().close()

async def check_token_regularly(self):
while True:
await asyncio.sleep(3600) # Check every hour
Expand Down Expand Up @@ -218,7 +246,6 @@ async def event_error(self, error: Exception, data: str = None):


async def main():
load_dotenv()
bot = TwitchBot()

while True:
Expand Down
Loading

0 comments on commit 8c16998

Please sign in to comment.