Advertisement
DimaDevelop

Untitled

Apr 12th, 2025
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.39 KB | None | 0 0
  1. import asyncio
  2. from typing import Any
  3.  
  4. from loguru import logger
  5.  
  6. from core.config import settings
  7. from pydantic_models.group import GroupModel
  8. from pydantic_models.user import UserModel
  9. from repositories.user_repo import UserRepository
  10.  
  11.  
  12. class BotManager:
  13.     def __init__(self):
  14.         self.bots: dict[int, Any] = {}  # type: ignore
  15.         self.started_bots: dict[int, bool] = {}  # type: ignore
  16.  
  17.     def add_bot(self, bot_id: int, bot: Any):
  18.         self.bots[bot_id] = bot
  19.         logger.debug(
  20.             f"{'User' if bot_id > 0 else 'Group'} {bot_id} added to session combiner"
  21.         )
  22.  
  23.     def get_bot(self, bot_id: int) -> UserModel | None:
  24.         bot = self.bots.get(bot_id)
  25.         if not bot:
  26.             logger.debug(f"{'User' if bot_id > 0 else 'Group'} {bot_id} not found")
  27.             return None
  28.  
  29.         return bot
  30.  
  31.     def get_group(self) -> GroupModel | None:
  32.         bot = self.bots.get(-settings.GROUP_ID)
  33.         if not bot:
  34.             logger.debug("Group not found")
  35.             return None
  36.  
  37.         return bot
  38.  
  39.     async def init(self, bot_id: int):
  40.         bot = self.bots.get(bot_id)
  41.         if not bot:
  42.             logger.debug(f"{'User' if bot_id > 0 else 'Group'} {bot_id} not found")
  43.             return
  44.  
  45.         await bot.init()
  46.  
  47.     async def start_bot(self, bot_id: int):
  48.         bot = self.bots.get(bot_id)
  49.         if not bot:
  50.             logger.debug(f"{'User' if bot_id > 0 else 'Group'} {bot_id} not found")
  51.             return
  52.  
  53.         await bot.init()
  54.         await bot.run_bot()
  55.         self.started_bots[bot_id] = True
  56.         logger.debug(f"{'User' if bot_id > 0 else 'Group'} {bot_id} session is started")
  57.  
  58.     async def stop_bot(self, bot_id: int):
  59.         bot = self.bots.get(bot_id)
  60.         if not bot:
  61.             logger.debug(f"{'User' if bot_id > 0 else 'Group'} {bot_id} not found")
  62.             return False
  63.         await bot.stop_bot()
  64.         self.started_bots[bot_id] = False
  65.         logger.debug(f"{'User' if bot_id > 0 else 'Group'} {bot_id} session is stopped")
  66.         return True
  67.  
  68.     async def restart_bot(self, bot_id: int):
  69.         bot = self.bots.get(bot_id)
  70.         if not bot:
  71.             logger.debug(f"{'User' if bot_id > 0 else 'Group'} {bot_id} not found")
  72.             return False
  73.  
  74.         await self.stop_bot(bot_id)
  75.         await self.start_bot(bot_id)
  76.         self.started_bots[bot_id] = True
  77.         logger.debug(
  78.             f"{'User' if bot_id > 0 else 'Group'} {bot_id} session is restarted"
  79.         )
  80.         return True
  81.  
  82.     async def stop_all_bots(self):
  83.         for bot in self.bots.values():
  84.             await bot.stop_bot()
  85.         logger.debug("All bots stopped")
  86.  
  87.     async def delete_bot(self, bot_id: int):
  88.         bot = self.bots.get(bot_id)
  89.         if bot:
  90.             await bot.stop_bot()
  91.             del self.bots[bot_id]
  92.  
  93.         if self.started_bots.get(bot_id):
  94.             del self.started_bots[bot_id]
  95.  
  96.         await UserRepository.delete_user(user_id=bot_id)
  97.         logger.debug(f"{'User' if bot_id > 0 else 'Group'} {bot_id} session is deleted")
  98.  
  99.     async def run_health_checks(self):
  100.         while True:
  101.             bot_ids = list(self.bots.keys())
  102.             for bot_id in bot_ids:
  103.                 bot = self.bots.get(bot_id)
  104.                 if bot:
  105.                     await bot.health_check()
  106.             await asyncio.sleep(30)
  107.  
  108.  
  109. bot_manager = BotManager()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement