This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
EmojiCaptchaBot/plugins/callbacks/verify.py
2023-08-10 13:05:40 +02:00

72 lines
2.1 KiB
Python

import contextlib
import logging
from datetime import datetime, timedelta
from apscheduler.jobstores.base import JobLookupError
from pykeyboard import InlineButton, InlineKeyboard
from pyrogram import filters
from pyrogram.types import CallbackQuery
from classes.pyroclient import PyroClient
from modules.database import col_schedule
from modules.kicker import kick_unverified
from modules.utils import get_captcha_image
logger = logging.getLogger(__name__)
@PyroClient.on_callback_query(filters.regex(r"verify;[\s\S]*"))
async def callback_verify(app: PyroClient, callback: CallbackQuery):
user_id = int(str(callback.data).split(";")[1])
if callback.from_user.id != user_id:
await callback.answer(app._("wrong_user", "callbacks"), show_alert=True)
return
user = await app.find_user(callback.from_user, callback.message.chat.id)
captcha = get_captcha_image(app.config["emojis"])
logger.info(
"Captcha for %s has been generated. All: %s, Correct: %s",
user.id,
captcha.emojis_all,
captcha.emojis_correct,
)
scheduled_job = col_schedule.find_one_and_delete(
{"user": user_id, "group": callback.message.chat.id}
)
if scheduled_job is not None and app.scheduler is not None:
with contextlib.suppress(JobLookupError):
app.scheduler.remove_job(scheduled_job["job_id"])
user.set_emojis(captcha.emojis_correct)
buttons = [
InlineButton(emoji, f"emoji;{user.id};{emoji}") for emoji in captcha.emojis_all
]
keyboard = InlineKeyboard(3)
keyboard.add(*buttons)
await callback.message.delete()
captcha_message = await app.send_photo(
callback.message.chat.id,
captcha.image,
caption=app._("verify", "messages"),
reply_markup=keyboard,
)
del captcha
if app.scheduler is not None:
app.scheduler.add_job(
kick_unverified,
"date",
[app, user.id, callback.message.chat.id, captcha_message.id],
run_date=datetime.now()
+ timedelta(seconds=app.config["timeouts"]["verify"]),
)