greetings.py 6.7 KB

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