Skip to content

Commit

Permalink
✨ Feature: 添加多消息段命令解析支持 (#2419)
Browse files Browse the repository at this point in the history
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
  • Loading branch information
RainEggplant and yanyongyu authored Oct 18, 2023
1 parent 6559b2f commit 97a57c2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions nonebot/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ def get_value(cls, bot: Bot, event: Event, state: T_State) -> CMD_RESULT:
# check whitespace
arg_str = segment_text[len(pf.key) :]
arg_str_stripped = arg_str.lstrip()
# check next segment until arg detected or no text remain
while not arg_str_stripped and msg and msg[0].is_text():
arg_str += str(msg.pop(0))
arg_str_stripped = arg_str.lstrip()

has_arg = arg_str_stripped or msg
if (
has_arg
Expand Down
30 changes: 30 additions & 0 deletions tests/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,36 @@ async def test_trie(app: App):
command_whitespace=" ",
)

message = FakeMessageSegment.text("/fake-prefix ") + FakeMessageSegment.text(
" some args"
)
event = make_fake_event(_message=message)()
state = {}
TrieRule.get_value(bot, event, state)
assert state[PREFIX_KEY] == CMD_RESULT(
command=("fake-prefix",),
raw_command="/fake-prefix",
command_arg=FakeMessage("some args"),
command_start="/",
command_whitespace=" ",
)

message = (
FakeMessageSegment.text("/fake-prefix ")
+ FakeMessageSegment.text(" ")
+ FakeMessageSegment.text(" some args")
)
event = make_fake_event(_message=message)()
state = {}
TrieRule.get_value(bot, event, state)
assert state[PREFIX_KEY] == CMD_RESULT(
command=("fake-prefix",),
raw_command="/fake-prefix",
command_arg=FakeMessage("some args"),
command_start="/",
command_whitespace=" ",
)

del TrieRule.prefix["/fake-prefix"]


Expand Down

0 comments on commit 97a57c2

Please sign in to comment.