autoapprove.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from pyrogram import filters
  2. from pyrogram.types import (
  3. CallbackQuery,
  4. Chat,
  5. ChatJoinRequest,
  6. InlineKeyboardButton,
  7. InlineKeyboardMarkup,
  8. Message,
  9. )
  10. from wbb import SUDOERS, app, db
  11. from wbb.core.decorators.permissions import adminsOnly
  12. from wbb.modules.admin import member_permissions
  13. from wbb.modules.greetings import handle_new_member
  14. approvaldb = db.autoapprove
  15. # For /help menu
  16. __MODULE__ = "Autoapprove"
  17. __HELP__ = """
  18. command: /autoapprove
  19. This module helps to automatically accept chat join request send by a user through invitation link of your group"""
  20. @app.on_message(filters.command("autoapprove") & filters.group)
  21. @adminsOnly("can_change_info")
  22. async def approval_command(client, message):
  23. chat_id = message.chat.id
  24. if (await approvaldb.count_documents({"chat_id": chat_id})) > 0:
  25. keyboard_OFF = InlineKeyboardMarkup(
  26. [[InlineKeyboardButton("Turn OFF", callback_data="approval_off")]]
  27. )
  28. await message.reply(
  29. "**Autoapproval for this chat: Enabled.**",
  30. reply_markup=keyboard_OFF,
  31. )
  32. else:
  33. keyboard_ON = InlineKeyboardMarkup(
  34. [[InlineKeyboardButton("Turn ON", callback_data="approval_on")]]
  35. )
  36. await message.reply(
  37. "**Autoapproval for this chat: Disabled.**",
  38. reply_markup=keyboard_ON,
  39. )
  40. @app.on_callback_query(filters.regex("approval(.*)"))
  41. async def approval_cb(client, cb):
  42. chat_id = cb.message.chat.id
  43. from_user = cb.from_user
  44. permissions = await member_permissions(chat_id, from_user.id)
  45. permission = "can_restrict_members"
  46. if permission not in permissions:
  47. if from_user.id not in SUDOERS:
  48. return await cb.answer(
  49. f"You don't have the required permission.\n Permission: {permission}",
  50. show_alert=True,
  51. )
  52. command_parts = cb.data.split("_", 1)
  53. option = command_parts[1]
  54. if option == "on":
  55. if await approvaldb.count_documents({"chat_id": chat_id}) == 0:
  56. approvaldb.insert_one({"chat_id": chat_id})
  57. keyboard_off = InlineKeyboardMarkup(
  58. [
  59. [
  60. InlineKeyboardButton(
  61. "Turn OFF", callback_data="approval_off"
  62. )
  63. ]
  64. ]
  65. )
  66. await cb.edit_message_text(
  67. "**Autoapproval for this chat: Enabled.**",
  68. reply_markup=keyboard_off,
  69. )
  70. elif option == "off":
  71. if await approvaldb.count_documents({"chat_id": chat_id}) > 0:
  72. approvaldb.delete_one({"chat_id": chat_id})
  73. keyboard_on = InlineKeyboardMarkup(
  74. [
  75. [
  76. InlineKeyboardButton(
  77. "Turn ON", callback_data="approval_on"
  78. )
  79. ]
  80. ]
  81. )
  82. await cb.edit_message_text(
  83. "**Autoapproval for this chat: Disabled.**",
  84. reply_markup=keyboard_on,
  85. )
  86. return await cb.answer()
  87. @app.on_chat_join_request(filters.group)
  88. async def accept(client, message: ChatJoinRequest):
  89. chat = message.chat
  90. user = message.from_user
  91. if (await approvaldb.count_documents({"chat_id": chat.id})) > 0:
  92. await app.approve_chat_join_request(chat_id=chat.id, user_id=user.id)
  93. await handle_new_member(message, user, chat)