Feature: 添加多消息段命令解析支持 (#2419)

Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
This commit is contained in:
RainEggplant 2023-10-18 15:55:09 +08:00 committed by GitHub
parent 6559b2ff27
commit 97a57c2f6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -117,6 +117,11 @@ class TrieRule:
# check whitespace # check whitespace
arg_str = segment_text[len(pf.key) :] arg_str = segment_text[len(pf.key) :]
arg_str_stripped = arg_str.lstrip() 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 has_arg = arg_str_stripped or msg
if ( if (
has_arg has_arg

View File

@ -113,6 +113,36 @@ async def test_trie(app: App):
command_whitespace=" ", 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"] del TrieRule.prefix["/fake-prefix"]