locks.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. """
  2. MIT License
  3. Copyright (c) 2021 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from pyrogram import filters
  21. from pyrogram.errors.exceptions.bad_request_400 import ChatNotModified
  22. from pyrogram.types import ChatPermissions
  23. from wbb import SUDOERS, app
  24. from wbb.core.decorators.errors import capture_err
  25. from wbb.modules.admin import current_chat_permissions, member_permissions
  26. __MODULE__ = "Locks"
  27. __HELP__ = """
  28. Commands: /lock | /unlock | /locks [No Parameters Required]
  29. Parameters:
  30. messages | stickers | gifs | media | games | polls
  31. inline | link_previews | group_info | user_add | pin
  32. You can only pass the "all" parameter with /lock, not with /unlock
  33. Example:
  34. /lock all
  35. """
  36. incorrect_parameters = "Incorrect Parameters, Check Locks Section In Help."
  37. data = {
  38. "messages": "can_send_messages",
  39. "stickers": "can_send_stickers",
  40. "gifs": "can_send_animations",
  41. "media": "can_send_media_messages",
  42. "games": "can_send_games",
  43. "inline": "can_use_inline_bots",
  44. "link_previews": "can_add_web_page_previews",
  45. "polls": "can_send_polls",
  46. "group_info": "can_change_info",
  47. "useradd": "can_invite_users",
  48. "pin": "can_pin_messages",
  49. }
  50. async def tg_lock(message, permissions: list, perm: str, lock: bool):
  51. if lock:
  52. if perm not in permissions:
  53. await message.reply_text("Already locked.")
  54. return
  55. else:
  56. if perm in permissions:
  57. await message.reply_text("Already Unlocked.")
  58. return
  59. (permissions.remove(perm) if lock else permissions.append(perm))
  60. permissions = {perm: True for perm in list(set(permissions))}
  61. try:
  62. await app.set_chat_permissions(message.chat.id, ChatPermissions(**permissions))
  63. except ChatNotModified:
  64. await message.reply_text("To unlock this, you have to unlock 'messages' first.")
  65. return
  66. await message.reply_text(("Locked." if lock else "Unlocked."))
  67. @app.on_message(filters.command(["lock", "unlock"]) & ~filters.private)
  68. @capture_err
  69. async def locks_func(_, message):
  70. try:
  71. user_id = message.from_user.id
  72. chat_id = message.chat.id
  73. if len(message.command) != 2:
  74. await message.reply_text(incorrect_parameters)
  75. return
  76. parameter = message.text.strip().split(None, 1)[1].lower()
  77. state = message.command[0].lower()
  78. if parameter not in data and parameter != "all":
  79. await message.reply_text(incorrect_parameters)
  80. return
  81. permissions = await member_permissions(chat_id, user_id)
  82. if "can_restrict_members" not in permissions and user_id not in SUDOERS:
  83. await message.reply_text("You Don't Have Enough Permissions.")
  84. return
  85. permissions = await current_chat_permissions(chat_id)
  86. if parameter in data:
  87. await tg_lock(
  88. message,
  89. permissions,
  90. data[parameter],
  91. True if state == "lock" else False,
  92. )
  93. return
  94. elif parameter == "all" and state == "lock":
  95. await app.set_chat_permissions(chat_id, ChatPermissions())
  96. await message.reply_text("Locked Everything.")
  97. except Exception as e:
  98. await message.reply_text(str(e))
  99. print(e)
  100. @app.on_message(filters.command("locks") & ~filters.private)
  101. async def locktypes(_, message):
  102. permissions = await current_chat_permissions(message.chat.id)
  103. if not permissions:
  104. await message.reply_text("No Permissions.")
  105. return
  106. perms = ""
  107. for i in permissions:
  108. perms += f"__**{i}**__\n"
  109. await message.reply_text(perms)