From 8741515cb5d9c92a7c6d5effb75ba1309bde0366 Mon Sep 17 00:00:00 2001 From: ShubhamBhut Date: Thu, 21 Sep 2023 10:10:57 +0530 Subject: [PATCH 1/2] fixes #206 --- CHANGES/1263.feature | 1 + aiogram/filters/command.py | 14 ++++++++++++++ tests/test_filters/test_command.py | 6 +++--- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 CHANGES/1263.feature diff --git a/CHANGES/1263.feature b/CHANGES/1263.feature new file mode 100644 index 00000000..3365bc82 --- /dev/null +++ b/CHANGES/1263.feature @@ -0,0 +1 @@ +Added support for commands starting with command prefix by automatically removing user added prefix diff --git a/aiogram/filters/command.py b/aiogram/filters/command.py index 6e654531..d5fcef27 100644 --- a/aiogram/filters/command.py +++ b/aiogram/filters/command.py @@ -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: diff --git a/tests/test_filters/test_command.py b/tests/test_filters/test_command.py index ec79098a..85acf8f4 100644 --- a/tests/test_filters/test_command.py +++ b/tests/test_filters/test_command.py @@ -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): From 2f8ed139803f6f251b5de42564ad60e4ccbcb30c Mon Sep 17 00:00:00 2001 From: Shubham Patel Date: Thu, 12 Oct 2023 21:40:55 +0530 Subject: [PATCH 2/2] update fix #206 --- aiogram/filters/command.py | 19 +++++-------------- tests/test_filters/test_command.py | 2 +- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/aiogram/filters/command.py b/aiogram/filters/command.py index d5fcef27..acaf2a04 100644 --- a/aiogram/filters/command.py +++ b/aiogram/filters/command.py @@ -78,17 +78,6 @@ 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): @@ -100,9 +89,8 @@ 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) + for individual_prefix in prefix: + command = command.lstrip(individual_prefix) items.append(command) if not items: @@ -182,6 +170,9 @@ class Command(Filter): return replace(command, regexp_match=result) command_name = command.command + for individual_prefix in self.prefix: + command_name = command.command.replace(individual_prefix, '', 1) + if self.ignore_case: command_name = command_name.casefold() diff --git a/tests/test_filters/test_command.py b/tests/test_filters/test_command.py index 85acf8f4..54e75e36 100644 --- a/tests/test_filters/test_command.py +++ b/tests/test_filters/test_command.py @@ -42,7 +42,7 @@ class TestCommandFilter: [("12TeSt", "3t4Est", "5TE6sT"), ("12test", "3t4est", "5te6st")], [[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):