From fca3c0ec8cb5e02ef1bcf908a934fdc9ebcf6fa2 Mon Sep 17 00:00:00 2001 From: Jayant Hegde Kageri Date: Wed, 5 Jan 2022 22:44:11 +0530 Subject: [PATCH] Release tgEasy v1.3.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For changelog: For changelog: https://github.com/jayantkageri/tgEasy/releases/tag/v1.3.0 Co-Authored-By: ÁÑÑÍHÌLÅTØR SPÄRK <75305464+annihilatorrrr@users.noreply.github.com> --- requirements.txt | 21 +----- setup.py | 5 +- tgEasy/__init__.py | 2 +- tgEasy/decorater/adminsOnly.py | 123 ++++++++++++++++++++++++++++++--- tgEasy/decorater/callback.py | 1 + tgEasy/decorater/command.py | 1 + 6 files changed, 121 insertions(+), 32 deletions(-) diff --git a/requirements.txt b/requirements.txt index c003653..1ef2e5c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,22 +1,5 @@ -# tgEasy - Easy for a brighter Shine. A monkey pather add-on for Pyrogram -# Copyright (C) 2021 Jayant Hegde Kageri - -# This file is part of tgEasy. - -# tgEasy is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# tgEasy is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. - -# You should have received a copy of the GNU Lesser General Public License -# along with tgEasy. If not, see . - pyrogram TgCrypto pyromod -prettyconf \ No newline at end of file +prettyconf +cachetools \ No newline at end of file diff --git a/setup.py b/setup.py index 3ae3306..f3f229e 100644 --- a/setup.py +++ b/setup.py @@ -29,6 +29,9 @@ with open("README.md", encoding="utf-8") as f: readme = f.read() +with open("requirements.txt", encoding="utf-8") as f: + requirements = f.read().splitlines() + setup( name="tgEasy", version=version, @@ -71,5 +74,5 @@ python_requires="~=3.6", packages=find_packages(), zip_safe=False, - install_requires=["pyrogram", "tgcrypto", "pyromod", "prettyconf"], + install_requires=requirements, ) diff --git a/tgEasy/__init__.py b/tgEasy/__init__.py index cb00840..55c188a 100644 --- a/tgEasy/__init__.py +++ b/tgEasy/__init__.py @@ -30,7 +30,7 @@ from .helpers import * from .scaffold import Scaffold -__version__ = "1.2.7" +__version__ = "1.3.0" __copyright__ = "Copyright 2021 Jayant Hegde Kageri " __license__ = "GNU Lesser General Public License v3 or later (LGPLv3+)" logging = logger.getLogger("tgEasy") diff --git a/tgEasy/decorater/adminsOnly.py b/tgEasy/decorater/adminsOnly.py index 1c56ed4..50eea9e 100644 --- a/tgEasy/decorater/adminsOnly.py +++ b/tgEasy/decorater/adminsOnly.py @@ -19,19 +19,100 @@ import typing import pyrogram +from cachetools import TTLCache +from pyrogram.types.messages_and_media import message + from tgEasy.scaffold import Scaffold from ..helpers import check_rights, handle_error, is_admin +ANON = TTLCache(maxsize=250, ttl=30) + + +async def anonymous_admin(m: pyrogram.types.Message): + """ + Helper function for Anonymous Admin Verification + """ + keyboard = pyrogram.types.InlineKeyboardMarkup( + [ + [ + pyrogram.types.InlineKeyboardButton( + text="Verify!", + callback_data=f"anon.{m.message_id}", + ), + ] + ] + ) + return await m.reply_text( + "Click here to prove you are admin with the required rights to perform this action!", + reply_markup=keyboard, + ) + class AdminsOnly(Scaffold): + async def anonymous_admin_verification( + client, CallbackQuery: pyrogram.types.CallbackQuery + ): + if int( + f"{CallbackQuery.message.chat.id}{CallbackQuery.data.split('.')[1]}" + ) not in set(ANON.keys()): + try: + await CallbackQuery.message.edit_text("Button has been Expired.") + except pyrogram.types.RPCError: + try: + await CallbackQuery.message.delete() + except pyrogram.types.RPCError: + pass + return + cb = ANON.pop( + int(f"{CallbackQuery.message.chat.id}{CallbackQuery.data.split('.')[1]}") + ) + member = await CallbackQuery.message.chat.get_member(CallbackQuery.from_user.id) + if bool(member.status not in ("creator", "administrator")): + return await CallbackQuery.answer("You need to be an admin to do this.") + permission = cb[2] + + if isinstance(permission, str): + if not await check_rights( + CallbackQuery.message.chat.id, + CallbackQuery.from_user.id, + permission, + client=client, + ): + return await CallbackQuery.message.edit_text( + f"You are Missing the following Rights to use this Command:\n{permission}", + ) + if isinstance(permission, list): + permissions = "" + for perm in permission: + if not await check_rights( + CallbackQuery.message.chat.id, + CallbackQuery.from_user.id, + perm, + client=client, + ): + permissions += f"\n{perm}" + if not permissions == "": + return await CallbackQuery.message.edit_text( + f"You are Missing the following Rights to use this Command:{permissions}", + ) + try: + await CallbackQuery.message.delete() + await cb[1](client, cb[0]) + except pyrogram.errors.exceptions.forbidden_403.ChatAdminRequired: + return await CallbackQuery.message.edit_text( + "I must be admin to execute this Command", + ) + except BaseException as e: + return await handle_error(e, CallbackQuery) + def adminsOnly( self, permission: typing.Union[str, list], TRUST_ANON_ADMINS: typing.Union[bool, bool] = False, ): """ - ### `tgEasy.tgClient.adminsOnly` + # `tgEasy.tgClient.adminsOnly` - A decorater for running the function only if the admin have the specified Rights. - We are still Working on this to make it to check Rights for Anonoymous Admins, Stay Tuned. - Parameters: @@ -41,7 +122,7 @@ def adminsOnly( - TRUST_ANON_ADMIN (bool) **optional**: - If User is Anonymous Admin also, It Runs the Function, By Default False - ### Example + # Example .. code-block:: python from tgEasy import tgClient import pyrogram @@ -59,34 +140,48 @@ async def decorator(client, message): permissions = "" if not message.chat.type == "supergroup": return await message.reply_text( - "This command can be used in supergroups only." + "This command can be used in supergroups only.", ) if message.sender_chat and not TRUST_ANON_ADMINS: - return await message.reply_text( - "The Right Check for Anonymous Admins is in Development. So you cannot perform this Action for Now, If you don't want this and want to Allow Anonymous Admins for performing Actions in this time Please Contact Bot Owner." + # return await message.reply_text( + # "The Right Check for Anonymous Admins is in Development. So you cannot perform this Action for Now, If you don't want this and want to Allow Anonymous Admins for performing Actions in this time Please Contact Bot Owner.", + # ) + ANON[int(f"{message.chat.id}{message.message_id}")] = ( + message, + func, + permission, ) + return await anonymous_admin(message) if not await is_admin( - message.chat.id, message.from_user.id, client=client + message.chat.id, + message.from_user.id, + client=client, ): return await message.reply_text( - "Only admins can execute this Command!" + "Only admins can execute this Command!", ) if isinstance(permission, str): if not await check_rights( - message.chat.id, message.from_user.id, permission, client=client + message.chat.id, + message.from_user.id, + permission, + client=client, ): return await message.reply_text( - f"You are Missing the following Rights to use this Command:\n{permission}" + f"You are Missing the following Rights to use this Command:\n{permission}", ) if isinstance(permission, list): for perm in permission: if not await check_rights( - message.chat.id, message.from_user.id, perm, client=client + message.chat.id, + message.from_user.id, + perm, + client=client, ): permissions += f"\n{perm}" if not permissions == "": return await message.reply_text( - f"You are Missing the following Rights to use this Command:{permissions}" + f"You are Missing the following Rights to use this Command:{permissions}", ) try: await func(client, message) @@ -95,6 +190,12 @@ async def decorator(client, message): except BaseException as exception: await handle_error(exception, message) + self.__client__.add_handler( + pyrogram.handlers.CallbackQueryHandler( + AdminsOnly.anonymous_admin_verification, + pyrogram.filters.regex("^anon."), + ), + ) return decorator return wrapper diff --git a/tgEasy/decorater/callback.py b/tgEasy/decorater/callback.py index c737e64..6a4b438 100644 --- a/tgEasy/decorater/callback.py +++ b/tgEasy/decorater/callback.py @@ -19,6 +19,7 @@ import typing import pyrogram + from tgEasy.scaffold import Scaffold from ..helpers import handle_error diff --git a/tgEasy/decorater/command.py b/tgEasy/decorater/command.py index 1f2151a..b2e455c 100644 --- a/tgEasy/decorater/command.py +++ b/tgEasy/decorater/command.py @@ -19,6 +19,7 @@ import typing import pyrogram + from tgEasy.scaffold import Scaffold from ..config import Config