From 7cabf922ea02a5f76737ff948ef599747d260b3b Mon Sep 17 00:00:00 2001 From: Mendel E Date: Wed, 7 Aug 2019 22:31:37 -0400 Subject: [PATCH] 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 --- pyrogram/client/filters/filters.py | 34 +++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index f74b1b8e..a095ed89 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -238,21 +238,31 @@ 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 not text: + return False - if command not in flt.commands: - return False + pattern = r"^{}(?:\s|$)" if flt.case_sensitive else r"(?i)^{}(?:\s|$)" - 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)