#!/usr/bin/python3 # -*- coding: utf-8 -*- ##################### # Aiogram bot logic # ##################### # Imports from asyncio import run from tubot.db.abc import CacheDB from tubot.dirgetter.abc import DirGetter from tubot.static.init import init_modules from tubot.static.controller import Controller from tubot.static.env import BOT_TOKEN from tubot.torrent.abc import TorrentAPI from aiogram import Bot, Dispatcher, Router from aiogram.types import Message from aiogram.filters.command import Command # Init ## --- Modules --- ## cache: CacheDB dirgetter: DirGetter torrent_api: TorrentAPI ctrl: Controller ## --- Bot --- ## router = Router() dp = Dispatcher() dp.include_router(router) async def initialize() -> None: # --- Modules --- # global cache, dirgetter, torrent_api, ctrl torrent_api, dirgetter, cache = await init_modules() ctrl = Controller(torrent_api, dirgetter, cache) # --- Bot --- # bot = Bot(BOT_TOKEN()()) await dp.start_polling(bot) def main() -> None: run(initialize()) ## -- Functions -- ## async def check_auth(msg: Message) -> bool: if msg.from_user is None: raise ValueError tg_id = msg.from_user.id name = msg.from_user.first_name user = await ctrl.get_user(tg_id, name) return user.auth @dp.message(Command("auth")) async def auth(msg: Message) -> None: if msg.from_user is None: raise ValueError password = msg.text if password: password = " ".join(password.split()[1:]) tgid = msg.from_user.id name = msg.from_user.first_name user = await ctrl.get_user(tgid, name) try: await ctrl.auth_user(user, password) await msg.answer("Auth complete!") except Exception as e: await msg.answer(str(e)) if __name__ == "__main__": main()