diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 50d841d5..ff043a15 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -16,12 +16,43 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import re + from .filter import Filter -def build(name: str, func: callable) -> type: - return type(name, (Filter,), {"__call__": func})() +def build(name: str, func: callable, **kwargs) -> type: + d = {"__call__": func} + d.update(kwargs) + + return type(name, (Filter,), d)() class Filters: - text = build("Text", lambda self, m: bool(m.text)) + text = build("Text", lambda _, m: bool(m.text and not m.text.startswith("/"))) + command = build("Command", lambda _, m: bool(m.text and m.text.startswith("/"))) + reply = build("Reply", lambda _, m: bool(m.reply_to_message)) + forwarded = build("Forwarded", lambda _, m: bool(m.forward_date)) + caption = build("Caption", lambda _, m: bool(m.caption)) + + audio = build("Audio", lambda _, m: bool(m.audio)) + document = build("Document", lambda _, m: bool(m.document)) + photo = build("Photo", lambda _, m: bool(m.photo)) + sticker = build("Sticker", lambda _, m: bool(m.sticker)) + video = build("Video", lambda _, m: bool(m.video)) + voice = build("Voice", lambda _, m: bool(m.voice)) + video_note = build("Voice", lambda _, m: bool(m.video_note)) + contact = build("Contact", lambda _, m: bool(m.contact)) + location = build("Location", lambda _, m: bool(m.location)) + venue = build("Venue", lambda _, m: bool(m.venue)) + + private = build("Private", lambda _, m: bool(m.chat.type == "private")) + group = build("Group", lambda _, m: bool(m.chat.type in ("group", "supergroup"))) + channel = build("Channel", lambda _, m: bool(m.chat.type == "channel")) + + @staticmethod + def regex(pattern, flags: int = 0): + return build( + "Regex", lambda _, m: bool(_.p.search(m.text or "")), + p=re.compile(pattern, flags) + )