Skip to content

Commit 0025489

Browse files
committed
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.
1 parent c2da2a6 commit 0025489

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pyrogram/client/methods/decorators/on_message.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import pyrogram
20+
from pyrogram.client.filters.filter import Filter
2021
from ...ext import BaseClient
2122

2223

2324
class OnMessage(BaseClient):
24-
def on_message(self, filters=None, group: int = 0):
25+
def on_message(self=None, filters=None, group: int = 0):
2526
"""Use this decorator to automatically register a function for handling
2627
messages. This does the same thing as :meth:`add_handler` using the
2728
:class:`MessageHandler`.
@@ -36,7 +37,14 @@ def on_message(self, filters=None, group: int = 0):
3637
"""
3738

3839
def decorator(func):
39-
self.add_handler(pyrogram.MessageHandler(func, filters), group)
40-
return func
40+
handler = pyrogram.MessageHandler(func, filters)
41+
42+
if isinstance(self, Filter):
43+
return pyrogram.MessageHandler(func, self), group if filters is None else filters
44+
45+
if self is not None:
46+
self.add_handler(handler, group)
47+
48+
return handler, group
4149

4250
return decorator

0 commit comments

Comments
 (0)