Add auth system to bot && reformat

- add auth_check() function to bot
- add /auth command to bot
This commit is contained in:
trueold89 2024-08-15 19:56:24 +03:00
parent 3c02cc20e9
commit 0aa90228e0
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
4 changed files with 116 additions and 8 deletions

80
bot.py Normal file
View File

@ -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()

View File

@ -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"],
)

View File

@ -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()

View File

@ -129,5 +129,6 @@ class CACHE_EXPIRE(ENV):
"""
Cache expire time in seconds
"""
_name = "CACHE_EXPIRE"
DEFAULT = "120"