2023-08-10 14:05:40 +03:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from pyrogram import filters
|
|
|
|
from pyrogram.types import (
|
|
|
|
CallbackQuery,
|
|
|
|
ChatPermissions,
|
|
|
|
InlineKeyboardButton,
|
|
|
|
InlineKeyboardMarkup,
|
|
|
|
Message,
|
|
|
|
)
|
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
from classes.callbacks import CallbackEmoji
|
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
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-08-12 23:36:55 +03:00
|
|
|
@PyroClient.on_callback_query(filters.regex(r"emoji:[\s\S]*")) # type: ignore
|
2023-08-10 14:05:40 +03:00
|
|
|
async def callback_emoji_button(app: PyroClient, callback: CallbackQuery):
|
2023-08-11 16:04:21 +03:00
|
|
|
parsed = CallbackEmoji.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
|
|
|
|
|
|
|
logger.debug(
|
|
|
|
"User %s has pressed the %s emoji '%s'",
|
|
|
|
user.id,
|
2023-08-11 16:04:21 +03:00
|
|
|
"correct" if parsed.emoji in user.emojis else "wrong",
|
|
|
|
parsed.emoji,
|
2023-08-10 14:05:40 +03:00
|
|
|
)
|
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
if parsed.emoji in user.selected:
|
2023-08-10 14:05:40 +03:00
|
|
|
await callback.answer()
|
|
|
|
return
|
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
await user.update_selected(parsed.emoji)
|
2023-08-10 14:05:40 +03:00
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
if parsed.emoji in user.emojis:
|
|
|
|
await user.update_score(1)
|
2023-08-10 14:05:40 +03:00
|
|
|
|
|
|
|
if user.score >= 5:
|
|
|
|
logger.info("User %s has passed the captcha", user.id)
|
|
|
|
|
|
|
|
await callback.message.delete()
|
2023-08-11 16:04:21 +03:00
|
|
|
await callback.answer(
|
|
|
|
app._("captcha_passed", "callbacks", locale=locale), show_alert=True
|
|
|
|
)
|
2023-08-10 14:05:40 +03:00
|
|
|
await app.send_message(
|
2023-08-11 16:04:21 +03:00
|
|
|
group.id,
|
|
|
|
app._("welcome", "messages", locale=locale).format(
|
|
|
|
mention=callback.from_user.mention
|
|
|
|
),
|
2023-08-10 14:05:40 +03:00
|
|
|
)
|
|
|
|
await app.restrict_chat_member(
|
2023-08-11 16:04:21 +03:00
|
|
|
chat_id=group.id,
|
2023-08-10 14:05:40 +03:00
|
|
|
user_id=callback.from_user.id,
|
2023-08-12 23:35:36 +03:00
|
|
|
permissions=callback.message.chat.permissions,
|
2023-08-10 14:05:40 +03:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"User %s has chosen correctly and needs to select %s more emoji(s)",
|
|
|
|
user.id,
|
|
|
|
5 - user.score,
|
|
|
|
)
|
|
|
|
|
|
|
|
emoji_placeholder = "✅"
|
|
|
|
else:
|
|
|
|
if user.mistakes >= 2:
|
|
|
|
logger.info("User %s has failed the captcha", user.id)
|
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
await user.set_failed(True)
|
|
|
|
await callback.answer(
|
|
|
|
app._("captcha_failed", "callbacks", locale=locale), show_alert=True
|
2023-08-10 14:05:40 +03:00
|
|
|
)
|
|
|
|
|
2023-08-14 13:11:53 +03:00
|
|
|
if group.ban_failed:
|
|
|
|
banned = await app.ban_chat_member(group.id, callback.from_user.id)
|
2023-08-11 16:04:21 +03:00
|
|
|
|
2023-08-14 13:11:53 +03:00
|
|
|
if isinstance(banned, Message):
|
|
|
|
await banned.delete()
|
2023-08-10 14:05:40 +03:00
|
|
|
|
|
|
|
await callback.message.delete()
|
|
|
|
return
|
|
|
|
|
2023-08-11 16:04:21 +03:00
|
|
|
await user.update_mistakes(1)
|
2023-08-10 14:05:40 +03:00
|
|
|
logger.info(
|
|
|
|
"User %s has made a mistake and has %s attempt(s) left",
|
|
|
|
user.id,
|
|
|
|
2 - user.mistakes,
|
|
|
|
)
|
|
|
|
|
|
|
|
await callback.answer(
|
2023-08-11 16:04:21 +03:00
|
|
|
app._("captcha_mistake", "callbacks", locale=locale).format(
|
|
|
|
remaining=2 - user.mistakes
|
|
|
|
),
|
2023-08-10 14:05:40 +03:00
|
|
|
show_alert=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
emoji_placeholder = "🛑"
|
|
|
|
|
|
|
|
button_replace = (0, 0)
|
|
|
|
|
|
|
|
for row_index, row in enumerate(callback.message.reply_markup.inline_keyboard):
|
|
|
|
for button_index, button in enumerate(row):
|
2023-08-11 16:04:21 +03:00
|
|
|
if button.text == parsed.emoji:
|
2023-08-10 14:05:40 +03:00
|
|
|
button_replace = (row_index, button_index)
|
|
|
|
|
|
|
|
new_keyboard = callback.message.reply_markup.inline_keyboard
|
|
|
|
new_keyboard[button_replace[0]][button_replace[1]] = InlineKeyboardButton(
|
|
|
|
emoji_placeholder, "nothing"
|
|
|
|
)
|
|
|
|
|
|
|
|
await callback.edit_message_reply_markup(
|
|
|
|
reply_markup=InlineKeyboardMarkup(new_keyboard)
|
|
|
|
)
|