greetings.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. user = await app.get_users(member.id)
  29. name_length = len(str(user.first_name) + str(user.last_name))
  30. if name_lenth > 50:
  31. await message.reply_text("[ERROR: MAX_NAME_LENGH_EXCEEDED]: Your Name Is Too Long And Weird, Change It!")
  32. button_message = await message.reply(
  33. text,
  34. reply_markup=InlineKeyboardMarkup(
  35. [
  36. [
  37. InlineKeyboardButton(
  38. "Press Here to Verify",
  39. 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
  40. )
  41. ]
  42. ]
  43. ),
  44. quote=True
  45. )
  46. #await message.reply_animation("CgACAgQAAx0CWIlO9AABATjjYBrlKqQID0SQ7Ey7bkVRKM5gNn8AAl8CAAIm39VRHDx6EoKU6W0eBA")
  47. # 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
  48. #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")
  49. asyncio.create_task(kick_restricted_after_delay(WELCOME_DELAY_KICK_SEC, button_message, member))
  50. await asyncio.sleep(0.5)
  51. @app.on_callback_query(filters.regex("pressed_button"))
  52. async def callback_query_welcome_button(client, callback_query):
  53. """After the new member presses the button, set his permissions to
  54. chat permissions, delete button message and join message
  55. """
  56. button_message = callback_query.message
  57. pending_user = await client.get_users(int(callback_query.data.replace('pressed_button ', '')))
  58. pressed_user_id = callback_query.from_user.id
  59. pending_user_id = pending_user.id
  60. if pending_user_id == pressed_user_id:
  61. await callback_query.answer("Congrats, captcha passed!")
  62. await button_message.chat.unban_member(pending_user_id)
  63. await button_message.delete()
  64. else:
  65. await callback_query.answer("This is not for you")
  66. # This code is really dumb
  67. # If anyone is interested, we should use threads and kill them when the user has verified himself.
  68. # 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)
  69. # so in such a case we may lose data anyway
  70. async def kick_restricted_after_delay(delay, button_message: Message, user: User):
  71. """If the new member is still restricted after the delay, delete
  72. button message and join message and then kick him
  73. """
  74. await asyncio.sleep(delay)
  75. join_message = button_message.reply_to_message
  76. group_chat = button_message.chat
  77. user_id = user.id
  78. await join_message.delete()
  79. await button_message.delete()
  80. await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
  81. @app.on_message(filters.left_chat_member)
  82. @capture_err
  83. async def left_chat_member(_, message: Message):
  84. """When a restricted member left the chat, ban him for a while"""
  85. group_chat = message.chat
  86. user_id = message.left_chat_member.id
  87. await _ban_restricted_user_until_date(group_chat, user_id,
  88. duration=WELCOME_DELAY_KICK_SEC)
  89. async def _ban_restricted_user_until_date(group_chat,
  90. user_id: int,
  91. duration: int):
  92. try:
  93. member = await group_chat.get_member(user_id)
  94. if member.status == "restricted":
  95. until_date = int(datetime.utcnow().timestamp() + duration)
  96. await group_chat.kick_member(user_id, until_date=until_date)
  97. except UserNotParticipant:
  98. pass