from typing import Any import inspect from .config_loader import global_config comm = global_config.bot.get("command") class CommandManager: def __init__(self, prefixes=(tuple[Any, ...] (comm))): self.prefixes = prefixes self.commands = {} # 存储指令函数 def command(self, name: str): """装饰器:注册指令""" def decorator(func): self.commands[name] = func return func return decorator async def handle_message(self, bot, event): """解析并分发指令""" raw_text = event.raw_message.strip() # 1. 检查前缀 prefix_found = None for p in self.prefixes: if raw_text.startswith(p): prefix_found = p break if not prefix_found: return # 不是指令,跳过 # 2. 拆分指令和参数 full_cmd = raw_text[len(prefix_found):].split() if not full_cmd: return cmd_name = full_cmd[0] args = full_cmd[1:] # 3. 查找并执行 if cmd_name in self.commands: func = self.commands[cmd_name] # 自动注入参数 (判断函数是否需要 args) sig = inspect.signature(func) if "args" in sig.parameters: await func(bot, event, args) else: await func(bot, event) # 实例化全局管理器 qianzhui = global_config.bot.get("command") matcher = CommandManager(prefixes=(tuple[Any, ...] (comm)))