MCPcopy Index your code
hub / github.com/nonebot/nonebot2 / command

Function command

nonebot/rule.py:392–439  ·  view source on GitHub ↗

匹配消息命令。 根据配置里提供的 {ref}``command_start` `, {ref}``command_sep` ` 判断消息是否为命令。 可以通过 {ref}`nonebot.params.Command` 获取匹配成功的命令(例: `("test",)`), 通过 {ref}`nonebot.params.RawCommand` 获取匹配成功的原始命令文本(例: `"/test"`), 通过 {

(
    *cmds: str | tuple[str, ...],
    force_whitespace: str | bool | None = None,
)

Source from the content-addressed store, hash-verified

390
391
392def command(
393 *cmds: str | tuple[str, ...],
394 force_whitespace: str | bool | None = None,
395) -> Rule:
396 """匹配消息命令。
397
398 根据配置里提供的 {ref}``command_start` <nonebot.config.Config.command_start>`,
399 {ref}``command_sep` <nonebot.config.Config.command_sep>` 判断消息是否为命令。
400
401 可以通过 {ref}`nonebot.params.Command` 获取匹配成功的命令(例: `("test",)`),
402 通过 {ref}`nonebot.params.RawCommand` 获取匹配成功的原始命令文本(例: `"/test"`),
403 通过 {ref}`nonebot.params.CommandArg` 获取匹配成功的命令参数。
404
405 参数:
406 cmds: 命令文本或命令元组
407 force_whitespace: 是否强制命令后必须有指定空白符
408
409 用法:
410 使用默认 `command_start`, `command_sep` 配置情况下:
411
412 命令 `("test",)` 可以匹配: `/test` 开头的消息
413 命令 `("test", "sub")` 可以匹配: `/test.sub` 开头的消息
414
415 :::tip 提示
416 命令内容与后续消息间无需空格!
417 :::
418 """
419
420 config = get_driver().config
421 command_start = config.command_start
422 command_sep = config.command_sep
423 commands: list[tuple[str, ...]] = []
424 for command in cmds:
425 if isinstance(command, str):
426 command = (command,)
427
428 commands.append(command)
429
430 if len(command) == 1:
431 for start in command_start:
432 TrieRule.add_prefix(f"{start}{command[0]}", TRIE_VALUE(start, command))
433 else:
434 for start, sep in product(command_start, command_sep):
435 TrieRule.add_prefix(
436 f"{start}{sep.join(command)}", TRIE_VALUE(start, command)
437 )
438
439 return Rule(CommandRule(commands, force_whitespace))
440
441
442class ArgumentParser(ArgParser):

Callers 2

on_commandFunction · 0.90
test_commandFunction · 0.90

Calls 7

get_driverFunction · 0.90
RuleClass · 0.90
TRIE_VALUEClass · 0.85
CommandRuleClass · 0.85
appendMethod · 0.80
add_prefixMethod · 0.80
joinMethod · 0.45

Tested by 1

test_commandFunction · 0.72