2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Update Filters.command

- Remove negative lookahead to fix "\"" reporting commas
- Escape cmd when interpolating the pattern
- Remove the escape character from the arguments
- Return True when a valid command is found, return False at the end
This commit is contained in:
Dan 2019-08-08 16:04:10 +02:00 committed by GitHub
parent 7cabf922ea
commit b55440ab86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -232,7 +232,7 @@ class Filters:
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.
""" """
command_re = re.compile(r"([\"'])(?!\\)(.*?)(?<!\\)\1|(\S+)") command_re = re.compile(r"([\"'])(.*?)(?<!\\)\1|(\S+)")
def func(flt, message): def func(flt, message):
text = message.text or message.caption text = message.text or message.caption
@ -250,21 +250,21 @@ class Filters:
without_prefix = text[len(prefix):] without_prefix = text[len(prefix):]
for cmd in flt.commands: for cmd in flt.commands:
if not re.match(pattern.format(re.escape(cmd)), without_prefix):
if not re.match(pattern.format(cmd), without_prefix):
continue continue
# match.groups are 1-indexed, group(1) is the quote, group(2) is the text # 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 # between the quotes, group(3) is unquoted, whitespace-split text
without_command = without_prefix[len(cmd):]
# Remove the escape character from the arguments
message.command = [cmd] + [ message.command = [cmd] + [
m.group(2) or m.group(3) or '' re.sub(r"\\([\"'])", r"\1", m.group(2) or m.group(3) or "")
for m in command_re.finditer(without_command) for m in command_re.finditer(without_prefix[len(cmd):])
] ]
break return True
return bool(message.command) return False
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}