Пример #1
0
async def welcome(_, message: Message):
    global answers_dicc
    """ Get cached answers from mongodb in case of bot's been restarted or crashed. """
    answers_dicc = await get_captcha_cache()
    """Mute new member and send message with button"""
    if not await is_captcha_on(message.chat.id):
        return
    for member in message.new_chat_members:
        try:
            if member.id in SUDOERS:
                continue  # ignore sudos
            if await is_gbanned_user(member.id):
                await message.chat.kick_member(member.id)
                await message.reply_text(
                    f"{member.mention} was globally banned, and got removed," +
                    " if you think this is a false gban, you can appeal" +
                    " for this ban in support chat.")
                continue
            if member.is_bot:
                continue  # ignore bots
            await message.chat.restrict_member(member.id, ChatPermissions())
            text = (
                f"{(member.mention())} Are you human?\n"
                f"Solve this captcha in {WELCOME_DELAY_KICK_SEC} seconds and 4 attempts or you'll be kicked."
            )
        except ChatAdminRequired:
            return
        # Generate a captcha image, answers and some wrong answers
        captcha = generate_captcha()
        captcha_image = captcha[0]
        captcha_answer = captcha[1]
        wrong_answers = captcha[2]  # This consists of 8 wrong answers
        correct_button = InlineKeyboardButton(
            f"{captcha_answer}",
            callback_data=f"pressed_button {captcha_answer} {member.id}",
        )
        temp_keyboard_1 = [correct_button]  # Button row 1
        temp_keyboard_2 = []  # Botton row 2
        temp_keyboard_3 = []
        for i in range(2):
            temp_keyboard_1.append(
                InlineKeyboardButton(
                    f"{wrong_answers[i]}",
                    callback_data=
                    f"pressed_button {wrong_answers[i]} {member.id}",
                ))
        for i in range(2, 5):
            temp_keyboard_2.append(
                InlineKeyboardButton(
                    f"{wrong_answers[i]}",
                    callback_data=
                    f"pressed_button {wrong_answers[i]} {member.id}",
                ))
        for i in range(5, 8):
            temp_keyboard_3.append(
                InlineKeyboardButton(
                    f"{wrong_answers[i]}",
                    callback_data=
                    f"pressed_button {wrong_answers[i]} {member.id}",
                ))

        shuffle(temp_keyboard_1)
        keyboard = [temp_keyboard_1, temp_keyboard_2, temp_keyboard_3]
        shuffle(keyboard)
        verification_data = {
            "chat_id": message.chat.id,
            "user_id": member.id,
            "answer": captcha_answer,
            "keyboard": keyboard,
            "attempts": 0,
        }
        keyboard = InlineKeyboardMarkup(keyboard)
        # Append user info, correct answer and
        answers_dicc.append(verification_data)
        # keyboard for later use with callback query
        button_message = await message.reply_photo(
            photo=captcha_image,
            caption=text,
            reply_markup=keyboard,
            quote=True,
        )
        os.remove(captcha_image)
        """ Save captcha answers etc in mongodb in case bot gets crashed or restarted. """
        await update_captcha_cache(answers_dicc)
        asyncio.create_task(
            kick_restricted_after_delay(WELCOME_DELAY_KICK_SEC, button_message,
                                        member))
        await asyncio.sleep(0.5)
Пример #2
0
async def welcome(_, message: Message):
    global answers_dicc
    """Mute new member and send message with button"""
    if not await is_captcha_on(message.chat.id):
        return
    for member in message.new_chat_members:
        try:
            if member.id in SUDOERS:
                continue  # ignore sudos
            if await is_gbanned_user(member.id):
                await message.chat.kick_member(member.id)
                continue
            if member.is_bot:
                continue  # ignore bots
            await message.chat.restrict_member(member.id, ChatPermissions())
            text = (
                f"Welcome, {(member.mention())} Are you human?\n"
                f"Solve this captcha in {WELCOME_DELAY_KICK_SEC} seconds or you'll be kicked."
            )
        except ChatAdminRequired:
            break
        # Generate a captcha image, answers and some wrong answers
        captcha = generate_captcha()
        captcha_image = captcha[0]
        captcha_answer = captcha[1]
        wrong_answers = captcha[2]  # This consists of 7 wrong answers
        correct_button = InlineKeyboardButton(
            f"{captcha_answer}",
            callback_data=f"pressed_button {captcha_answer} {member.id}")
        temp_keyboard_1 = [correct_button]  # Button row 1
        temp_keyboard_2 = []  # Botton row 2
        for i in range(3):
            temp_keyboard_1.append(
                InlineKeyboardButton(
                    f"{wrong_answers[i]}",
                    callback_data=
                    f"pressed_button {wrong_answers[i]} {member.id}"))
        for i in range(3, 7):
            temp_keyboard_2.append(
                InlineKeyboardButton(
                    f"{wrong_answers[i]}",
                    callback_data=
                    f"pressed_button {wrong_answers[i]} {member.id}"))
        shuffle(temp_keyboard_1)
        keyboard = [temp_keyboard_1, temp_keyboard_2]
        shuffle(keyboard)
        verification_data = {
            "user_id": member.id,
            "answer": captcha_answer,
            "keyboard": keyboard
        }
        keyboard = InlineKeyboardMarkup(keyboard)
        # Append user info, correct answer and
        answers_dicc.append(verification_data)
        # keyboard for later use with callback query
        button_message = await message.reply_photo(photo=captcha_image,
                                                   caption=text,
                                                   reply_markup=keyboard,
                                                   quote=True)
        os.remove(captcha_image)
        asyncio.create_task(
            kick_restricted_after_delay(WELCOME_DELAY_KICK_SEC, button_message,
                                        member))
        await asyncio.sleep(0.5)