| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- """
- MIT License
- Copyright (c) 2024 TheHamkerCat
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- """
- from pyrogram import filters
- from pyrogram.errors.exceptions.bad_request_400 import ChatNotModified
- from pyrogram.types import ChatPermissions
- from wbb import SUDOERS, app
- from wbb.core.decorators.errors import capture_err
- from wbb.core.decorators.permissions import adminsOnly
- from wbb.modules.admin import list_admins
- from wbb.utils.functions import get_urls_from_text
- from wbb.utils.i18n import telegram_permission_label
- __MODULE__ = "群权限锁"
- __HELP__ = """
- 命令:/lock、/unlock、/locks
- 可用参数:
- messages 文字 | stickers 贴纸 | gifs 动图 | media 媒体
- games 游戏 | polls 投票 | inline 内联内容 | url 链接预览
- group_info 群信息 | useradd 邀请成员 | pin 置顶
- `/lock all` 可一次锁定全部权限;`/unlock` 不支持 all 参数。
- 示例:`/lock media`
- """
- incorrect_parameters = "参数不正确,请发送 /help locks 查看群权限锁说明。"
- # Using disable_preview as a switch for url checker
- # That way we won't need an additional db to check
- # If url lock is enabled/disabled for a chat
- data = {
- "messages": "can_send_messages",
- "stickers": "can_send_other_messages",
- "gifs": "can_send_other_messages",
- "media": "can_send_media_messages",
- "games": "can_send_other_messages",
- "inline": "can_send_other_messages",
- "url": "can_add_web_page_previews",
- "polls": "can_send_polls",
- "group_info": "can_change_info",
- "useradd": "can_invite_users",
- "pin": "can_pin_messages",
- }
- async def current_chat_permissions(chat_id):
- perms = []
- perm = (await app.get_chat(chat_id)).permissions
- if perm.can_send_messages:
- perms.append("can_send_messages")
- if perm.can_send_media_messages:
- perms.append("can_send_media_messages")
- if perm.can_send_other_messages:
- perms.append("can_send_other_messages")
- if perm.can_add_web_page_previews:
- perms.append("can_add_web_page_previews")
- if perm.can_send_polls:
- perms.append("can_send_polls")
- if perm.can_change_info:
- perms.append("can_change_info")
- if perm.can_invite_users:
- perms.append("can_invite_users")
- if perm.can_pin_messages:
- perms.append("can_pin_messages")
- return perms
- async def tg_lock(message, permissions: list, perm: str, lock: bool):
- if lock:
- if perm not in permissions:
- return await message.reply_text("该权限已经锁定。")
- permissions.remove(perm)
- else:
- if perm in permissions:
- return await message.reply_text("该权限已经解锁。")
- permissions.append(perm)
- permissions = {perm: True for perm in list(set(permissions))}
- try:
- await app.set_chat_permissions(
- message.chat.id, ChatPermissions(**permissions)
- )
- except ChatNotModified:
- return await message.reply_text(
- "请先解锁文字消息权限。"
- )
- await message.reply_text("权限已锁定。" if lock else "权限已解锁。")
- @app.on_message(filters.command(["lock", "unlock"]) & ~filters.private)
- @adminsOnly("can_restrict_members")
- async def locks_func(_, message):
- if len(message.command) != 2:
- return await message.reply_text(incorrect_parameters)
- chat_id = message.chat.id
- parameter = message.text.strip().split(None, 1)[1].lower()
- state = message.command[0].lower()
- if parameter not in data and parameter != "all":
- return await message.reply_text(incorrect_parameters)
- permissions = await current_chat_permissions(chat_id)
- if parameter in data:
- await tg_lock(
- message,
- permissions,
- data[parameter],
- bool(state == "lock"),
- )
- elif parameter == "all" and state == "lock":
- await app.set_chat_permissions(chat_id, ChatPermissions())
- await message.reply_text(f"{message.chat.title} 的全部成员权限已锁定。")
- elif parameter == "all" and state == "unlock":
- await app.set_chat_permissions(
- chat_id,
- ChatPermissions(
- can_send_messages=True,
- can_send_media_messages=True,
- can_send_other_messages=True,
- can_add_web_page_previews=True,
- can_send_polls=True,
- can_change_info=False,
- can_invite_users=True,
- can_pin_messages=False,
- ),
- )
- await message.reply(f"{message.chat.title} 的常用成员权限已解锁。")
- @app.on_message(filters.command("locks") & ~filters.private)
- @capture_err
- async def locktypes(_, message):
- permissions = await current_chat_permissions(message.chat.id)
- if not permissions:
- return await message.reply_text("普通成员当前没有可用权限。")
- perms = ""
- for i in permissions:
- perms += f"__**{telegram_permission_label(i)}**__\n"
- await message.reply_text(perms)
- @app.on_message(filters.text & ~filters.private, group=69)
- async def url_detector(_, message):
- user = message.from_user
- chat_id = message.chat.id
- text = message.text.lower().strip()
- if not text or not user:
- return
- mods = await list_admins(chat_id)
- if user.id in mods or user.id in SUDOERS:
- return
- check = get_urls_from_text(text)
- if check:
- permissions = await current_chat_permissions(chat_id)
- if "can_add_web_page_previews" not in permissions:
- try:
- await message.delete()
- except Exception:
- await message.reply_text(
- "该消息包含链接,但机器人没有删除消息权限。"
- )
|