locks.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. """
  2. MIT License
  3. Copyright (c) 2024 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.core.decorators.permissions import adminsOnly
  26. from wbb.modules.admin import list_admins
  27. from wbb.utils.functions import get_urls_from_text
  28. __MODULE__ = "Locks"
  29. __HELP__ = """
  30. Commands: /lock | /unlock | /locks [No Parameters Required]
  31. Parameters:
  32. messages | stickers | gifs | media | games | polls
  33. inline | url | group_info | user_add | pin
  34. You can only pass the "all" parameter with /lock, not with /unlock
  35. Example:
  36. /lock all
  37. """
  38. incorrect_parameters = "Incorrect Parameters, Check Locks Section In Help."
  39. # Using disable_preview as a switch for url checker
  40. # That way we won't need an additional db to check
  41. # If url lock is enabled/disabled for a chat
  42. data = {
  43. "messages": "can_send_messages",
  44. "stickers": "can_send_other_messages",
  45. "gifs": "can_send_other_messages",
  46. "media": "can_send_media_messages",
  47. "games": "can_send_other_messages",
  48. "inline": "can_send_other_messages",
  49. "url": "can_add_web_page_previews",
  50. "polls": "can_send_polls",
  51. "group_info": "can_change_info",
  52. "useradd": "can_invite_users",
  53. "pin": "can_pin_messages",
  54. }
  55. async def current_chat_permissions(chat_id):
  56. perms = []
  57. perm = (await app.get_chat(chat_id)).permissions
  58. if perm.can_send_messages:
  59. perms.append("can_send_messages")
  60. if perm.can_send_media_messages:
  61. perms.append("can_send_media_messages")
  62. if perm.can_send_other_messages:
  63. perms.append("can_send_other_messages")
  64. if perm.can_add_web_page_previews:
  65. perms.append("can_add_web_page_previews")
  66. if perm.can_send_polls:
  67. perms.append("can_send_polls")
  68. if perm.can_change_info:
  69. perms.append("can_change_info")
  70. if perm.can_invite_users:
  71. perms.append("can_invite_users")
  72. if perm.can_pin_messages:
  73. perms.append("can_pin_messages")
  74. return perms
  75. async def tg_lock(message, permissions: list, perm: str, lock: bool):
  76. if lock:
  77. if perm not in permissions:
  78. return await message.reply_text("Already locked.")
  79. permissions.remove(perm)
  80. else:
  81. if perm in permissions:
  82. return await message.reply_text("Already Unlocked.")
  83. permissions.append(perm)
  84. permissions = {perm: True for perm in list(set(permissions))}
  85. try:
  86. await app.set_chat_permissions(
  87. message.chat.id, ChatPermissions(**permissions)
  88. )
  89. except ChatNotModified:
  90. return await message.reply_text(
  91. "To unlock this, you have to unlock 'messages' first."
  92. )
  93. await message.reply_text(("Locked." if lock else "Unlocked."))
  94. @app.on_message(filters.command(["lock", "unlock"]) & ~filters.private)
  95. @adminsOnly("can_restrict_members")
  96. async def locks_func(_, message):
  97. if len(message.command) != 2:
  98. return await message.reply_text(incorrect_parameters)
  99. chat_id = message.chat.id
  100. parameter = message.text.strip().split(None, 1)[1].lower()
  101. state = message.command[0].lower()
  102. if parameter not in data and parameter != "all":
  103. return await message.reply_text(incorrect_parameters)
  104. permissions = await current_chat_permissions(chat_id)
  105. if parameter in data:
  106. await tg_lock(
  107. message,
  108. permissions,
  109. data[parameter],
  110. bool(state == "lock"),
  111. )
  112. elif parameter == "all" and state == "lock":
  113. await app.set_chat_permissions(chat_id, ChatPermissions())
  114. await message.reply_text(f"Locked Everything in {message.chat.title}")
  115. elif parameter == "all" and state == "unlock":
  116. await app.set_chat_permissions(
  117. chat_id,
  118. ChatPermissions(
  119. can_send_messages=True,
  120. can_send_media_messages=True,
  121. can_send_other_messages=True,
  122. can_add_web_page_previews=True,
  123. can_send_polls=True,
  124. can_change_info=False,
  125. can_invite_users=True,
  126. can_pin_messages=False,
  127. ),
  128. )
  129. await message.reply(f"Unlocked Everything in {message.chat.title}")
  130. @app.on_message(filters.command("locks") & ~filters.private)
  131. @capture_err
  132. async def locktypes(_, message):
  133. permissions = await current_chat_permissions(message.chat.id)
  134. if not permissions:
  135. return await message.reply_text("No Permissions.")
  136. perms = ""
  137. for i in permissions:
  138. perms += f"__**{i}**__\n"
  139. await message.reply_text(perms)
  140. @app.on_message(filters.text & ~filters.private, group=69)
  141. async def url_detector(_, message):
  142. user = message.from_user
  143. chat_id = message.chat.id
  144. text = message.text.lower().strip()
  145. if not text or not user:
  146. return
  147. mods = await list_admins(chat_id)
  148. if user.id in mods or user.id in SUDOERS:
  149. return
  150. check = get_urls_from_text(text)
  151. if check:
  152. permissions = await current_chat_permissions(chat_id)
  153. if "can_add_web_page_previews" not in permissions:
  154. try:
  155. await message.delete()
  156. except Exception:
  157. await message.reply_text(
  158. "This message contains a URL, "
  159. + "but i don't have enough permissions to delete it"
  160. )