mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-16 19:11:00 +00:00
🎨 improve command implementation
This commit is contained in:
@ -196,12 +196,12 @@ def keyword(msg: str) -> Rule:
|
||||
return Rule(_keyword)
|
||||
|
||||
|
||||
def command(command: Tuple[str, ...]) -> Rule:
|
||||
def command(*cmds: Union[str, Tuple[str, ...]]) -> Rule:
|
||||
"""
|
||||
:说明:
|
||||
命令形式匹配,根据配置里提供的 ``command_start``, ``command_sep`` 判断消息是否为命令。
|
||||
:参数:
|
||||
* ``command: Tuples[str, ...]``: 命令内容
|
||||
* ``*cmds: Union[str, Tuple[str, ...]]``: 命令内容
|
||||
:示例:
|
||||
使用默认 ``command_start``, ``command_sep`` 配置
|
||||
|
||||
@ -216,15 +216,20 @@ def command(command: Tuple[str, ...]) -> Rule:
|
||||
config = get_driver().config
|
||||
command_start = config.command_start
|
||||
command_sep = config.command_sep
|
||||
if len(command) == 1:
|
||||
for start in command_start:
|
||||
TrieRule.add_prefix(f"{start}{command[0]}", command)
|
||||
else:
|
||||
for start, sep in product(command_start, command_sep):
|
||||
TrieRule.add_prefix(f"{start}{sep.join(command)}", command)
|
||||
commands = list(cmds)
|
||||
for index, command in enumerate(commands):
|
||||
if isinstance(command, str):
|
||||
commands[index] = command = (command,)
|
||||
|
||||
if len(command) == 1:
|
||||
for start in command_start:
|
||||
TrieRule.add_prefix(f"{start}{command[0]}", command)
|
||||
else:
|
||||
for start, sep in product(command_start, command_sep):
|
||||
TrieRule.add_prefix(f"{start}{sep.join(command)}", command)
|
||||
|
||||
async def _command(bot: Bot, event: Event, state: dict) -> bool:
|
||||
return command == state["_prefix"]["command"]
|
||||
return state["_prefix"]["command"] in commands
|
||||
|
||||
return Rule(_command)
|
||||
|
||||
|
Reference in New Issue
Block a user