Add some more filters

This commit is contained in:
Dan 2018-04-10 13:14:10 +02:00
parent 9ce13518ec
commit f2424d3b1e

View File

@ -16,12 +16,43 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
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)
)