greetings.py 7.1 KB

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