greetings.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. from pyrogram.errors.exceptions.forbidden_403 import
  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. await message.chat.restrict_member(message.from_user.id, ChatPermissions())
  27. button_message = await message.reply(
  28. text,
  29. reply_markup=InlineKeyboardMarkup(
  30. [
  31. [
  32. InlineKeyboardButton(
  33. "Press Here to Verify",
  34. callback_data="pressed_button"
  35. )
  36. ]
  37. ]
  38. ),
  39. quote=True
  40. )
  41. await message.reply_animation("CgACAgQAAx0CWIlO9AABATjjYBrlKqQID0SQ7Ey7bkVRKM5gNn8AAl8CAAIm39VRHDx6EoKU6W0eBA")
  42. await kick_restricted_after_delay(WELCOME_DELAY_KICK_SEC, button_message)
  43. @app.on_callback_query(filters.regex("pressed_button"))
  44. async def callback_query_welcome_button(_, callback_query):
  45. """After the new member presses the button, set his permissions to
  46. chat permissions, delete button message and join message
  47. """
  48. button_message = callback_query.message
  49. join_message = button_message.reply_to_message
  50. pending_user = join_message.from_user
  51. pending_user_id = pending_user.id
  52. pressed_user_id = callback_query.from_user.id
  53. if pending_user_id == pressed_user_id:
  54. await callback_query.answer("Congrats, captcha passed!")
  55. await button_message.chat.unban_member(pending_user_id)
  56. await button_message.delete()
  57. else:
  58. await callback_query.answer("This is not for you")
  59. async def kick_restricted_after_delay(delay, button_message: Message):
  60. """If the new member is still restricted after the delay, delete
  61. button message and join message and then kick him
  62. """
  63. await asyncio.sleep(delay)
  64. join_message = button_message.reply_to_message
  65. group_chat = button_message.chat
  66. user_id = join_message.from_user.id
  67. await join_message.delete()
  68. await button_message.delete()
  69. await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
  70. @app.on_message(filters.left_chat_member)
  71. @capture_err
  72. async def left_chat_member(_, message: Message):
  73. """When a restricted member left the chat, ban him for a while"""
  74. group_chat = message.chat
  75. user_id = message.left_chat_member.id
  76. await _ban_restricted_user_until_date(group_chat, user_id,
  77. duration=WELCOME_DELAY_KICK_SEC)
  78. async def _ban_restricted_user_until_date(group_chat,
  79. user_id: int,
  80. duration: int):
  81. try:
  82. member = await group_chat.get_member(user_id)
  83. if member.status == "restricted":
  84. until_date = int(datetime.utcnow().timestamp() + duration)
  85. await group_chat.kick_member(user_id, until_date=until_date)
  86. except UserNotParticipant:
  87. pass