This commit is contained in:
Lonami Exo 2018-02-20 15:51:55 +01:00
parent 69a85b863f
commit 377a9143fc

View File

@ -174,9 +174,11 @@ 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).
pattern (:obj:`str` | :obj:`Pattern`, optional): Regex pattern. If not ``None``, pattern (:obj:`str`, :obj:`callable`, :obj:`re`, optional):
``re.match`` is used on :attr:`event.message.message` to determine if an event If set, only messages matching this pattern will be handled.
should be handled. You can specify a regex-like string which will be matched
against the message, a callable function that returns ``True``
if a message is acceptable, or a compiled regex pattern.
Notes: Notes:
The ``message.from_id`` might not only be an integer or ``None``, The ``message.from_id`` might not only be an integer or ``None``,
@ -191,7 +193,14 @@ class NewMessage(_EventBuilder):
super().__init__(chats=chats, blacklist_chats=blacklist_chats) super().__init__(chats=chats, blacklist_chats=blacklist_chats)
self.incoming = incoming self.incoming = incoming
self.outgoing = outgoing self.outgoing = outgoing
if isinstance(pattern, str):
self.pattern = re.compile(pattern).match
elif not pattern or callable(pattern):
self.pattern = pattern self.pattern = pattern
elif hasattr(pattern, 'match') and callable(pattern.match):
self.pattern = pattern.match
else:
raise TypeError('Invalid pattern type given')
def build(self, update): def build(self, update):
if isinstance(update, if isinstance(update,
@ -242,7 +251,7 @@ class NewMessage(_EventBuilder):
return return
if self.outgoing and not event.message.out: if self.outgoing and not event.message.out:
return return
if self.pattern and not re.match(self.pattern, event.message.message): if self.pattern and not self.pattern(event.message.message or ''):
return return
return self._filter_event(event) return self._filter_event(event)