Handle callable from_users for NewMessage events

This commit is contained in:
Mehdi Rahimi 2024-09-04 20:36:04 +03:30
parent aec957d62d
commit 3a5e035ea8

View File

@ -19,7 +19,7 @@ class NewMessage(EventBuilder):
If set to `True`, only **outgoing** messages will be handled. If set to `True`, only **outgoing** messages will be handled.
Mutually exclusive with ``incoming`` (can only set one of either). Mutually exclusive with ``incoming`` (can only set one of either).
from_users (`entity`, optional): from_users (`entity`, `callable`, optional):
Unlike `chats`, this parameter filters the *senders* of the Unlike `chats`, this parameter filters the *senders* of the
message. That is, only messages *sent by these users* will be message. That is, only messages *sent by these users* will be
handled. Use `chats` if you want private messages with this/these handled. Use `chats` if you want private messages with this/these
@ -74,6 +74,7 @@ class NewMessage(EventBuilder):
self.outgoing = outgoing self.outgoing = outgoing
self.from_users = from_users self.from_users = from_users
self.forwards = forwards self.forwards = forwards
if isinstance(pattern, str): if isinstance(pattern, str):
self.pattern = re.compile(pattern).match self.pattern = re.compile(pattern).match
elif not pattern or callable(pattern): elif not pattern or callable(pattern):
@ -83,6 +84,9 @@ class NewMessage(EventBuilder):
else: else:
raise TypeError('Invalid pattern type given') raise TypeError('Invalid pattern type given')
if callable(from_users):
self.from_users = from_users()
# Should we short-circuit? E.g. perform no check at all # Should we short-circuit? E.g. perform no check at all
self._no_check = all(x is None for x in ( self._no_check = all(x is None for x in (
self.chats, self.incoming, self.outgoing, self.pattern, self.chats, self.incoming, self.outgoing, self.pattern,