greetings.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """
  2. Thanks to @dashezup for this module
  3. Repo - https://github.com/dashezup/tgbot
  4. """
  5. import asyncio
  6. from datetime import datetime
  7. from pyrogram import filters
  8. from wbb import app, WELCOME_DELAY_KICK_SEC
  9. from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, ChatPermissions, User
  10. from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant
  11. from wbb.utils.errors import capture_err
  12. from wbb.utils.dbfunctions import is_gbanned_user
  13. @app.on_message(filters.new_chat_members)
  14. @capture_err
  15. async def welcome(_, message: Message):
  16. """Mute new member and send message with button"""
  17. for member in message.new_chat_members:
  18. if member.is_bot:
  19. continue # ignore bots
  20. if await is_gbanned_user(member.id):
  21. await message.chat.kick_member(member.id)
  22. continue
  23. text = (f"Welcome, {(member.mention())}\n**Are you human?**\n"
  24. "You will be removed from this chat if you are not verified "
  25. f"in {WELCOME_DELAY_KICK_SEC} seconds")
  26. await message.chat.restrict_member(member.id, ChatPermissions())
  27. button_message = await message.reply(
  28. text,
  29. reply_markup=InlineKeyboardMarkup(
  30. [
  31. [
  32. InlineKeyboardButton(
  33. "Press Here to Verify",
  34. callback_data="pressed_button {}".format(member.id)
  35. )
  36. ]
  37. ]
  38. ),
  39. quote=True
  40. )
  41. asyncio.create_task(kick_restricted_after_delay(
  42. WELCOME_DELAY_KICK_SEC, button_message, member))
  43. await asyncio.sleep(0.5)
  44. @app.on_callback_query(filters.regex("pressed_button"))
  45. async def callback_query_welcome_button(client, callback_query):
  46. """After the new member presses the button, set his permissions to
  47. chat permissions, delete button message and join message
  48. """
  49. button_message = callback_query.message
  50. pending_user = await app.get_users(int(callback_query.data.replace('pressed_button ', '')))
  51. pressed_user_id = callback_query.from_user.id
  52. pending_user_id = pending_user.id
  53. if pending_user_id == pressed_user_id:
  54. await callback_query.answer("Captcha passed!, Have a nice stay.")
  55. await button_message.chat.unban_member(pending_user_id)
  56. await button_message.delete()
  57. else:
  58. await callback_query.answer("This is not for you")
  59. async def kick_restricted_after_delay(delay, button_message: Message, user: User):
  60. """If the new member is still restricted after the delay, delete
  61. button message and join message and then kick him
  62. """
  63. await asyncio.sleep(delay)
  64. join_message = button_message.reply_to_message
  65. group_chat = button_message.chat
  66. user_id = user.id
  67. await join_message.delete()
  68. await button_message.delete()
  69. await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
  70. @app.on_message(filters.left_chat_member)
  71. @capture_err
  72. async def left_chat_member(_, message: Message):
  73. """When a restricted member left the chat, ban him for a while"""
  74. group_chat = message.chat
  75. user_id = message.left_chat_member.id
  76. await _ban_restricted_user_until_date(group_chat, user_id,
  77. duration=WELCOME_DELAY_KICK_SEC)
  78. async def _ban_restricted_user_until_date(group_chat,
  79. user_id: int,
  80. duration: int):
  81. try:
  82. member = await group_chat.get_member(user_id)
  83. if member.status == "restricted":
  84. until_date = int(datetime.utcnow().timestamp() + duration)
  85. await group_chat.kick_member(user_id, until_date=until_date)
  86. except UserNotParticipant:
  87. pass