greetings.py 5.3 KB

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