flash / Mikobot /utils /permissions.py
Karma
Create permissions.py
3fc8928
raw
history blame
4.14 kB
# <============================================== IMPORTS =========================================================>
from functools import wraps
from time import time
from traceback import format_exc as err
from pyrogram.enums import ChatMembersFilter
from pyrogram.errors.exceptions.forbidden_403 import ChatWriteForbidden
from pyrogram.types import Message
from Mikobot import DRAGONS, app
# <=======================================================================================================>
# <================================================ FUNCTION =======================================================>
async def member_permissions(chat_id: int, user_id: int):
perms = []
member = (await app.get_chat_member(chat_id, user_id)).privileges
if not member:
return []
if member.can_post_messages:
perms.append("can_post_messages")
if member.can_edit_messages:
perms.append("can_edit_messages")
if member.can_delete_messages:
perms.append("can_delete_messages")
if member.can_restrict_members:
perms.append("can_restrict_members")
if member.can_promote_members:
perms.append("can_promote_members")
if member.can_change_info:
perms.append("can_change_info")
if member.can_invite_users:
perms.append("can_invite_users")
if member.can_pin_messages:
perms.append("can_pin_messages")
if member.can_manage_video_chats:
perms.append("can_manage_video_chats")
return perms
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 = (
"You don't have the required permission to perform this action."
+ f"\n**Permission:** __{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 DRAGONS and permission not in permissions:
return await unauthorised(message, permission, subFunc2)
return await authorised(func, subFunc2, client, message, *args, **kwargs)
return subFunc2
return subFunc
admins_in_chat = {}
async def list_admins(chat_id: int):
global admins_in_chat
if chat_id in admins_in_chat:
interval = time() - admins_in_chat[chat_id]["last_updated_at"]
if interval < 3600:
return admins_in_chat[chat_id]["data"]
admins_in_chat[chat_id] = {
"last_updated_at": time(),
"data": [
member.user.id
async for member in app.get_chat_members(
chat_id, filter=ChatMembersFilter.ADMINISTRATORS
)
],
}
return admins_in_chat[chat_id]["data"]
# <================================================ END =======================================================>