import logging from pyrogram import filters from pyrogram.types import ( CallbackQuery, ChatPermissions, InlineKeyboardButton, InlineKeyboardMarkup, Message, ) from classes.pyroclient import PyroClient logger = logging.getLogger(__name__) @PyroClient.on_callback_query(filters.regex(r"emoji;[\s\S]*")) async def callback_emoji_button(app: PyroClient, callback: CallbackQuery): user_id = int(str(callback.data).split(";")[1]) emoji = str(callback.data).split(";")[2] 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) logger.debug( "User %s has pressed the %s emoji '%s'", user.id, "correct" if emoji in user.emojis else "wrong", emoji, ) if emoji in user.selected: await callback.answer() return user.update_selected(emoji) if emoji in user.emojis: user.update_score(1) if user.score >= 5: logger.info("User %s has passed the captcha", user.id) await callback.message.delete() await callback.answer(app._("captcha_passed", "callbacks"), show_alert=True) await app.send_message( callback.message.chat.id, app._( "welcome", "messages", ).format(mention=callback.from_user.mention), ) await app.restrict_chat_member( chat_id=callback.message.chat.id, user_id=callback.from_user.id, permissions=ChatPermissions(can_send_messages=True), ) 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) user.set_failed(True) await callback.answer(app._("captcha_failed", "callbacks"), show_alert=True) banned = await app.ban_chat_member( callback.message.chat.id, callback.from_user.id ) if isinstance(banned, Message): await banned.delete() await callback.message.delete() return user.update_mistakes(1) logger.info( "User %s has made a mistake and has %s attempt(s) left", user.id, 2 - user.mistakes, ) await callback.answer( app._("captcha_mistake", "callbacks").format(remaining=2 - user.mistakes), 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): if button.text == emoji: 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) )