locks.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 app
  24. from wbb.core.decorators.errors import capture_err
  25. from wbb.core.decorators.permissions import adminsOnly
  26. from wbb.modules.admin import current_chat_permissions
  27. __MODULE__ = "Locks"
  28. __HELP__ = """
  29. Commands: /lock | /unlock | /locks [No Parameters Required]
  30. Parameters:
  31. messages | stickers | gifs | media | games | polls
  32. inline | link_previews | group_info | user_add | pin
  33. You can only pass the "all" parameter with /lock, not with /unlock
  34. Example:
  35. /lock all
  36. """
  37. incorrect_parameters = "Incorrect Parameters, Check Locks Section In Help."
  38. data = {
  39. "messages": "can_send_messages",
  40. "stickers": "can_send_stickers",
  41. "gifs": "can_send_animations",
  42. "media": "can_send_media_messages",
  43. "games": "can_send_games",
  44. "inline": "can_use_inline_bots",
  45. "link_previews": "can_add_web_page_previews",
  46. "polls": "can_send_polls",
  47. "group_info": "can_change_info",
  48. "useradd": "can_invite_users",
  49. "pin": "can_pin_messages",
  50. }
  51. async def tg_lock(message, permissions: list, perm: str, lock: bool):
  52. if lock:
  53. if perm not in permissions:
  54. return await message.reply_text("Already locked.")
  55. else:
  56. if perm in permissions:
  57. return await message.reply_text("Already Unlocked.")
  58. (permissions.remove(perm) if lock else permissions.append(perm))
  59. permissions = {perm: True for perm in list(set(permissions))}
  60. try:
  61. await app.set_chat_permissions(
  62. message.chat.id, ChatPermissions(**permissions)
  63. )
  64. except ChatNotModified:
  65. return await message.reply_text(
  66. "To unlock this, you have to unlock 'messages' first."
  67. )
  68. await message.reply_text(("Locked." if lock else "Unlocked."))
  69. @app.on_message(filters.command(["lock", "unlock"]) & ~filters.private)
  70. @adminsOnly("can_restrict_members")
  71. async def locks_func(_, message):
  72. if len(message.command) != 2:
  73. return await message.reply_text(incorrect_parameters)
  74. chat_id = message.chat.id
  75. parameter = message.text.strip().split(None, 1)[1].lower()
  76. state = message.command[0].lower()
  77. if parameter not in data and parameter != "all":
  78. return await message.reply_text(incorrect_parameters)
  79. permissions = await current_chat_permissions(chat_id)
  80. if parameter in data:
  81. return await tg_lock(
  82. message,
  83. permissions,
  84. data[parameter],
  85. True if state == "lock" else False,
  86. )
  87. elif parameter == "all" and state == "lock":
  88. await app.set_chat_permissions(chat_id, ChatPermissions())
  89. await message.reply_text("Locked Everything.")
  90. @app.on_message(filters.command("locks") & ~filters.private)
  91. @capture_err
  92. async def locktypes(_, message):
  93. permissions = await current_chat_permissions(message.chat.id)
  94. if not permissions:
  95. return await message.reply_text("No Permissions.")
  96. perms = ""
  97. for i in permissions:
  98. perms += f"__**{i}**__\n"
  99. await message.reply_text(perms)