Skip to content

Commit

Permalink
feat: 插件支持正则匹配
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulter committed Aug 5, 2024
1 parent 2ebc27e commit 68ee40a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
21 changes: 18 additions & 3 deletions model/command/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.")
Expand All @@ -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:
Expand All @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion model/command/parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

class CommandTokens():
def __init__(self) -> None:
self.tokens = []
Expand All @@ -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
return cmd_tokens

def regex_match(self, message: str, command: str) -> bool:
return re.search(command, message, re.MULTILINE) is not None

5 changes: 3 additions & 2 deletions model/plugin/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class CommandRegisterRequest():
description: str
priority: int
handler: Callable
use_regex: bool = False
plugin_name: str = None

class PluginCommandBridge():
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))

2 changes: 2 additions & 0 deletions model/plugin/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 9 additions & 7 deletions type/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
'''
Expand Down

0 comments on commit 68ee40a

Please sign in to comment.