dragxd commited on
Commit
21de065
·
1 Parent(s): dadaa53

fixed the bot.py

Browse files
Files changed (1) hide show
  1. core/bot.py +22 -4
core/bot.py CHANGED
@@ -82,10 +82,17 @@ class Bot(TelegramClient):
82
  def __repr__(self):
83
  return "<AutoAnimeBot.Client :\n bot: {}\n>".format(self._bot)
84
 
85
- async def start_client(self, **kwargs):
86
  """function to start client"""
87
  if self._log_at:
88
  self.logger.info("Trying to login.")
 
 
 
 
 
 
 
89
  try:
90
  await self.start(**kwargs)
91
  if self.user_client:
@@ -101,9 +108,20 @@ class Bot(TelegramClient):
101
  self.logger.critical("String session expired.")
102
  except ConnectionError as e:
103
  self.logger.error(f"Connection failed: {e}")
104
- self.logger.info("Retrying connection in 10 seconds...")
105
- await asyncio.sleep(10)
106
- await self.start_client(**kwargs)
 
 
 
 
 
 
 
 
 
 
 
107
  except (AccessTokenExpiredError, AccessTokenInvalidError):
108
  self.logger.critical(
109
  "Bot token is expired or invalid. Create new from @Botfather and add in BOT_TOKEN env variable!"
 
82
  def __repr__(self):
83
  return "<AutoAnimeBot.Client :\n bot: {}\n>".format(self._bot)
84
 
85
+ async def start_client(self, **kwargs, retry_count=0):
86
  """function to start client"""
87
  if self._log_at:
88
  self.logger.info("Trying to login.")
89
+
90
+ # Maximum retry limit to prevent infinite loops
91
+ max_retries = 3
92
+ if retry_count >= max_retries:
93
+ self.logger.critical(f"Failed to connect after {max_retries} attempts. Exiting.")
94
+ sys.exit(1)
95
+
96
  try:
97
  await self.start(**kwargs)
98
  if self.user_client:
 
108
  self.logger.critical("String session expired.")
109
  except ConnectionError as e:
110
  self.logger.error(f"Connection failed: {e}")
111
+ if retry_count < max_retries - 1:
112
+ self.logger.info(f"Retrying connection in 10 seconds... (Attempt {retry_count + 1}/{max_retries})")
113
+ await asyncio.sleep(10)
114
+ await self.start_client(**kwargs, retry_count=retry_count + 1)
115
+ else:
116
+ self.logger.warning("Max retries reached. Starting bot without user client...")
117
+ # Start only the bot client without user client
118
+ try:
119
+ await self.start(**kwargs)
120
+ await self.pyro_client.start()
121
+ self.logger.info("Bot started successfully without user client")
122
+ except Exception as bot_error:
123
+ self.logger.critical(f"Failed to start bot: {bot_error}")
124
+ sys.exit(1)
125
  except (AccessTokenExpiredError, AccessTokenInvalidError):
126
  self.logger.critical(
127
  "Bot token is expired or invalid. Create new from @Botfather and add in BOT_TOKEN env variable!"