From 0e79ce74e12816fde9b2b83c929234d5730155f2 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Mon, 27 Nov 2023 22:21:03 +0300 Subject: [PATCH] Moved bot class to neuroapi module --- main.py | 30 ++---------------------------- neuroapi/types/__init__.py | 1 + neuroapi/types/_bot.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 neuroapi/types/_bot.py diff --git a/main.py b/main.py index 5795646..98c1979 100644 --- a/main.py +++ b/main.py @@ -3,39 +3,13 @@ import logging import signal import sys -# import aioschedule as schedule -from aiogram import Bot, Dispatcher - from handlers.admin_commands import AdminCommands -from handlers.handler import Handler from handlers.user_commands import UserCommands from neuroapi.config import Config +from neuroapi.types import NeuroApiBot +# import aioschedule as schedule -class NeuroApiBot: - bot: Bot - dp: Dispatcher - - _instances = {} - - def __init__(self, token: str) -> None: - self.bot = Bot(token) - self.dp = Dispatcher() - self._instances - - def __new__(cls, token: str) -> 'NeuroApiBot': - assert isinstance(token, str) - if token not in cls._instances: - cls._instances[token] = super(NeuroApiBot, cls).__new__(cls) - return cls._instances[token] - - def include_router(self, *routerClasses: Handler) -> None: - for routerClass in routerClasses: - assert issubclass(routerClass, Handler) - self.dp.include_routers(routerClass(self.bot)()) - - async def start(self, skip_updates=True): - await self.dp.start_polling(self.bot, skip_updates=skip_updates) async def delay_bot()->None: if Config().token is None: diff --git a/neuroapi/types/__init__.py b/neuroapi/types/__init__.py index bfa4e2f..f3e3efe 100644 --- a/neuroapi/types/__init__.py +++ b/neuroapi/types/__init__.py @@ -1,4 +1,5 @@ from ._admin import Admin +from ._bot import NeuroApiBot from ._image import Image from ._post import Post from ._singleton import Singleton diff --git a/neuroapi/types/_bot.py b/neuroapi/types/_bot.py new file mode 100644 index 0000000..6b9febf --- /dev/null +++ b/neuroapi/types/_bot.py @@ -0,0 +1,29 @@ +from aiogram import Bot, Dispatcher + +from handlers.handler import Handler + + +class NeuroApiBot: + bot: Bot + dp: Dispatcher + + _instances = {} + + def __init__(self, token: str) -> None: + self.bot = Bot(token) + self.dp = Dispatcher() + self._instances + + def __new__(cls, token: str) -> 'NeuroApiBot': + assert isinstance(token, str) + if token not in cls._instances: + cls._instances[token] = super(NeuroApiBot, cls).__new__(cls) + return cls._instances[token] + + def include_router(self, *routerClasses: Handler) -> None: + for routerClass in routerClasses: + assert issubclass(routerClass, Handler) + self.dp.include_routers(routerClass(self.bot)()) + + async def start(self, skip_updates=True): + await self.dp.start_polling(self.bot, skip_updates=skip_updates) \ No newline at end of file