greetings.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. """
  2. Thanks to @dashezup for this idea
  3. Repo - https://github.com/dashezup/tgbot
  4. """
  5. import asyncio
  6. from wbb import app, WELCOME_DELAY_KICK_SEC
  7. from wbb.modules.admin import member_permissions
  8. from wbb.utils.errors import capture_err
  9. from wbb.utils.filter_groups import welcome_captcha_group
  10. from wbb.utils.dbfunctions import (
  11. is_gbanned_user, is_captcha_on, captcha_on, captcha_off
  12. )
  13. from pyrogram.types import (
  14. Message, InlineKeyboardMarkup,
  15. InlineKeyboardButton, ChatPermissions, User
  16. )
  17. from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant, ChatAdminRequired
  18. from pyrogram import emoji
  19. from pyrogram import filters
  20. from random import randint, shuffle
  21. from datetime import datetime
  22. __MODULE__ = "Captcha"
  23. __HELP__ = "/captcha [ON|OFF] - Enable/Disable Captcha."
  24. @app.on_message(filters.new_chat_members, group=welcome_captcha_group)
  25. @capture_err
  26. async def welcome(_, message: Message):
  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 await is_gbanned_user(member.id):
  33. await message.chat.kick_member(member.id)
  34. continue
  35. if member.is_bot:
  36. continue # ignore bots
  37. text = (f"Welcome, {(member.mention())}\nAre you human?\n"
  38. f"Click on the button which include this emoji {emoji.CHECK_MARK_BUTTON}.'")
  39. await message.chat.restrict_member(member.id, ChatPermissions())
  40. except ChatAdminRequired:
  41. continue
  42. keyboard = [
  43. InlineKeyboardButton(
  44. f"{emoji.BRAIN}",
  45. callback_data=f"pressed_button {emoji.BRAIN} {member.id}"
  46. ),
  47. InlineKeyboardButton(
  48. f"{emoji.CHECK_MARK_BUTTON}",
  49. callback_data=f"pressed_button {emoji.CHECK_MARK_BUTTON} {member.id}"
  50. ),
  51. InlineKeyboardButton(
  52. f"{emoji.CROSS_MARK}",
  53. callback_data=f"pressed_button {emoji.CROSS_MARK} {member.id}"
  54. ),
  55. InlineKeyboardButton(
  56. f"{emoji.ROBOT}",
  57. callback_data=f"pressed_button {emoji.ROBOT} {member.id}"
  58. )
  59. ]
  60. shuffle(keyboard)
  61. keyboard = InlineKeyboardMarkup([keyboard])
  62. button_message = await message.reply(
  63. text,
  64. reply_markup=keyboard,
  65. quote=True
  66. )
  67. asyncio.create_task(kick_restricted_after_delay(
  68. WELCOME_DELAY_KICK_SEC, button_message, member))
  69. await asyncio.sleep(0.5)
  70. @app.on_callback_query(filters.regex("pressed_button"))
  71. async def callback_query_welcome_button(client, callback_query):
  72. """After the new member presses the button, set his permissions to
  73. chat permissions, delete button message and join message
  74. """
  75. button_message = callback_query.message
  76. data = callback_query.data
  77. answer = data.split(None, 2)[1]
  78. pending_user = await app.get_users(int(data.split(None, 2)[2]))
  79. pressed_user_id = callback_query.from_user.id
  80. pending_user_id = pending_user.id
  81. if answer != emoji.CHECK_MARK_BUTTON:
  82. await callback_query.answer("Yeah, It's Wrong.")
  83. keyboard = [
  84. InlineKeyboardButton(
  85. f"{emoji.BRAIN}",
  86. callback_data=f"pressed_button {emoji.BRAIN} {pending_user_id}"
  87. ),
  88. InlineKeyboardButton(
  89. f"{emoji.CHECK_MARK_BUTTON}",
  90. callback_data=f"pressed_button {emoji.CHECK_MARK_BUTTON} {pending_user_id}"
  91. ),
  92. InlineKeyboardButton(
  93. f"{emoji.CROSS_MARK}",
  94. callback_data=f"pressed_button {emoji.CROSS_MARK} {pending_user_id}"
  95. ),
  96. InlineKeyboardButton(
  97. f"{emoji.ROBOT}",
  98. callback_data=f"pressed_button {emoji.ROBOT} {pending_user_id}"
  99. )
  100. ]
  101. shuffle(keyboard)
  102. keyboard = InlineKeyboardMarkup([keyboard])
  103. await button_message.edit(
  104. text=button_message.text,
  105. reply_markup=keyboard
  106. )
  107. return
  108. if pending_user_id == pressed_user_id:
  109. await callback_query.answer("Captcha passed successfully!")
  110. await button_message.chat.unban_member(pending_user_id)
  111. await button_message.delete()
  112. else:
  113. await callback_query.answer("This is not for you")
  114. async def kick_restricted_after_delay(delay, button_message: Message, user: User):
  115. """If the new member is still restricted after the delay, delete
  116. button message and join message and then kick him
  117. """
  118. await asyncio.sleep(delay)
  119. join_message = button_message.reply_to_message
  120. group_chat = button_message.chat
  121. user_id = user.id
  122. await join_message.delete()
  123. await button_message.delete()
  124. await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
  125. @app.on_message(filters.left_chat_member)
  126. @capture_err
  127. async def left_chat_member(_, message: Message):
  128. """When a restricted member left the chat, ban him for a while"""
  129. group_chat = message.chat
  130. user_id = message.left_chat_member.id
  131. await _ban_restricted_user_until_date(group_chat, user_id,
  132. duration=WELCOME_DELAY_KICK_SEC)
  133. async def _ban_restricted_user_until_date(group_chat,
  134. user_id: int,
  135. duration: int):
  136. try:
  137. member = await group_chat.get_member(user_id)
  138. if member.status == "restricted":
  139. until_date = int(datetime.utcnow().timestamp() + duration)
  140. await group_chat.kick_member(user_id, until_date=until_date)
  141. except UserNotParticipant:
  142. pass
  143. @app.on_message(filters.command("captcha") & ~filters.private)
  144. @capture_err
  145. async def captcha_state(_, message):
  146. usage = "**Usage:**\n/captcha [ON|OFF]"
  147. if len(message.command) != 2:
  148. await message.reply_text(usage)
  149. return
  150. user_id = message.from_user.id
  151. chat_id = message.chat.id
  152. permissions = await member_permissions(chat_id, user_id)
  153. if "can_restrict_members" not in permissions:
  154. await message.reply_text("You don't have enough permissions.")
  155. return
  156. state = message.text.split(None, 1)[1].strip()
  157. state = state.lower()
  158. if state == "on":
  159. await captcha_on(chat_id)
  160. await message.reply_text("Enabled Captcha For New Users.")
  161. elif state == "off":
  162. await captcha_off(chat_id)
  163. await message.reply_text("Disabled Captcha For New Users.")
  164. else:
  165. await message.reply_text(usage)