From 0025489c865bebdc37beddc2587c2f863f0e381e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 12 Oct 2018 14:12:29 +0200 Subject: [PATCH] Allow on_message to behave like a static decorator This enabled usages like @Client.on_message(...). To preserve positional arguments order and thus ease the static decorator usage there's a not-so-elegant hack in place that shifts values. --- pyrogram/client/methods/decorators/on_message.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/methods/decorators/on_message.py b/pyrogram/client/methods/decorators/on_message.py index 0011e083..7a0d54a0 100644 --- a/pyrogram/client/methods/decorators/on_message.py +++ b/pyrogram/client/methods/decorators/on_message.py @@ -17,11 +17,12 @@ # along with Pyrogram. If not, see . import pyrogram +from pyrogram.client.filters.filter import Filter from ...ext import BaseClient class OnMessage(BaseClient): - def on_message(self, filters=None, group: int = 0): + def on_message(self=None, filters=None, group: int = 0): """Use this decorator to automatically register a function for handling messages. This does the same thing as :meth:`add_handler` using the :class:`MessageHandler`. @@ -36,7 +37,14 @@ class OnMessage(BaseClient): """ def decorator(func): - self.add_handler(pyrogram.MessageHandler(func, filters), group) - return func + handler = pyrogram.MessageHandler(func, filters) + + if isinstance(self, Filter): + return pyrogram.MessageHandler(func, self), group if filters is None else filters + + if self is not None: + self.add_handler(handler, group) + + return handler, group return decorator