From cb959b310ccfb1cce6a4b3dc4fe16de441c38c10 Mon Sep 17 00:00:00 2001 From: yasirarism <55983182+yasirarism@users.noreply.github.com> Date: Tue, 4 Jul 2023 09:35:42 +0700 Subject: [PATCH] debugg --- misskaty/core/misskaty_patch/__init__.py | 2 +- .../misskaty_patch/decorators/__init__.py | 1 + .../core/misskaty_patch/decorators/command.py | 123 ++++++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 misskaty/core/misskaty_patch/decorators/__init__.py create mode 100644 misskaty/core/misskaty_patch/decorators/command.py diff --git a/misskaty/core/misskaty_patch/__init__.py b/misskaty/core/misskaty_patch/__init__.py index 9898ef7c..130afb7f 100644 --- a/misskaty/core/misskaty_patch/__init__.py +++ b/misskaty/core/misskaty_patch/__init__.py @@ -1 +1 @@ -from . import bound, listen, methods +from . import bound, listen, methods, decorators diff --git a/misskaty/core/misskaty_patch/decorators/__init__.py b/misskaty/core/misskaty_patch/decorators/__init__.py new file mode 100644 index 00000000..5e53d88f --- /dev/null +++ b/misskaty/core/misskaty_patch/decorators/__init__.py @@ -0,0 +1 @@ +from .command import command \ No newline at end of file diff --git a/misskaty/core/misskaty_patch/decorators/command.py b/misskaty/core/misskaty_patch/decorators/command.py new file mode 100644 index 00000000..5e6ef78e --- /dev/null +++ b/misskaty/core/misskaty_patch/decorators/command.py @@ -0,0 +1,123 @@ +import typing +import pyrogram +from pyrogram.methods import Decorators +from misskaty.vars import COMMAND_HANDLER + +def command( + self, + command: typing.Union[str, list], + pm_only: typing.Union[bool, bool] = False, + group_only: typing.Union[bool, bool] = False, + self_admin: typing.Union[bool, bool] = False, + self_only: typing.Union[bool, bool] = False, + handler: typing.Optional[list] = None, + filter: typing.Union[pyrogram.filters.Filter, pyrogram.filters.Filter] = None, + *args, + **kwargs + ): + """ + ### `tgEasy.tgClient.command` + - A decorater to Register Commands in simple way and manage errors in that Function itself, alternative for `@pyrogram.Client.on_message(pyrogram.filters.command('command'))` + - Parameters: + - command (str || list): + - The command to be handled for a function + + - group_only (bool) **optional**: + - If True, the command will only executed in Groups only, By Default False. + + - pm_only (bool) **optional**: + - If True, the command will only executed in Private Messages only, By Default False. + + - self_only (bool) **optional**: + - If True, the command will only excute if used by Self only, By Default False. + + - handler (list) **optional**: + - If set, the command will be handled by the specified Handler, By Default `Config.HANDLERS`. + + - self_admin (bool) **optional**: + - If True, the command will only executeed if the Bot is Admin in the Chat, By Default False + + - filter (`~pyrogram.filters`) **optional**: + - Pyrogram Filters, hope you know about this, for Advaced usage. Use `and` for seaperating filters. + + #### Example + .. code-block:: python + import pyrogram + from tgEasy import tgClient + + app = tgClient(pyrogram.Client()) + + @app.command("start", group_only=False, pm_only=False, self_admin=False, self_only=False, pyrogram.filters.chat("777000") and pyrogram.filters.text) + async def start(client, message): + await message.reply_text(f"Hello {message.from_user.mention}") + """ + if handler is None: + handler = COMMAND_HANDLER + if filter: + if self_only: + filter = ( + pyrogram.filters.command(command, prefixes=handler) + & filter + & pyrogram.filters.me + ) + else: + filter = ( + pyrogram.filters.command(command, prefixes=handler) + & filter + & pyrogram.filters.me + ) + else: + if self_only: + filter = ( + pyrogram.filters.command(command, prefixes=handler) + & pyrogram.filters.me + ) + else: + filter = pyrogram.filters.command(command, prefixes=handler) + + def wrapper(func): + async def decorator(client, message: pyrogram.types.Message): + if ( + self_admin + and message.chat.type != pyrogram.enums.ChatType.SUPERGROUP + ): + return await message.reply_text( + "This command can be used in supergroups only." + ) + if self_admin: + me = await client.get_chat_member( + message.chat.id, (await client.get_me()).id + ) + if me.status not in ( + pyrogram.enums.ChatMemberStatus.OWNER, + pyrogram.enums.ChatMemberStatus.ADMINISTRATOR, + ): + return await message.reply_text( + "I must be admin to execute this Command" + ) + if ( + group_only + and message.chat.type != pyrogram.enums.ChatType.SUPERGROUP + ): + return await message.reply_text( + "This command can be used in supergroups only." + ) + if pm_only and message.chat.type != pyrogram.enums.ChatType.PRIVATE: + return await message.reply_text( + "This command can be used in PMs only." + ) + try: + await func(client, message) + except pyrogram.errors.exceptions.forbidden_403.ChatWriteForbidden: + await client.leave_chat(message.chat.id) + except BaseException as exception: + return + + self.__client__.add_handler( + pyrogram.handlers.MessageHandler(callback=decorator, filters=filter) + ) + return decorator + + return wrapper + +Decorators.command = command \ No newline at end of file