shopbot/__main__.py
2024-12-25 04:30:33 +03:00

80 lines
1.9 KiB
Python

import asyncio
import logging
import sys
from aiogram import Bot, Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.types import ErrorEvent
from aiogram_dialog import setup_dialogs
from environs import Env
from faststream.nats import NatsBroker
from shopbot.bot.commands import command_router
from shopbot.bot.database import initialize_database
from shopbot.bot.dialogs import (
admin_dialog,
distribution_dialog,
gift_balance_dialog,
history_dialog,
profile_dialog,
purchase_accounts_dialog,
start_dialog,
take_balance_dialog,
up_balance_cryptobot_dialog,
up_balance_dialog,
up_balance_lolz_dialog,
)
from shopbot.consumers.new_account import router as new_account_router
async def on_error(event: ErrorEvent, bot: Bot):
logging.error(f"An error occurred: {event.exception}", exc_info=True)
async def main():
env = Env()
env.read_env()
bot = Bot(
token=env("BOT_TOKEN"), default=DefaultBotProperties(parse_mode=ParseMode.HTML)
)
dp = Dispatcher()
broker = NatsBroker()
broker.include_router(new_account_router)
await initialize_database()
ROUTERS = [
command_router,
admin_dialog,
distribution_dialog,
gift_balance_dialog,
take_balance_dialog,
start_dialog,
purchase_accounts_dialog,
history_dialog,
profile_dialog,
up_balance_dialog,
up_balance_cryptobot_dialog,
up_balance_lolz_dialog,
]
for router in ROUTERS:
dp.include_router(router)
dp.error.register(on_error)
setup_dialogs(dp)
try:
async with broker:
await broker.start()
await dp.start_polling(bot)
finally:
await bot.session.close()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())