greetings.py 3.7 KB

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