mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-29 13:27:47 +00:00
Update Filters.command
- Removed "posix" parameter because we only want posix=True and the filter becomes simpler. - Figured out how to deal with single backslashes ("\") errors. - Refactor the whole filter: use better names for identifiers. - Rename parameter "prefix" to "prefixes".
This commit is contained in:
parent
b4cdf1900c
commit
d8765080d3
@ -212,9 +212,8 @@ class Filters:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def command(
|
def command(
|
||||||
commands: str or list,
|
commands: str or list,
|
||||||
prefix: str or list = "/",
|
prefixes: str or list = "/",
|
||||||
case_sensitive: bool = False,
|
case_sensitive: bool = False
|
||||||
posix: bool = True
|
|
||||||
):
|
):
|
||||||
"""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.
|
||||||
|
|
||||||
@ -225,18 +224,14 @@ class Filters:
|
|||||||
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*
|
||||||
field of the :obj:`Message`.
|
field of the :obj:`Message`.
|
||||||
|
|
||||||
prefix (``str`` | ``list``, *optional*):
|
prefixes (``str`` | ``list``, *optional*):
|
||||||
A prefix or a list of prefixes as string the filter should look for.
|
A prefix or a list of prefixes as string the filter should look for.
|
||||||
Defaults to "/" (slash). Examples: ".", "!", ["/", "!", "."].
|
Defaults to "/" (slash). Examples: ".", "!", ["/", "!", "."], list(".:!").
|
||||||
Can be None or "" (empty string) to allow commands with no prefix at all.
|
Pass None or "" (empty string) to allow commands with no prefix at all.
|
||||||
|
|
||||||
case_sensitive (``bool``, *optional*):
|
case_sensitive (``bool``, *optional*):
|
||||||
Pass True if you want your command(s) to be case sensitive. Defaults to False.
|
Pass True if you want your command(s) to be case sensitive. Defaults to False.
|
||||||
Examples: when True, command="Start" would trigger /Start but not /start.
|
Examples: when True, command="Start" would trigger /Start but not /start.
|
||||||
|
|
||||||
posix (``bool``, *optional*):
|
|
||||||
Pass False if you don't want to use shlex posix mode, Defaults to True.
|
|
||||||
It will strip quotes, and has some other behaviors, read more https://docs.python.org/3/library/shlex.html#parsing-rules
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def func(flt, message):
|
def func(flt, message):
|
||||||
@ -244,26 +239,37 @@ class Filters:
|
|||||||
message.command = None
|
message.command = None
|
||||||
|
|
||||||
if text:
|
if text:
|
||||||
for p in flt.p:
|
for prefix in flt.prefixes:
|
||||||
if text.startswith(p):
|
if text.startswith(prefix):
|
||||||
c = text[len(p):].split(None, 1)[0]
|
command = text[len(prefix):].split()[0]
|
||||||
c = c if flt.cs else c.lower()
|
command = command if flt.case_sensitive else command.lower()
|
||||||
if c not in flt.c:
|
|
||||||
|
if command not in flt.commands:
|
||||||
return False
|
return False
|
||||||
try:
|
|
||||||
a = split(text, posix=flt.psx)[1:]
|
text = text.replace("\\", "\\\\")
|
||||||
message.command = ([c] + a)
|
|
||||||
except ValueError as e:
|
args = shlex.split(text)[1:]
|
||||||
message.command = {"text": text[len(p):], "error": e}
|
message.command = [command] + args
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
return bool(message.command)
|
return bool(message.command)
|
||||||
|
|
||||||
commands = commands if type(commands) is list else [commands]
|
commands = commands if type(commands) is list else [commands]
|
||||||
commands = {c if case_sensitive else c.lower() for c in commands}
|
commands = {c if case_sensitive else c.lower() for c in commands}
|
||||||
prefixes = set(prefix) if prefix else {""}
|
|
||||||
|
|
||||||
return create(func, "CommandFilter", c=commands, p=prefixes, cs=case_sensitive, psx=posix)
|
prefixes = [] if prefixes is None else prefixes
|
||||||
|
prefixes = prefixes if type(prefixes) is list else [prefixes]
|
||||||
|
prefixes = set(prefixes) if prefixes else {""}
|
||||||
|
|
||||||
|
return create(
|
||||||
|
func,
|
||||||
|
"CommandFilter",
|
||||||
|
commands=commands,
|
||||||
|
prefixes=prefixes,
|
||||||
|
case_sensitive=case_sensitive
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def regex(pattern, flags: int = 0):
|
def regex(pattern, flags: int = 0):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user