From 9672b3ecfbd15b94eb1f7e1bb67e51f7f9b38baf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joscha=20G=C3=B6tzer?= Date: Mon, 22 Oct 2018 23:37:19 +0200 Subject: [PATCH] Allow access to result of EventBuilder's `func` predicate in event handler callbacks --- telethon/events/common.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/telethon/events/common.py b/telethon/events/common.py index f5fdc11f..06e3961e 100644 --- a/telethon/events/common.py +++ b/telethon/events/common.py @@ -63,7 +63,8 @@ class EventBuilder(abc.ABC): @client.on(events.NewMessage(func=lambda e: e.is_private)) async def handler(event): - pass # code here + # Access the result of func + assert event.func_result is True """ self_id = None @@ -114,8 +115,14 @@ class EventBuilder(abc.ABC): # If it doesn't match but it's a whitelist ignore. return None - if not self.func or self.func(event): - return event + if self.func: + func_result = self.func(event) + + if not func_result: + return None + + event.func_result = func_result + return event class EventCommon(ChatGetter, abc.ABC): @@ -126,8 +133,11 @@ class EventCommon(ChatGetter, abc.ABC): ` which means you have access to all chat properties and methods. - In addition, you can access the `original_update` - field which contains the original :tl:`Update`. + You can access the `original_update` field which + contains the original :tl:`Update`. + + In addition, the result of the event builder's `func` + predicate can be accessed via `event.func_result`. """ _event_name = 'Event' @@ -140,6 +150,7 @@ class EventCommon(ChatGetter, abc.ABC): self._chat = None self._broadcast = broadcast self.original_update = None + self.func_result = None def _set_client(self, client): """