Feat: changed bot starting

This commit is contained in:
2024-12-26 16:08:32 +03:00
parent 2882688ff4
commit 53bb09f41c
4 changed files with 25 additions and 10 deletions

10
main.py
View File

@@ -3,9 +3,8 @@ import logging
from sys import exit
import uvloop
from aiogram import Bot, Dispatcher
from nwxraybot import Settings
from nwxraybot import NwXrayBot, Settings
from nwxraybot.handlers import HelloHandler
from nwxraybot.models import User
@@ -21,7 +20,6 @@ if __name__ == "__main__":
User.create_table()
# Start bot
bot = Bot(token=config.bot_token.get_secret_value())
dp = Dispatcher()
dp.include_routers(HelloHandler(bot)())
uvloop.run(dp.start_polling(bot, skip_updates=True))
bot = NwXrayBot(config.bot_token.get_secret_value())
bot.include_routers(HelloHandler(bot)())
uvloop.run(bot.start(skip_updates=True))

View File

@@ -1 +1,2 @@
from nwxraybot.bot import NwXrayBot
from nwxraybot.config import Settings

View File

@@ -0,0 +1,14 @@
from aiogram import Bot, Dispatcher, Router
class NwXrayBot:
def __init__(self, token: str) -> None:
self.bot = Bot(token=token)
self.dp = Dispatcher()
def include_routers(self, *routers: Router):
for router in routers:
self.dp.include_router(router)
async def start(self, skip_updates: bool = False) -> None:
await self.dp.start_polling(self.bot, skip_updates=skip_updates)

View File

@@ -1,12 +1,14 @@
from aiogram import Bot, Router
from aiogram import Router
from nwxraybot import NwXrayBot
class Handler:
bot: Bot
bot: NwXrayBot
router: Router
def __init__(self, bot: Bot) -> None:
assert isinstance(bot, Bot)
def __init__(self, bot: NwXrayBot) -> None:
assert isinstance(bot, NwXrayBot)
self.bot = bot
self.router = Router()