| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- """
- 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 functools import wraps
- from traceback import format_exc as err
- from pyrogram.errors.exceptions.forbidden_403 import ChatWriteForbidden
- from pyrogram.types import Message
- from wbb import SUDOERS, app
- from wbb.modules.admin import member_permissions
- from wbb.utils.i18n import telegram_permission_label
- async def authorised(func, subFunc2, client, message, *args, **kwargs):
- chatID = message.chat.id
- try:
- await func(client, message, *args, **kwargs)
- except ChatWriteForbidden:
- await app.leave_chat(chatID)
- except Exception as e:
- try:
- await message.reply_text(str(e.MESSAGE))
- except AttributeError:
- await message.reply_text(str(e))
- e = err()
- print(str(e))
- return subFunc2
- async def unauthorised(message: Message, permission, subFunc2):
- chatID = message.chat.id
- text = (
- "你没有执行此操作所需的权限。"
- + f"\n**所需权限:** __{telegram_permission_label(permission)}__"
- )
- try:
- await message.reply_text(text)
- except ChatWriteForbidden:
- await app.leave_chat(chatID)
- return subFunc2
- def adminsOnly(permission):
- def subFunc(func):
- @wraps(func)
- async def subFunc2(client, message: Message, *args, **kwargs):
- chatID = message.chat.id
- if not message.from_user:
- # For anonymous admins
- if (
- message.sender_chat
- and message.sender_chat.id == message.chat.id
- ):
- return await authorised(
- func,
- subFunc2,
- client,
- message,
- *args,
- **kwargs,
- )
- return await unauthorised(message, permission, subFunc2)
- # For admins and sudo users
- userID = message.from_user.id
- permissions = await member_permissions(chatID, userID)
- if userID not in SUDOERS and permission not in permissions:
- return await unauthorised(message, permission, subFunc2)
- return await authorised(
- func, subFunc2, client, message, *args, **kwargs
- )
- return subFunc2
- return subFunc
|