greetings.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. text = (f"Welcome, {(member.mention())}\n**Are you human?**\n"
  23. "You will be removed from this chat if you are not verified "
  24. f"in {WELCOME_DELAY_KICK_SEC} seconds")
  25. await message.chat.restrict_member(member.id, ChatPermissions())
  26. button_message = await message.reply(
  27. text,
  28. reply_markup=InlineKeyboardMarkup(
  29. [
  30. [
  31. InlineKeyboardButton(
  32. "Press Here to Verify",
  33. 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
  34. )
  35. ]
  36. ]
  37. ),
  38. quote=True
  39. )
  40. #await message.reply_animation("CgACAgQAAx0CWIlO9AABATjjYBrlKqQID0SQ7Ey7bkVRKM5gNn8AAl8CAAIm39VRHDx6EoKU6W0eBA")
  41. # 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
  42. #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")
  43. asyncio.create_task(kick_restricted_after_delay(WELCOME_DELAY_KICK_SEC, button_message, member))
  44. await asyncio.sleep(0.5)
  45. @app.on_callback_query(filters.regex("pressed_button"))
  46. async def callback_query_welcome_button(client, 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. pending_user = await client.get_users(int(callback_query.data.replace('pressed_button ', '')))
  52. pressed_user_id = callback_query.from_user.id
  53. pending_user_id = pending_user.id
  54. if pending_user_id == pressed_user_id:
  55. await callback_query.answer("Congrats, captcha passed!")
  56. await button_message.chat.unban_member(pending_user_id)
  57. await button_message.delete()
  58. else:
  59. await callback_query.answer("This is not for you")
  60. # This code is really dumb
  61. # If anyone is interested, we should use threads and kill them when the user has verified himself.
  62. # 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)
  63. # so in such a case we may lose data anyway
  64. async def kick_restricted_after_delay(delay, button_message: Message, user: User):
  65. """If the new member is still restricted after the delay, delete
  66. button message and join message and then kick him
  67. """
  68. await asyncio.sleep(delay)
  69. join_message = button_message.reply_to_message
  70. group_chat = button_message.chat
  71. user_id = user.id
  72. await join_message.delete()
  73. await button_message.delete()
  74. await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
  75. @app.on_message(filters.left_chat_member)
  76. @capture_err
  77. async def left_chat_member(_, message: Message):
  78. """When a restricted member left the chat, ban him for a while"""
  79. group_chat = message.chat
  80. user_id = message.left_chat_member.id
  81. await _ban_restricted_user_until_date(group_chat, user_id,
  82. duration=WELCOME_DELAY_KICK_SEC)
  83. async def _ban_restricted_user_until_date(group_chat,
  84. user_id: int,
  85. duration: int):
  86. try:
  87. member = await group_chat.get_member(user_id)
  88. if member.status == "restricted":
  89. until_date = int(datetime.utcnow().timestamp() + duration)
  90. await group_chat.kick_member(user_id, until_date=until_date)
  91. except UserNotParticipant:
  92. pass