| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- """
- MIT License
- Copyright (c) 2021 TheHamkerCat
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- """
- import asyncio
- import os
- from datetime import datetime
- from random import shuffle
- from pykeyboard import InlineKeyboard
- from pyrogram import filters
- from pyrogram.errors.exceptions.bad_request_400 import (ChatAdminRequired,
- UserNotParticipant)
- from pyrogram.types import (ChatPermissions, InlineKeyboardButton,
- InlineKeyboardMarkup, Message, User)
- from wbb import SUDOERS, WELCOME_DELAY_KICK_SEC, app
- from wbb.core.decorators.errors import capture_err
- from wbb.modules.admin import member_permissions
- from wbb.utils.dbfunctions import (captcha_off, captcha_on, del_welcome,
- get_captcha_cache, get_welcome,
- is_captcha_on, is_gbanned_user, set_welcome,
- update_captcha_cache)
- from wbb.utils.filter_groups import welcome_captcha_group
- from wbb.utils.functions import generate_captcha
- __MODULE__ = "Greetings"
- __HELP__ = """
- /captcha [ON|OFF] - Enable/Disable captcha.
- /set_welcome - Reply this to a message containing correct
- format for a welcome message, check end of this message.
- /del_welcome - Delete the welcome message.
- /get_welcome - Get the welcome message.
- **SET_WELCOME ->**
- The format should be something like below.
- ```
- **Hi** {name} Welcome to {chat}
- ~ #This separater (~) should be there between text and buttons, remove this comment also
- button=[Duck, https://duckduckgo.com]
- button2=[Github, https://github.com]
- ```
- **NOTES ->**
- for /rules, you can do /filter rules to a message
- containing rules of your groups whenever a user
- sends /rules, he'll get the message
- """
- answers_dicc = []
- loop = asyncio.get_running_loop()
- async def get_initial_captcha_cache():
- global answers_dicc
- answers_dicc = await get_captcha_cache()
- return
- loop.create_task(get_initial_captcha_cache())
- @app.on_message(filters.new_chat_members, group=welcome_captcha_group)
- @capture_err
- 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)
- async def send_welcome_message(callback_query, pending_user_id):
- try:
- raw_text = await get_welcome(callback_query.message.chat.id)
- except TypeError:
- return
- raw_text = raw_text.strip().replace("`", "")
- if not raw_text:
- return
- text = raw_text.split("~")[0].strip()
- buttons_text_list = raw_text.split("~")[1].strip().splitlines()
- if "{chat}" in text:
- text = text.replace("{chat}", callback_query.message.chat.title)
- if "{name}" in text:
- text = text.replace(
- "{name}", (await app.get_users(pending_user_id)).mention
- )
- buttons = InlineKeyboard(row_width=2)
- list_of_buttons = []
- for button_string in buttons_text_list:
- button_string = button_string.strip().split("=")[1].strip()
- button_string = button_string.replace("[", "").strip()
- button_string = button_string.replace("]", "").strip()
- button_string = button_string.split(",")
- button_text = button_string[0].strip()
- button_url = button_string[1].strip()
- list_of_buttons.append(
- InlineKeyboardButton(text=button_text, url=button_url)
- )
- buttons.add(*list_of_buttons)
- await app.send_message(
- callback_query.message.chat.id,
- text=text,
- reply_markup=buttons,
- disable_web_page_preview=True,
- )
- @app.on_callback_query(filters.regex("pressed_button"))
- async def callback_query_welcome_button(_, callback_query):
- """After the new member presses the correct button,
- set his permissions to chat permissions,
- delete button message and join message.
- """
- global answers_dicc
- data = callback_query.data
- pressed_user_id = callback_query.from_user.id
- pending_user_id = int(data.split(None, 2)[2])
- button_message = callback_query.message
- answer = data.split(None, 2)[1]
- if len(answers_dicc) != 0:
- for i in answers_dicc:
- if (
- i["user_id"] == pending_user_id
- and i["chat_id"] == button_message.chat.id
- ):
- correct_answer = i["answer"]
- keyboard = i["keyboard"]
- if pending_user_id == pressed_user_id:
- if answer != correct_answer:
- await callback_query.answer("Yeah, It's Wrong.")
- for iii in answers_dicc:
- if (
- iii["user_id"] == pending_user_id
- and iii["chat_id"] == button_message.chat.id
- ):
- attempts = iii["attempts"]
- if attempts >= 3:
- answers_dicc.remove(iii)
- await button_message.chat.kick_member(pending_user_id)
- await asyncio.sleep(1)
- await button_message.chat.unban_member(pending_user_id)
- await button_message.delete()
- await update_captcha_cache(answers_dicc)
- return
- else:
- iii["attempts"] += 1
- break
- shuffle(keyboard[0])
- shuffle(keyboard[1])
- shuffle(keyboard[2])
- shuffle(keyboard)
- keyboard = InlineKeyboardMarkup(keyboard)
- await button_message.edit(
- text=button_message.caption.markdown, reply_markup=keyboard
- )
- return
- await callback_query.answer("Captcha passed successfully!")
- await button_message.chat.unban_member(pending_user_id)
- await button_message.delete()
- if len(answers_dicc) != 0:
- for ii in answers_dicc:
- if (
- ii["user_id"] == pending_user_id
- and ii["chat_id"] == button_message.chat.id
- ):
- answers_dicc.remove(ii)
- await update_captcha_cache(answers_dicc)
- """ send welcome message """
- await send_welcome_message(callback_query, pending_user_id)
- return
- else:
- await callback_query.answer("This is not for you")
- return
- async def kick_restricted_after_delay(
- delay, button_message: Message, user: User
- ):
- """If the new member is still restricted after the delay, delete
- button message and join message and then kick him
- """
- global answers_dicc
- await asyncio.sleep(delay)
- join_message = button_message.reply_to_message
- group_chat = button_message.chat
- user_id = user.id
- await join_message.delete()
- await button_message.delete()
- if len(answers_dicc) != 0:
- for i in answers_dicc:
- if i["user_id"] == user_id:
- answers_dicc.remove(i)
- await update_captcha_cache(answers_dicc)
- await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
- async def _ban_restricted_user_until_date(
- group_chat, user_id: int, duration: int
- ):
- try:
- member = await group_chat.get_member(user_id)
- if member.status == "restricted":
- until_date = int(datetime.utcnow().timestamp() + duration)
- await group_chat.kick_member(user_id, until_date=until_date)
- except UserNotParticipant:
- pass
- @app.on_message(filters.command("captcha") & ~filters.private)
- @capture_err
- async def captcha_state(_, message):
- usage = "**Usage:**\n/captcha [ON|OFF]"
- if len(message.command) != 2:
- await message.reply_text(usage)
- return
- user_id = message.from_user.id
- chat_id = message.chat.id
- permissions = await member_permissions(chat_id, user_id)
- if "can_restrict_members" not in permissions:
- await message.reply_text("You don't have enough permissions.")
- return
- state = message.text.split(None, 1)[1].strip()
- state = state.lower()
- if state == "on":
- await captcha_on(chat_id)
- await message.reply_text("Enabled Captcha For New Users.")
- elif state == "off":
- await captcha_off(chat_id)
- await message.reply_text("Disabled Captcha For New Users.")
- else:
- await message.reply_text(usage)
- """ WELCOME MESSAGE """
- @app.on_message(filters.command("set_welcome") & ~filters.private)
- @capture_err
- async def set_welcome_func(_, message):
- usage = "You need to reply to a text, check the Greetings module in /help"
- if not message.reply_to_message:
- await message.reply_text(usage)
- return
- if not message.reply_to_message.text:
- await message.reply_text(usage)
- return
- user_id = message.from_user.id
- chat_id = message.chat.id
- permissions = await member_permissions(chat_id, user_id)
- if "can_change_info" not in permissions:
- await message.reply_text("You don't have enough permissions.")
- return
- raw_text = str(message.reply_to_message.text.markdown)
- await set_welcome(chat_id, raw_text)
- await message.reply_text("Welcome message has been successfully set.")
- @app.on_message(filters.command("del_welcome") & ~filters.private)
- @capture_err
- async def del_welcome_func(_, message):
- user_id = message.from_user.id
- chat_id = message.chat.id
- permissions = await member_permissions(chat_id, user_id)
- if "can_change_info" not in permissions:
- await message.reply_text("You don't have enough permissions.")
- return
- await del_welcome(chat_id)
- await message.reply_text("Welcome message has been deleted.")
- @app.on_message(filters.command("get_welcome") & ~filters.private)
- @capture_err
- async def get_welcome_func(_, message):
- user_id = message.from_user.id
- chat_id = message.chat.id
- permissions = await member_permissions(chat_id, user_id)
- if "can_change_info" not in permissions:
- await message.reply_text("You don't have enough permissions.")
- return
- welcome_message = await get_welcome(chat_id)
- await message.reply_text(welcome_message)
|