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 pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
  14. from pyrogram.types import ChatPermissions
  15. from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant, ChatAdminRequired
  16. @app.on_message(filters.new_chat_members)
  17. async def welcome(_, message: Message):
  18. """Mute new member and send message with button"""
  19. new_members = [f"{u.mention}" for u in message.new_chat_members]
  20. text = (f"Welcome, {', '.join(new_members)}\n**Are you human?**\n"
  21. "You will be removed from this chat if you are not verified "
  22. f"in {WELCOME_DELAY_KICK_SEC} seconds")
  23. try:
  24. await message.chat.restrict_member(message.from_user.id, ChatPermissions())
  25. except ChatAdminRequired:
  26. msg = "Yeah, I Can't Verify Newly Joined Members, Make Sure I'm Admin."
  27. await message.reply_text(msg)
  28. return
  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. async def left_chat_member(_, message: Message):
  74. """When a restricted member left the chat, ban him for a while"""
  75. group_chat = message.chat
  76. user_id = message.left_chat_member.id
  77. await _ban_restricted_user_until_date(group_chat, user_id,
  78. duration=WELCOME_DELAY_KICK_SEC)
  79. async def _ban_restricted_user_until_date(group_chat,
  80. user_id: int,
  81. duration: int):
  82. try:
  83. member = await group_chat.get_member(user_id)
  84. if member.status == "restricted":
  85. until_date = int(datetime.utcnow().timestamp() + duration)
  86. await group_chat.kick_member(user_id, until_date=until_date)
  87. except UserNotParticipant:
  88. pass