Files
Telegram-bot-old/main.py

49 lines
1.2 KiB
Python

import asyncio
from aiogram import Bot, Dispatcher
from databases.db_config import init_postgresql, init_mongodb, close_connections
from aiogram.types import BotCommand
from utils.LogCon import setup_logger, load_config
from Middleware.anti_spam_middleware import AntiSpamMiddleware
import logging
setup_logger()
logger = logging.getLogger(__name__)
BOT_TOKEN = load_config()['token']
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()
dp.message.middleware(AntiSpamMiddleware(rate_limit=1))
async def set_commands():
commands = [
BotCommand(command="/start", description="Запустить бота"),
]
await bot.set_my_commands(commands)
async def on_startup():
await init_mongodb()
await set_commands()
print("Бот запущен!")
async def on_shutdown():
await close_connections()
await bot.session.close()
print("Бот остановлен.")
async def main():
from handlers.handlers import register_handlers
register_handlers(dp)
await init_postgresql() # Убедитесь, что таблицы создаются здесь
await on_startup()
try:
await dp.start_polling(bot)
finally:
await on_shutdown()
if __name__ == "__main__":
asyncio.run(main())