greetings.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, ChatPermissions, User
  16. from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant
  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. for member in message.new_chat_members:
  22. if member.is_bot:
  23. continue # ignore bots
  24. text = (f"Welcome, {(member.mention())}\n**Are you human?**\n"
  25. "You will be removed from this chat if you are not verified "
  26. f"in {WELCOME_DELAY_KICK_SEC} seconds")
  27. await message.chat.restrict_member(member.id, ChatPermissions())
  28. button_message = await message.reply(
  29. text,
  30. reply_markup=InlineKeyboardMarkup(
  31. [
  32. [
  33. InlineKeyboardButton(
  34. "Press Here to Verify",
  35. callback_data="pressed_button {}".format(member.id) # According to the docs, the user can send arbitrary data in this field, yet, I'm not able to do so, so until there's a db to pass data, imma use it like this so it will be very easy to change it to use a db in the future
  36. )
  37. ]
  38. ]
  39. ),
  40. quote=True
  41. )
  42. #await message.reply_animation("CgACAgQAAx0CWIlO9AABATjjYBrlKqQID0SQ7Ey7bkVRKM5gNn8AAl8CAAIm39VRHDx6EoKU6W0eBA")
  43. # wtf is this? "file_id is unique for each individual bot and can't be transferred from one bot to another." source: https://core.telegram.org/bots/api#sending-files
  44. #and here is the error I just received -- pyrogram.errors.exceptions.bad_request_400.MediaEmpty: [400 MEDIA_EMPTY]: The media you tried to send is invalid (caused by "messages.SendMedia")
  45. asyncio.create_task(kick_restricted_after_delay(WELCOME_DELAY_KICK_SEC, button_message, member))
  46. await asyncio.sleep(0.5)
  47. @app.on_callback_query(filters.regex("pressed_button"))
  48. async def callback_query_welcome_button(client, callback_query):
  49. """After the new member presses the button, set his permissions to
  50. chat permissions, delete button message and join message
  51. """
  52. button_message = callback_query.message
  53. pending_user = await client.get_users(int(callback_query.data.replace('pressed_button ', '')))
  54. pressed_user_id = callback_query.from_user.id
  55. pending_user_id = pending_user.id
  56. if pending_user_id == pressed_user_id:
  57. await callback_query.answer("Congrats, captcha passed!")
  58. await button_message.chat.unban_member(pending_user_id)
  59. await button_message.delete()
  60. else:
  61. await callback_query.answer("This is not for you")
  62. # This code is really dumb
  63. # If anyone is interested, we should use threads and kill them when the user has verified himself.
  64. # Also, once there's a db, we should save this data and re-create the threads in case the bot crashed, tho pyrogram doesn't support history messages (obviously)
  65. # so in such a case we may lose data anyway
  66. async def kick_restricted_after_delay(delay, button_message: Message, user: User):
  67. """If the new member is still restricted after the delay, delete
  68. button message and join message and then kick him
  69. """
  70. await asyncio.sleep(delay)
  71. join_message = button_message.reply_to_message
  72. group_chat = button_message.chat
  73. user_id = user.id
  74. await join_message.delete()
  75. await button_message.delete()
  76. await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
  77. @app.on_message(filters.left_chat_member)
  78. @capture_err
  79. async def left_chat_member(_, message: Message):
  80. """When a restricted member left the chat, ban him for a while"""
  81. group_chat = message.chat
  82. user_id = message.left_chat_member.id
  83. await _ban_restricted_user_until_date(group_chat, user_id,
  84. duration=WELCOME_DELAY_KICK_SEC)
  85. async def _ban_restricted_user_until_date(group_chat,
  86. user_id: int,
  87. duration: int):
  88. try:
  89. member = await group_chat.get_member(user_id)
  90. if member.status == "restricted":
  91. until_date = int(datetime.utcnow().timestamp() + duration)
  92. await group_chat.kick_member(user_id, until_date=until_date)
  93. except UserNotParticipant:
  94. pass