From f0b1cc41f35a5e28b075a1b9c993caf0cbdab8fe Mon Sep 17 00:00:00 2001 From: Jonathan <49692607+jonatan1609@users.noreply.github.com> Date: Wed, 17 Mar 2021 21:07:54 +0200 Subject: [PATCH] Allow decorators in plugins to be stacked (#642) This allows registering the same callback function more than once by using different handlers. --- pyrogram/client.py | 11 +++++------ pyrogram/methods/decorators/on_callback_query.py | 11 ++++++++--- .../methods/decorators/on_chosen_inline_result.py | 11 ++++++++--- pyrogram/methods/decorators/on_deleted_messages.py | 11 ++++++++--- pyrogram/methods/decorators/on_inline_query.py | 11 ++++++++--- pyrogram/methods/decorators/on_message.py | 11 ++++++++--- pyrogram/methods/decorators/on_poll.py | 11 ++++++++--- pyrogram/methods/decorators/on_raw_update.py | 11 ++++++++--- pyrogram/methods/decorators/on_user_status.py | 11 ++++++++--- 9 files changed, 69 insertions(+), 30 deletions(-) diff --git a/pyrogram/client.py b/pyrogram/client.py index 35153bde..2820de18 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -717,13 +717,12 @@ class Client(Methods, Scaffold): for name in vars(module).keys(): # noinspection PyBroadException try: - handler, group = getattr(module, name).handler + for handler, group in getattr(module, name).handlers: + if isinstance(handler, Handler) and isinstance(group, int): + self.add_handler(handler, group) - if isinstance(handler, Handler) and isinstance(group, int): - self.add_handler(handler, group) - - log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format( - self.session_name, type(handler).__name__, name, group, module_path)) + log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format( + self.session_name, type(handler).__name__, name, group, module_path)) count += 1 except Exception: diff --git a/pyrogram/methods/decorators/on_callback_query.py b/pyrogram/methods/decorators/on_callback_query.py index f939a355..884fbb95 100644 --- a/pyrogram/methods/decorators/on_callback_query.py +++ b/pyrogram/methods/decorators/on_callback_query.py @@ -47,9 +47,14 @@ class OnCallbackQuery(Scaffold): if isinstance(self, pyrogram.Client): self.add_handler(pyrogram.handlers.CallbackQueryHandler(func, filters), group) elif isinstance(self, Filter) or self is None: - func.handler = ( - pyrogram.handlers.CallbackQueryHandler(func, self), - group if filters is None else filters + if not hasattr(func, "handlers"): + func.handlers = [] + + func.handlers.append( + ( + pyrogram.handlers.CallbackQueryHandler(func, self), + group if filters is None else filters + ) ) return func diff --git a/pyrogram/methods/decorators/on_chosen_inline_result.py b/pyrogram/methods/decorators/on_chosen_inline_result.py index ae56ea07..4e972a06 100644 --- a/pyrogram/methods/decorators/on_chosen_inline_result.py +++ b/pyrogram/methods/decorators/on_chosen_inline_result.py @@ -47,9 +47,14 @@ class OnChosenInlineResult(Scaffold): if isinstance(self, pyrogram.Client): self.add_handler(pyrogram.handlers.ChosenInlineResultHandler(func, filters), group) elif isinstance(self, Filter) or self is None: - func.handler = ( - pyrogram.handlers.ChosenInlineResultHandler(func, self), - group if filters is None else filters + if not hasattr(func, "handlers"): + func.handlers = [] + + func.handlers.append( + ( + pyrogram.handlers.ChosenInlineResultHandler(func, self), + group if filters is None else filters + ) ) return func diff --git a/pyrogram/methods/decorators/on_deleted_messages.py b/pyrogram/methods/decorators/on_deleted_messages.py index 40787140..d093310c 100644 --- a/pyrogram/methods/decorators/on_deleted_messages.py +++ b/pyrogram/methods/decorators/on_deleted_messages.py @@ -47,9 +47,14 @@ class OnDeletedMessages(Scaffold): if isinstance(self, pyrogram.Client): self.add_handler(pyrogram.handlers.DeletedMessagesHandler(func, filters), group) elif isinstance(self, Filter) or self is None: - func.handler = ( - pyrogram.handlers.DeletedMessagesHandler(func, self), - group if filters is None else filters + if not hasattr(func, "handlers"): + func.handlers = [] + + func.handlers.append( + ( + pyrogram.handlers.DeletedMessagesHandler(func, self), + group if filters is None else filters + ) ) return func diff --git a/pyrogram/methods/decorators/on_inline_query.py b/pyrogram/methods/decorators/on_inline_query.py index dc3edbc8..60463f66 100644 --- a/pyrogram/methods/decorators/on_inline_query.py +++ b/pyrogram/methods/decorators/on_inline_query.py @@ -47,9 +47,14 @@ class OnInlineQuery(Scaffold): if isinstance(self, pyrogram.Client): self.add_handler(pyrogram.handlers.InlineQueryHandler(func, filters), group) elif isinstance(self, Filter) or self is None: - func.handler = ( - pyrogram.handlers.InlineQueryHandler(func, self), - group if filters is None else filters + if not hasattr(func, "handlers"): + func.handlers = [] + + func.handlers.append( + ( + pyrogram.handlers.InlineQueryHandler(func, self), + group if filters is None else filters + ) ) return func diff --git a/pyrogram/methods/decorators/on_message.py b/pyrogram/methods/decorators/on_message.py index ad17bd25..ec23084f 100644 --- a/pyrogram/methods/decorators/on_message.py +++ b/pyrogram/methods/decorators/on_message.py @@ -47,9 +47,14 @@ class OnMessage(Scaffold): if isinstance(self, pyrogram.Client): self.add_handler(pyrogram.handlers.MessageHandler(func, filters), group) elif isinstance(self, Filter) or self is None: - func.handler = ( - pyrogram.handlers.MessageHandler(func, self), - group if filters is None else filters + if not hasattr(func, "handlers"): + func.handlers = [] + + func.handlers.append( + ( + pyrogram.handlers.MessageHandler(func, self), + group if filters is None else filters + ) ) return func diff --git a/pyrogram/methods/decorators/on_poll.py b/pyrogram/methods/decorators/on_poll.py index d2af04dd..fc49b192 100644 --- a/pyrogram/methods/decorators/on_poll.py +++ b/pyrogram/methods/decorators/on_poll.py @@ -47,9 +47,14 @@ class OnPoll(Scaffold): if isinstance(self, pyrogram.Client): self.add_handler(pyrogram.handlers.PollHandler(func, filters), group) elif isinstance(self, Filter) or self is None: - func.handler = ( - pyrogram.handlers.PollHandler(func, self), - group if filters is None else filters + if not hasattr(func, "handlers"): + func.handlers = [] + + func.handlers.append( + ( + pyrogram.handlers.PollHandler(func, self), + group if filters is None else filters + ) ) return func diff --git a/pyrogram/methods/decorators/on_raw_update.py b/pyrogram/methods/decorators/on_raw_update.py index 4d7dc349..0ce6bf5d 100644 --- a/pyrogram/methods/decorators/on_raw_update.py +++ b/pyrogram/methods/decorators/on_raw_update.py @@ -41,9 +41,14 @@ class OnRawUpdate(Scaffold): if isinstance(self, pyrogram.Client): self.add_handler(pyrogram.handlers.RawUpdateHandler(func), group) else: - func.handler = ( - pyrogram.handlers.RawUpdateHandler(func), - group if self is None else group + if not hasattr(func, "handlers"): + func.handlers = [] + + func.handlers.append( + ( + pyrogram.handlers.RawUpdateHandler(func), + group if self is None else group + ) ) return func diff --git a/pyrogram/methods/decorators/on_user_status.py b/pyrogram/methods/decorators/on_user_status.py index d67cfaec..fda3fb80 100644 --- a/pyrogram/methods/decorators/on_user_status.py +++ b/pyrogram/methods/decorators/on_user_status.py @@ -45,9 +45,14 @@ class OnUserStatus(Scaffold): if isinstance(self, pyrogram.Client): self.add_handler(pyrogram.handlers.UserStatusHandler(func, filters), group) elif isinstance(self, Filter) or self is None: - func.handler = ( - pyrogram.handlers.UserStatusHandler(func, self), - group if filters is None else filters + if not hasattr(func, "handlers"): + func.handlers = [] + + func.handlers.append( + ( + pyrogram.handlers.UserStatusHandler(func, self), + group if filters is None else filters + ) ) return func