diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..d013ef4 --- /dev/null +++ b/bot.py @@ -0,0 +1,80 @@ +#!/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() diff --git a/setup.py b/setup.py index 0333cfd..13016d1 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( "aiohttp>=3.9.5", "aiofiles>=23.2.1", "redis>=5.0.8", - "aiogram>=3.10.0" + "aiogram>=3.10.0", ], packages=["tubot", "tubot.static", "tubot.torrent", "tubot.dirgetter", "db"], ) diff --git a/tubot/bot.py b/tubot/bot.py index 3dfbd7e..d013ef4 100644 --- a/tubot/bot.py +++ b/tubot/bot.py @@ -16,6 +16,8 @@ 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 @@ -26,9 +28,9 @@ torrent_api: TorrentAPI ctrl: Controller ## --- Bot --- ## -bot: Bot -router: Router -dp: Dispatcher +router = Router() +dp = Dispatcher() +dp.include_router(router) async def initialize() -> None: @@ -37,10 +39,6 @@ async def initialize() -> None: torrent_api, dirgetter, cache = await init_modules() ctrl = Controller(torrent_api, dirgetter, cache) # --- Bot --- # - global router, dp, bot - router = Router() - dp = Dispatcher() - dp.include_router(router) bot = Bot(BOT_TOKEN()()) await dp.start_polling(bot) @@ -49,5 +47,34 @@ 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() diff --git a/tubot/static/env.py b/tubot/static/env.py index 8b3646b..442820b 100644 --- a/tubot/static/env.py +++ b/tubot/static/env.py @@ -129,5 +129,6 @@ class CACHE_EXPIRE(ENV): """ Cache expire time in seconds """ + _name = "CACHE_EXPIRE" DEFAULT = "120"