feat: 插件支持正则匹配
This commit is contained in:
@@ -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 @@ class CommandManager():
|
||||
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 @@ class CommandManager():
|
||||
inner_command=plugin_metadata == None,
|
||||
plugin_metadata=plugin_metadata,
|
||||
handler=handler,
|
||||
use_regex=use_regex,
|
||||
description=description
|
||||
)
|
||||
if plugin_metadata:
|
||||
@@ -65,13 +70,23 @@ class CommandManager():
|
||||
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:
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import re
|
||||
|
||||
class CommandTokens():
|
||||
def __init__(self) -> None:
|
||||
self.tokens = []
|
||||
@@ -16,4 +18,8 @@ class CommandParser():
|
||||
cmd_tokens = CommandTokens()
|
||||
cmd_tokens.tokens = message.split(" ")
|
||||
cmd_tokens.len = len(cmd_tokens.tokens)
|
||||
return cmd_tokens
|
||||
return cmd_tokens
|
||||
|
||||
def regex_match(self, message: str, command: str) -> bool:
|
||||
return re.search(command, message, re.MULTILINE) is not None
|
||||
|
||||
@@ -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 @@ class PluginCommandBridge():
|
||||
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))
|
||||
|
||||
@@ -155,6 +155,8 @@ class PluginManager():
|
||||
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)
|
||||
|
||||
|
||||
+9
-7
@@ -49,17 +49,19 @@ class Context:
|
||||
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):
|
||||
'''
|
||||
|
||||
Reference in New Issue
Block a user