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:
parent
c85f991443
commit
7cabf922ea
@ -238,21 +238,31 @@ class Filters:
|
|||||||
text = message.text or message.caption
|
text = message.text or message.caption
|
||||||
message.command = None
|
message.command = None
|
||||||
|
|
||||||
if text:
|
if not text:
|
||||||
for prefix in flt.prefixes:
|
return False
|
||||||
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:
|
pattern = r"^{}(?:\s|$)" if flt.case_sensitive else r"(?i)^{}(?:\s|$)"
|
||||||
return False
|
|
||||||
|
|
||||||
message.command = args
|
for prefix in flt.prefixes:
|
||||||
|
if not text.startswith(prefix):
|
||||||
|
continue
|
||||||
|
|
||||||
break
|
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
|
||||||
|
|
||||||
return bool(message.command)
|
return bool(message.command)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user