diff --git a/model/command/manager.py b/model/command/manager.py index 697ce21..3f7608c 100644 --- a/model/command/manager.py +++ b/model/command/manager.py @@ -20,7 +20,8 @@ class CommandMetadata(): inner_command: bool plugin_metadata: PluginMetadata handler: callable - description: str + use_regex: bool = False + description: str = "" class CommandManager(): def __init__(self): @@ -33,10 +34,13 @@ def register(self, description: str, priority: int, handler: callable, + use_regex: bool = False, plugin_metadata: PluginMetadata = None, ): ''' 优先级越高,越先被处理。 + + use_regex: 是否使用正则表达式匹配指令。 ''' if command in self.commands_handler: raise ValueError(f"Command {command} already exists.") @@ -48,6 +52,7 @@ def register(self, inner_command=plugin_metadata == None, plugin_metadata=plugin_metadata, handler=handler, + use_regex=use_regex, description=description ) if plugin_metadata: @@ -65,13 +70,23 @@ def register_from_pcb(self, pcb: PluginCommandBridge): if not plugin: logger.warning(f"插件 {request.plugin_name} 未找到,无法注册指令 {request.command_name}。") else: - self.register(request.command_name, request.description, request.priority, request.handler, plugin.metadata) + self.register(command=request.command_name, + description=request.description, + priority=request.priority, + handler=request.handler, + use_regex=request.use_regex, + plugin_metadata=plugin.metadata) self.plugin_commands_waitlist = [] async def scan_command(self, message_event: AstrMessageEvent, context: Context) -> CommandResult: message_str = message_event.message_str for _, command in self.commands: - if message_str.startswith(command): + trig = False + if self.commands_handler[command].use_regex: + trig = self.command_parser.regex_match(message_str, command) + else: + trig = message_str.startswith(command) + if trig: logger.info(f"触发 {command} 指令。") command_result = await self.execute_handler(command, message_event, context) if command_result.hit: diff --git a/model/command/parser.py b/model/command/parser.py index db289a1..ff1e4b7 100644 --- a/model/command/parser.py +++ b/model/command/parser.py @@ -1,3 +1,5 @@ +import re + class CommandTokens(): def __init__(self) -> None: self.tokens = [] @@ -16,4 +18,8 @@ def parse(self, message: str): cmd_tokens = CommandTokens() cmd_tokens.tokens = message.split(" ") cmd_tokens.len = len(cmd_tokens.tokens) - return cmd_tokens \ No newline at end of file + return cmd_tokens + + def regex_match(self, message: str, command: str) -> bool: + return re.search(command, message, re.MULTILINE) is not None + \ No newline at end of file diff --git a/model/plugin/command.py b/model/plugin/command.py index bd6c8e4..3321d52 100644 --- a/model/plugin/command.py +++ b/model/plugin/command.py @@ -13,6 +13,7 @@ class CommandRegisterRequest(): description: str priority: int handler: Callable + use_regex: bool = False plugin_name: str = None class PluginCommandBridge(): @@ -20,6 +21,6 @@ def __init__(self, cached_plugins: RegisteredPlugins): self.plugin_commands_waitlist: List[CommandRegisterRequest] = [] self.cached_plugins = cached_plugins - def register_command(self, plugin_name, command_name, description, priority, handler): - self.plugin_commands_waitlist.append(CommandRegisterRequest(command_name, description, priority, handler, plugin_name)) + def register_command(self, plugin_name, command_name, description, priority, handler, use_regex=False): + self.plugin_commands_waitlist.append(CommandRegisterRequest(command_name, description, priority, handler, use_regex, plugin_name)) \ No newline at end of file diff --git a/model/plugin/manager.py b/model/plugin/manager.py index d892965..35fcb90 100644 --- a/model/plugin/manager.py +++ b/model/plugin/manager.py @@ -155,6 +155,8 @@ def plugin_reload(self): p = plugin['module'] module_path = plugin['module_path'] root_dir_name = plugin['pname'] + + logger.info(f"正在加载插件 {root_dir_name} ...") # self.check_plugin_dept_update(cached_plugins, root_dir_name) diff --git a/type/types.py b/type/types.py index 3d73318..195e2b1 100644 --- a/type/types.py +++ b/type/types.py @@ -49,17 +49,19 @@ def register_commands(self, command_name: str, description: str, priority: int, - handler: callable): + handler: callable, + use_regex: bool = False): ''' 注册插件指令。 - `plugin_name`: 插件名,注意需要和你的 metadata 中的一致。 - `command_name`: 指令名,如 "help"。不需要带前缀。 - `description`: 指令描述。 - `priority`: 优先级越高,越先被处理。合理的优先级应该在 1-10 之间。 - `handler`: 指令处理函数。函数参数:message: AstrMessageEvent, context: Context + @param plugin_name: 插件名,注意需要和你的 metadata 中的一致。 + @param command_name: 指令名,如 "help"。不需要带前缀。 + @param description: 指令描述。 + @param priority: 优先级越高,越先被处理。合理的优先级应该在 1-10 之间。 + @param handler: 指令处理函数。函数参数:message: AstrMessageEvent, context: Context + @param use_regex: 是否使用正则表达式匹配指令名。 ''' - self.plugin_command_bridge.register_command(plugin_name, command_name, description, priority, handler) + self.plugin_command_bridge.register_command(plugin_name, command_name, description, priority, handler, use_regex) def register_task(self, coro: Awaitable, task_name: str): '''