locks.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. from wbb.utils.i18n import telegram_permission_label
  29. __MODULE__ = "群权限锁"
  30. __HELP__ = """
  31. 命令:/lock、/unlock、/locks
  32. 可用参数:
  33. messages 文字 | stickers 贴纸 | gifs 动图 | media 媒体
  34. games 游戏 | polls 投票 | inline 内联内容 | url 链接预览
  35. group_info 群信息 | useradd 邀请成员 | pin 置顶
  36. `/lock all` 可一次锁定全部权限;`/unlock` 不支持 all 参数。
  37. 示例:`/lock media`
  38. """
  39. incorrect_parameters = "参数不正确,请发送 /help locks 查看群权限锁说明。"
  40. # Using disable_preview as a switch for url checker
  41. # That way we won't need an additional db to check
  42. # If url lock is enabled/disabled for a chat
  43. data = {
  44. "messages": "can_send_messages",
  45. "stickers": "can_send_other_messages",
  46. "gifs": "can_send_other_messages",
  47. "media": "can_send_media_messages",
  48. "games": "can_send_other_messages",
  49. "inline": "can_send_other_messages",
  50. "url": "can_add_web_page_previews",
  51. "polls": "can_send_polls",
  52. "group_info": "can_change_info",
  53. "useradd": "can_invite_users",
  54. "pin": "can_pin_messages",
  55. }
  56. async def current_chat_permissions(chat_id):
  57. perms = []
  58. perm = (await app.get_chat(chat_id)).permissions
  59. if perm.can_send_messages:
  60. perms.append("can_send_messages")
  61. if perm.can_send_media_messages:
  62. perms.append("can_send_media_messages")
  63. if perm.can_send_other_messages:
  64. perms.append("can_send_other_messages")
  65. if perm.can_add_web_page_previews:
  66. perms.append("can_add_web_page_previews")
  67. if perm.can_send_polls:
  68. perms.append("can_send_polls")
  69. if perm.can_change_info:
  70. perms.append("can_change_info")
  71. if perm.can_invite_users:
  72. perms.append("can_invite_users")
  73. if perm.can_pin_messages:
  74. perms.append("can_pin_messages")
  75. return perms
  76. async def tg_lock(message, permissions: list, perm: str, lock: bool):
  77. if lock:
  78. if perm not in permissions:
  79. return await message.reply_text("该权限已经锁定。")
  80. permissions.remove(perm)
  81. else:
  82. if perm in permissions:
  83. return await message.reply_text("该权限已经解锁。")
  84. permissions.append(perm)
  85. permissions = {perm: True for perm in list(set(permissions))}
  86. try:
  87. await app.set_chat_permissions(
  88. message.chat.id, ChatPermissions(**permissions)
  89. )
  90. except ChatNotModified:
  91. return await message.reply_text(
  92. "请先解锁文字消息权限。"
  93. )
  94. await message.reply_text("权限已锁定。" if lock else "权限已解锁。")
  95. @app.on_message(filters.command(["lock", "unlock"]) & ~filters.private)
  96. @adminsOnly("can_restrict_members")
  97. async def locks_func(_, message):
  98. if len(message.command) != 2:
  99. return await message.reply_text(incorrect_parameters)
  100. chat_id = message.chat.id
  101. parameter = message.text.strip().split(None, 1)[1].lower()
  102. state = message.command[0].lower()
  103. if parameter not in data and parameter != "all":
  104. return await message.reply_text(incorrect_parameters)
  105. permissions = await current_chat_permissions(chat_id)
  106. if parameter in data:
  107. await tg_lock(
  108. message,
  109. permissions,
  110. data[parameter],
  111. bool(state == "lock"),
  112. )
  113. elif parameter == "all" and state == "lock":
  114. await app.set_chat_permissions(chat_id, ChatPermissions())
  115. await message.reply_text(f"{message.chat.title} 的全部成员权限已锁定。")
  116. elif parameter == "all" and state == "unlock":
  117. await app.set_chat_permissions(
  118. chat_id,
  119. ChatPermissions(
  120. can_send_messages=True,
  121. can_send_media_messages=True,
  122. can_send_other_messages=True,
  123. can_add_web_page_previews=True,
  124. can_send_polls=True,
  125. can_change_info=False,
  126. can_invite_users=True,
  127. can_pin_messages=False,
  128. ),
  129. )
  130. await message.reply(f"{message.chat.title} 的常用成员权限已解锁。")
  131. @app.on_message(filters.command("locks") & ~filters.private)
  132. @capture_err
  133. async def locktypes(_, message):
  134. permissions = await current_chat_permissions(message.chat.id)
  135. if not permissions:
  136. return await message.reply_text("普通成员当前没有可用权限。")
  137. perms = ""
  138. for i in permissions:
  139. perms += f"__**{telegram_permission_label(i)}**__\n"
  140. await message.reply_text(perms)
  141. @app.on_message(filters.text & ~filters.private, group=69)
  142. async def url_detector(_, message):
  143. user = message.from_user
  144. chat_id = message.chat.id
  145. text = message.text.lower().strip()
  146. if not text or not user:
  147. return
  148. mods = await list_admins(chat_id)
  149. if user.id in mods or user.id in SUDOERS:
  150. return
  151. check = get_urls_from_text(text)
  152. if check:
  153. permissions = await current_chat_permissions(chat_id)
  154. if "can_add_web_page_previews" not in permissions:
  155. try:
  156. await message.delete()
  157. except Exception:
  158. await message.reply_text(
  159. "该消息包含链接,但机器人没有删除消息权限。"
  160. )