greetings.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. Thanks to @dashezup for this module
  3. Repo - https://github.com/dashezup/tgbot
  4. A Pyrogram Smart Plugin to verify if new members are human
  5. # the bot must be admin in the chat
  6. # should not be lesser than 30 or the user may be banned if he didn't
  7. # press the verification button
  8. """
  9. import asyncio
  10. from datetime import datetime
  11. from pyrogram import filters
  12. from wbb import app, WELCOME_DELAY_KICK_SEC
  13. from wbb.utils.errors import capture_err
  14. from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
  15. from pyrogram.types import ChatPermissions
  16. from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant, ChatAdminRequired
  17. @app.on_message(filters.new_chat_members)
  18. @capture_err
  19. async def welcome(_, message: Message):
  20. """Mute new member and send message with button"""
  21. new_members = [f"{u.mention}" for u in message.new_chat_members]
  22. text = (f"Welcome, {', '.join(new_members)}\n**Are you human?**\n"
  23. "You will be removed from this chat if you are not verified "
  24. f"in {WELCOME_DELAY_KICK_SEC} seconds")
  25. await message.chat.restrict_member(message.from_user.id, ChatPermissions())
  26. button_message = await message.reply(
  27. text,
  28. reply_markup=InlineKeyboardMarkup(
  29. [
  30. [
  31. InlineKeyboardButton(
  32. "Press Here to Verify",
  33. callback_data="pressed_button"
  34. )
  35. ]
  36. ]
  37. ),
  38. quote=True
  39. )
  40. await message.reply_animation("CgACAgQAAx0CWIlO9AABATjjYBrlKqQID0SQ7Ey7bkVRKM5gNn8AAl8CAAIm39VRHDx6EoKU6W0eBA")
  41. await kick_restricted_after_delay(WELCOME_DELAY_KICK_SEC, button_message)
  42. @app.on_callback_query(filters.regex("pressed_button"))
  43. async def callback_query_welcome_button(_, callback_query):
  44. """After the new member presses the button, set his permissions to
  45. chat permissions, delete button message and join message
  46. """
  47. button_message = callback_query.message
  48. join_message = button_message.reply_to_message
  49. pending_user = join_message.from_user
  50. pending_user_id = pending_user.id
  51. pressed_user_id = callback_query.from_user.id
  52. if pending_user_id == pressed_user_id:
  53. await callback_query.answer("Congrats, captcha passed!")
  54. await button_message.chat.unban_member(pending_user_id)
  55. await button_message.delete()
  56. else:
  57. await callback_query.answer("This is not for you")
  58. async def kick_restricted_after_delay(delay, button_message: Message):
  59. """If the new member is still restricted after the delay, delete
  60. button message and join message and then kick him
  61. """
  62. await asyncio.sleep(delay)
  63. join_message = button_message.reply_to_message
  64. group_chat = button_message.chat
  65. user_id = join_message.from_user.id
  66. await join_message.delete()
  67. await button_message.delete()
  68. await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
  69. @app.on_message(filters.left_chat_member)
  70. @capture_err
  71. async def left_chat_member(_, message: Message):
  72. """When a restricted member left the chat, ban him for a while"""
  73. group_chat = message.chat
  74. user_id = message.left_chat_member.id
  75. await _ban_restricted_user_until_date(group_chat, user_id,
  76. duration=WELCOME_DELAY_KICK_SEC)
  77. async def _ban_restricted_user_until_date(group_chat,
  78. user_id: int,
  79. duration: int):
  80. try:
  81. member = await group_chat.get_member(user_id)
  82. if member.status == "restricted":
  83. until_date = int(datetime.utcnow().timestamp() + duration)
  84. await group_chat.kick_member(user_id, until_date=until_date)
  85. except UserNotParticipant:
  86. pass