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

76 lines
2.3 KiB
Python
Raw Permalink Normal View History

2023-08-10 14:05:40 +03:00
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
2023-08-11 16:04:21 +03:00
from classes.callbacks import CallbackVerify
2023-08-10 14:05:40 +03:00
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]*")) # type: ignore
2023-08-10 14:05:40 +03:00
async def callback_verify(app: PyroClient, callback: CallbackQuery):
2023-08-11 16:04:21 +03:00
parsed = CallbackVerify.from_callback(callback)
2023-08-17 17:37:42 +03:00
group = await app.find_group(callback.message.chat.id)
2023-08-11 16:04:21 +03:00
locale = group.select_locale(app, callback.message.from_user)
2023-08-10 14:05:40 +03:00
2023-08-11 16:04:21 +03:00
if callback.from_user.id != parsed.user_id:
await callback.answer(
app._("wrong_user", "callbacks", locale=locale), show_alert=True
)
2023-08-10 14:05:40 +03:00
return
2023-08-11 16:04:21 +03:00
user = await app.find_user(callback.from_user, group.id)
2023-08-10 14:05:40 +03:00
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,
)
2023-08-11 16:04:21 +03:00
scheduled_job = await col_schedule.find_one_and_delete(
{"user": parsed.user_id, "group": group.id}
2023-08-10 14:05:40 +03:00
)
if scheduled_job is not None and app.scheduler is not None:
with contextlib.suppress(JobLookupError):
app.scheduler.remove_job(scheduled_job["job_id"])
2023-08-11 16:04:21 +03:00
await user.set_emojis(captcha.emojis_correct)
2023-08-10 14:05:40 +03:00
buttons = [
InlineButton(emoji, f"emoji:{user.id}:{emoji}") for emoji in captcha.emojis_all
2023-08-10 14:05:40 +03:00
]
keyboard = InlineKeyboard(3)
keyboard.add(*buttons)
await callback.message.delete()
captcha_message = await app.send_photo(
2023-08-11 16:04:21 +03:00
group.id,
2023-08-10 14:05:40 +03:00
captcha.image,
2023-08-11 16:04:21 +03:00
caption=app._("verify", "messages", locale=locale),
2023-08-10 14:05:40 +03:00
reply_markup=keyboard,
)
del captcha
if app.scheduler is not None:
app.scheduler.add_job(
kick_unverified,
"date",
2023-08-11 16:04:21 +03:00
[app, user.id, group.id, captcha_message.id],
2023-08-14 13:34:52 +03:00
run_date=datetime.now() + timedelta(seconds=group.timeout_verify),
2023-08-10 14:05:40 +03:00
)