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

78 lines
2.4 KiB
Python
Raw 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
2023-08-11 16:04:21 +03:00
from classes.pyrogroup import PyroGroup
2023-08-10 14:05:40 +03:00
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)
group = await PyroGroup.create_if_not_exists(callback.message.chat.id, None, True)
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-10 14:05:40 +03:00
run_date=datetime.now()
+ timedelta(seconds=app.config["timeouts"]["verify"]),
)