Skip to content

Commit 4fc4501

Browse files
committed
Remove trailing username when adding args to Message.command
Fixes #676
1 parent 1d940b9 commit 4fc4501

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

pyrogram/filters.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -770,34 +770,34 @@ async def func(flt, client: pyrogram.Client, message: Message):
770770
nonlocal username
771771

772772
if username is None:
773-
username = (await client.get_me()).username
773+
username = (await client.get_me()).username or ""
774774

775775
text = message.text or message.caption
776776
message.command = None
777777

778778
if not text:
779779
return False
780780

781-
pattern = rf"^(?:{{cmd}}|{{cmd}}@{username})(?:\s|$)" if username else r"^{cmd}(?:\s|$)"
782-
783781
for prefix in flt.prefixes:
784782
if not text.startswith(prefix):
785783
continue
786784

787785
without_prefix = text[len(prefix):]
788786

789787
for cmd in flt.commands:
790-
if not re.match(pattern.format(cmd=re.escape(cmd)), without_prefix,
788+
if not re.match(rf"^(?:{cmd}(?:@?{username})?)(?:\s|$)", without_prefix,
791789
flags=re.IGNORECASE if not flt.case_sensitive else 0):
792790
continue
793791

792+
without_command = re.sub(rf"{cmd}(?:@?{username})?\s", "", without_prefix, count=1)
793+
794794
# match.groups are 1-indexed, group(1) is the quote, group(2) is the text
795795
# between the quotes, group(3) is unquoted, whitespace-split text
796796

797797
# Remove the escape character from the arguments
798798
message.command = [cmd] + [
799799
re.sub(r"\\([\"'])", r"\1", m.group(2) or m.group(3) or "")
800-
for m in command_re.finditer(without_prefix[len(cmd):])
800+
for m in command_re.finditer(without_command)
801801
]
802802

803803
return True

tests/filters/test_command.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ async def test_with_args():
107107
await f(c, m)
108108
assert m.command == ["start"] + list("abc")
109109

110+
m = Message('/start@username a b c')
111+
await f(c, m)
112+
assert m.command == ["start"] + list("abc")
113+
110114
m = Message("/start 'a b' c")
111115
await f(c, m)
112116
assert m.command == ["start", "a b", "c"]

0 commit comments

Comments
 (0)