2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Refactor the command filter.

Also allow it to work on media captions as well
This commit is contained in:
Dan 2019-04-13 14:21:53 +02:00
parent 88078d4573
commit 292a6ea7bf

View File

@ -222,14 +222,16 @@ class Filters:
- poll""" - poll"""
@staticmethod @staticmethod
def command(command: str or list, def command(
prefix: str or list = "/", commands: str or list,
separator: str = " ", prefix: str or list = "/",
case_sensitive: bool = False): separator: str = " ",
case_sensitive: bool = False
):
"""Filter commands, i.e.: text messages starting with "/" or any other custom prefix. """Filter commands, i.e.: text messages starting with "/" or any other custom prefix.
Args: Args:
command (``str`` | ``list``): commands (``str`` | ``list``):
The command or list of commands as string the filter should look for. The command or list of commands as string the filter should look for.
Examples: "start", ["start", "help", "settings"]. When a message text containing Examples: "start", ["start", "help", "settings"]. When a message text containing
a command arrives, the command itself and its arguments will be stored in the *command* a command arrives, the command itself and its arguments will be stored in the *command*
@ -249,31 +251,25 @@ class Filters:
Examples: when True, command="Start" would trigger /Start but not /start. Examples: when True, command="Start" would trigger /Start but not /start.
""" """
def f(_, m): def func(flt, message):
if m.text: text = message.text or message.caption
for i in _.p:
if m.text.startswith(i): if text:
t = m.text.split(_.s) for p in flt.p:
c, a = t[0][len(i):], t[1:] if text.startswith(p):
c = c if _.cs else c.lower() s = text.split(flt.s)
m.command = ([c] + a) if c in _.c else None c, a = s[0][len(p):], s[1:]
c = c if flt.cs else c.lower()
message.command = ([c] + a) if c in flt.c else None
break break
return bool(m.command) return bool(message.command)
return create( commands = commands if type(commands) is list else [commands]
"Command", commands = {c if case_sensitive else c.lower() for c in commands}
f, prefixes = set(prefix) if prefix else {""}
c={command if case_sensitive
else command.lower()} return create("Command", func=func, c=commands, p=prefixes, s=separator, cs=case_sensitive)
if not isinstance(command, list)
else {c if case_sensitive
else c.lower()
for c in command},
p=set(prefix) if prefix else {""},
s=separator,
cs=case_sensitive
)
@staticmethod @staticmethod
def regex(pattern, flags: int = 0): def regex(pattern, flags: int = 0):