2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 21:07:59 +00:00

Filters.command improvements

- Use regex for case sensitivity
- Less indentation
- Ensure that the command returned is the correct case
- Ensure that if the command has more text, it is split by whitespace
This commit is contained in:
Mendel E 2019-08-07 22:31:37 -04:00
parent c85f991443
commit 7cabf922ea

View File

@ -238,19 +238,29 @@ class Filters:
text = message.text or message.caption
message.command = None
if text:
for prefix in flt.prefixes:
if text.startswith(prefix):
# groups are 1-indexed, group(1) is the quote, group(2) is the text
# between the quotes, group(3) is unquoted, whitespace-split text
args = [m.group(2) or m.group(3) or ""
for m in re.finditer(command_re, text[len(prefix):])]
command = args[0] if flt.case_sensitive else args[0].lower()
if command not in flt.commands:
if not text:
return False
message.command = args
pattern = r"^{}(?:\s|$)" if flt.case_sensitive else r"(?i)^{}(?:\s|$)"
for prefix in flt.prefixes:
if not text.startswith(prefix):
continue
without_prefix = text[len(prefix):]
for cmd in flt.commands:
if not re.match(pattern.format(cmd), without_prefix):
continue
# match.groups are 1-indexed, group(1) is the quote, group(2) is the text
# between the quotes, group(3) is unquoted, whitespace-split text
without_command = without_prefix[len(cmd):]
message.command = [cmd] + [
m.group(2) or m.group(3) or ''
for m in command_re.finditer(without_command)
]
break