greetings.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 wbb.modules.admin import list_admins
  15. from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
  16. from pyrogram.types import ChatPermissions
  17. from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant
  18. @app.on_message(filters.new_chat_members)
  19. @capture_err
  20. async def welcome(_, message: Message):
  21. """Mute new member and send message with button"""
  22. new_members = [f"{u.mention}" for u in message.new_chat_members]
  23. text = (f"Welcome, {', '.join(new_members)}\n**Are you human?**\n"
  24. "You will be removed from this chat if you are not verified "
  25. f"in {WELCOME_DELAY_KICK_SEC} seconds")
  26. if message.from_user.id in await list_admins(message.chat.id):
  27. return
  28. await message.chat.restrict_member(message.from_user.id, ChatPermissions())
  29. button_message = await message.reply(
  30. text,
  31. reply_markup=InlineKeyboardMarkup(
  32. [
  33. [
  34. InlineKeyboardButton(
  35. "Press Here to Verify",
  36. callback_data="pressed_button"
  37. )
  38. ]
  39. ]
  40. ),
  41. quote=True
  42. )
  43. await message.reply_animation("CgACAgQAAx0CWIlO9AABATjjYBrlKqQID0SQ7Ey7bkVRKM5gNn8AAl8CAAIm39VRHDx6EoKU6W0eBA")
  44. await kick_restricted_after_delay(WELCOME_DELAY_KICK_SEC, button_message)
  45. @app.on_callback_query(filters.regex("pressed_button"))
  46. async def callback_query_welcome_button(_, callback_query):
  47. """After the new member presses the button, set his permissions to
  48. chat permissions, delete button message and join message
  49. """
  50. button_message = callback_query.message
  51. join_message = button_message.reply_to_message
  52. pending_user = join_message.from_user
  53. pending_user_id = pending_user.id
  54. pressed_user_id = callback_query.from_user.id
  55. if pending_user_id == pressed_user_id:
  56. await callback_query.answer("Congrats, captcha passed!")
  57. await button_message.chat.unban_member(pending_user_id)
  58. await button_message.delete()
  59. else:
  60. await callback_query.answer("This is not for you")
  61. async def kick_restricted_after_delay(delay, button_message: Message):
  62. """If the new member is still restricted after the delay, delete
  63. button message and join message and then kick him
  64. """
  65. await asyncio.sleep(delay)
  66. join_message = button_message.reply_to_message
  67. group_chat = button_message.chat
  68. user_id = join_message.from_user.id
  69. await join_message.delete()
  70. await button_message.delete()
  71. await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
  72. @app.on_message(filters.left_chat_member)
  73. @capture_err
  74. async def left_chat_member(_, message: Message):
  75. """When a restricted member left the chat, ban him for a while"""
  76. group_chat = message.chat
  77. user_id = message.left_chat_member.id
  78. await _ban_restricted_user_until_date(group_chat, user_id,
  79. duration=WELCOME_DELAY_KICK_SEC)
  80. async def _ban_restricted_user_until_date(group_chat,
  81. user_id: int,
  82. duration: int):
  83. try:
  84. member = await group_chat.get_member(user_id)
  85. if member.status == "restricted":
  86. until_date = int(datetime.utcnow().timestamp() + duration)
  87. await group_chat.kick_member(user_id, until_date=until_date)
  88. except UserNotParticipant:
  89. pass