Use regex for message.command
This commit is contained in:
parent
d8765080d3
commit
c85f991443
@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
from shlex import split
|
|
||||||
|
|
||||||
from .filter import Filter
|
from .filter import Filter
|
||||||
from ..types.bots_and_keyboards import InlineKeyboardMarkup, ReplyKeyboardMarkup
|
from ..types.bots_and_keyboards import InlineKeyboardMarkup, ReplyKeyboardMarkup
|
||||||
@ -233,6 +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+)")
|
||||||
|
|
||||||
def func(flt, message):
|
def func(flt, message):
|
||||||
text = message.text or message.caption
|
text = message.text or message.caption
|
||||||
@ -241,16 +241,16 @@ class Filters:
|
|||||||
if text:
|
if text:
|
||||||
for prefix in flt.prefixes:
|
for prefix in flt.prefixes:
|
||||||
if text.startswith(prefix):
|
if text.startswith(prefix):
|
||||||
command = text[len(prefix):].split()[0]
|
# groups are 1-indexed, group(1) is the quote, group(2) is the text
|
||||||
command = command if flt.case_sensitive else command.lower()
|
# 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 command not in flt.commands:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
text = text.replace("\\", "\\\\")
|
message.command = args
|
||||||
|
|
||||||
args = shlex.split(text)[1:]
|
|
||||||
message.command = [command] + args
|
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user