greetings.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. """ Kang with proper credits or /gbun """
  2. import asyncio, os
  3. from wbb import app, WELCOME_DELAY_KICK_SEC, SUDOERS
  4. from wbb.modules.admin import member_permissions
  5. from wbb.utils.errors import capture_err
  6. from wbb.utils.filter_groups import welcome_captcha_group
  7. from wbb.utils.functions import generate_captcha
  8. from wbb.utils.dbfunctions import (
  9. is_gbanned_user, is_captcha_on, captcha_on, captcha_off
  10. )
  11. from pyrogram.types import (
  12. Message, InlineKeyboardMarkup,
  13. InlineKeyboardButton, ChatPermissions, User
  14. )
  15. from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant, ChatAdminRequired
  16. from pyrogram import filters
  17. from random import shuffle
  18. from datetime import datetime
  19. __MODULE__ = "Captcha"
  20. __HELP__ = "/captcha [ON|OFF] - Enable/Disable Captcha."
  21. answers_dicc = []
  22. @app.on_message(filters.new_chat_members, group=welcome_captcha_group)
  23. @capture_err
  24. async def welcome(_, message: Message):
  25. global answers_dicc
  26. """Mute new member and send message with button"""
  27. if not await is_captcha_on(message.chat.id):
  28. return
  29. for member in message.new_chat_members:
  30. try:
  31. if member.id in SUDOERS:
  32. continue # ignore sudos
  33. if await is_gbanned_user(member.id):
  34. await message.chat.kick_member(member.id)
  35. continue
  36. if member.is_bot:
  37. continue # ignore bots
  38. await message.chat.restrict_member(member.id, ChatPermissions())
  39. text = (f"Welcome, {(member.mention())} Are you human?\n"
  40. f"Solve this captcha in {WELCOME_DELAY_KICK_SEC} seconds or you'll be kicked.")
  41. except ChatAdminRequired:
  42. break
  43. captcha = generate_captcha() # Generate a captcha image, answers and some wrong answers
  44. captcha_image = captcha[0]
  45. captcha_answer = captcha[1]
  46. wrong_answers = captcha[2] # This consists of 7 wrong answers
  47. correct_button = InlineKeyboardButton(
  48. f"{captcha_answer}",
  49. callback_data=f"pressed_button {captcha_answer} {member.id}"
  50. )
  51. temp_keyboard_1 = [correct_button] # Button row 1
  52. temp_keyboard_2 = [] # Botton row 2
  53. for i in range(3):
  54. temp_keyboard_1.append(
  55. InlineKeyboardButton(
  56. f"{wrong_answers[i]}",
  57. callback_data=f"pressed_button {wrong_answers[i]} {member.id}"
  58. )
  59. )
  60. for i in range(3, 7):
  61. temp_keyboard_2.append(
  62. InlineKeyboardButton(
  63. f"{wrong_answers[i]}",
  64. callback_data=f"pressed_button {wrong_answers[i]} {member.id}"
  65. )
  66. )
  67. shuffle(temp_keyboard_1)
  68. keyboard = [temp_keyboard_1, temp_keyboard_2]
  69. shuffle(keyboard)
  70. verification_data = {
  71. "user_id": member.id,
  72. "answer": captcha_answer,
  73. "keyboard": keyboard
  74. }
  75. keyboard = InlineKeyboardMarkup(keyboard)
  76. answers_dicc.append(verification_data) # Append user info, correct answer and
  77. # keyboard for later use with callback query
  78. button_message = await message.reply_photo(
  79. photo=captcha_image,
  80. caption=text,
  81. reply_markup=keyboard,
  82. quote=True
  83. )
  84. os.remove(captcha_image)
  85. asyncio.create_task(kick_restricted_after_delay(
  86. WELCOME_DELAY_KICK_SEC, button_message, member))
  87. await asyncio.sleep(0.5)
  88. @app.on_callback_query(filters.regex("pressed_button"))
  89. async def callback_query_welcome_button(_, callback_query):
  90. """After the new member presses the correct button,
  91. set his permissions to chat permissions,
  92. delete button message and join message.
  93. """
  94. global answers_dicc
  95. data = callback_query.data
  96. pending_user = await app.get_users(int(data.split(None, 2)[2]))
  97. pressed_user_id = callback_query.from_user.id
  98. pending_user_id = pending_user.id
  99. button_message = callback_query.message
  100. answer = data.split(None, 2)[1]
  101. if len(answers_dicc) != 0:
  102. for i in answers_dicc:
  103. if i['user_id'] == pending_user_id:
  104. correct_answer = i['answer']
  105. keyboard = i['keyboard']
  106. if pending_user_id == pressed_user_id:
  107. if answer != correct_answer:
  108. await callback_query.answer("Yeah, It's Wrong.")
  109. shuffle(keyboard[0])
  110. shuffle(keyboard[1])
  111. shuffle(keyboard)
  112. keyboard = InlineKeyboardMarkup(keyboard)
  113. await button_message.edit(
  114. text=button_message.caption.markdown,
  115. reply_markup=keyboard
  116. )
  117. return
  118. await callback_query.answer("Captcha passed successfully!")
  119. await button_message.chat.unban_member(pending_user_id)
  120. await button_message.delete()
  121. if len(answers_dicc) != 0:
  122. for ii in answers_dicc:
  123. if ii['user_id'] == pending_user_id:
  124. answers_dicc.remove(ii)
  125. return
  126. else:
  127. await callback_query.answer("This is not for you")
  128. return
  129. async def kick_restricted_after_delay(delay, button_message: Message, user: User):
  130. """If the new member is still restricted after the delay, delete
  131. button message and join message and then kick him
  132. """
  133. global answers_dicc
  134. await asyncio.sleep(delay)
  135. join_message = button_message.reply_to_message
  136. group_chat = button_message.chat
  137. user_id = user.id
  138. await join_message.delete()
  139. await button_message.delete()
  140. if len(answers_dicc) != 0:
  141. for i in answers_dicc:
  142. if i['user_id'] == user_id:
  143. answers_dicc.remove(i)
  144. await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
  145. async def _ban_restricted_user_until_date(group_chat,
  146. user_id: int,
  147. duration: int):
  148. try:
  149. member = await group_chat.get_member(user_id)
  150. if member.status == "restricted":
  151. until_date = int(datetime.utcnow().timestamp() + duration)
  152. await group_chat.kick_member(user_id, until_date=until_date)
  153. except UserNotParticipant:
  154. pass
  155. @app.on_message(filters.command("captcha") & ~filters.private)
  156. @capture_err
  157. async def captcha_state(_, message):
  158. usage = "**Usage:**\n/captcha [ON|OFF]"
  159. if len(message.command) != 2:
  160. await message.reply_text(usage)
  161. return
  162. user_id = message.from_user.id
  163. chat_id = message.chat.id
  164. permissions = await member_permissions(chat_id, user_id)
  165. if "can_restrict_members" not in permissions:
  166. await message.reply_text("You don't have enough permissions.")
  167. return
  168. state = message.text.split(None, 1)[1].strip()
  169. state = state.lower()
  170. if state == "on":
  171. await captcha_on(chat_id)
  172. await message.reply_text("Enabled Captcha For New Users.")
  173. elif state == "off":
  174. await captcha_off(chat_id)
  175. await message.reply_text("Disabled Captcha For New Users.")
  176. else:
  177. await message.reply_text(usage)