mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
fixes #206
This commit is contained in:
parent
cf3044687a
commit
8741515cb5
3 changed files with 18 additions and 3 deletions
1
CHANGES/1263.feature
Normal file
1
CHANGES/1263.feature
Normal file
|
|
@ -0,0 +1 @@
|
|||
Added support for commands starting with command prefix by automatically removing user added prefix
|
||||
|
|
@ -78,6 +78,17 @@ class Command(Filter):
|
|||
" or their Iterable"
|
||||
)
|
||||
|
||||
prefixes = []
|
||||
for char in prefix:
|
||||
if char not in prefixes:
|
||||
prefixes.append(char)
|
||||
|
||||
operations = {
|
||||
BotCommand: lambda cmd, prefix: cmd.command.lstrip(prefix),
|
||||
re.Pattern: lambda cmd, prefix: re.compile(cmd.pattern.lstrip(prefix)),
|
||||
str: lambda cmd, prefix: cmd.lstrip(prefix),
|
||||
}
|
||||
|
||||
items = []
|
||||
for command in (*values, *commands):
|
||||
if isinstance(command, BotCommand):
|
||||
|
|
@ -89,6 +100,9 @@ class Command(Filter):
|
|||
)
|
||||
if ignore_case and isinstance(command, str):
|
||||
command = command.casefold()
|
||||
for individual_prefix in prefixes:
|
||||
if isinstance(command, (str, BotCommand, re.Pattern)):
|
||||
command = operations[type(command)](command, individual_prefix)
|
||||
items.append(command)
|
||||
|
||||
if not items:
|
||||
|
|
|
|||
|
|
@ -38,11 +38,11 @@ class TestCommandFilter:
|
|||
@pytest.mark.parametrize(
|
||||
"commands,checklist",
|
||||
[
|
||||
[("Test1", "tEst2", "teSt3"), ("test1", "test2", "test3")],
|
||||
[("/Test1", "tEst2", "teSt3"), ("test1", "test2", "test3")],
|
||||
[("12TeSt", "3t4Est", "5TE6sT"), ("12test", "3t4est", "5te6st")],
|
||||
[[BotCommand(command="Test", description="Test1")], ("test",)],
|
||||
[[BotCommand(command="/Test", description="Test1")], ("test",)],
|
||||
[[BotCommand(command="tEsT", description="Test2")], ("test",)],
|
||||
[(re.compile(r"test(\d+)"), "TeSt"), (re.compile(r"test(\d+)"), "test")],
|
||||
[(re.compile(r"/test(\d+)"), "TeSt"), (re.compile(r"test(\d+)"), "test")],
|
||||
],
|
||||
)
|
||||
def test_init_casefold(self, commands, checklist):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue