20260101_01

This commit is contained in:
2026-01-01 00:32:36 +08:00
parent 878dbee576
commit dfd4ec638b
5 changed files with 166 additions and 84 deletions

View File

@@ -1,25 +1,47 @@
from typing import Any
import inspect
from typing import Any, Tuple, Dict, List, Callable
from .config_loader import global_config
comm = global_config.bot.get("command")
# 从配置中获取命令前缀
comm_prefixes = global_config.bot.get("command", ("/",))
class CommandManager:
def __init__(self, prefixes=(tuple[Any, ...] (comm))):
def __init__(self, prefixes: Tuple[str, ...] = ("/",)):
self.prefixes = prefixes
self.commands = {} # 存储指令函数
self.commands: Dict[str, Callable] = {} # 存储消息指令
self.notice_handlers: List[Dict] = [] # 存储通知处理器
self.request_handlers: List[Dict] = [] # 存储请求处理器
# --- 1. 消息指令装饰器 ---
def command(self, name: str):
"""装饰器:注册指令"""
"""装饰器:注册消息指令,例如 @matcher.command("echo")"""
def decorator(func):
self.commands[name] = func
return func
return decorator
# --- 2. 通知事件装饰器 ---
def on_notice(self, notice_type: str = None):
"""装饰器:注册通知处理器"""
def decorator(func):
self.notice_handlers.append({"type": notice_type, "func": func})
return func
return decorator
# --- 3. 请求事件装饰器 ---
def on_request(self, request_type: str = None):
"""装饰器:注册请求处理器"""
def decorator(func):
self.request_handlers.append({"type": request_type, "func": func})
return func
return decorator
# --- 消息分发逻辑 ---
async def handle_message(self, bot, event):
"""解析并分发指令"""
"""解析并分发消息指令"""
if not event.raw_message:
return
raw_text = event.raw_message.strip()
# 1. 检查前缀
@@ -30,7 +52,7 @@ class CommandManager:
break
if not prefix_found:
return # 不是指令,跳过
return
# 2. 拆分指令和参数
full_cmd = raw_text[len(prefix_found):].split()
@@ -43,13 +65,41 @@ class CommandManager:
# 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)
await self._run_handler(func, bot, event, args)
# --- 通知分发逻辑 ---
async def handle_notice(self, bot, event):
"""分发通知事件"""
for handler in self.notice_handlers:
if handler["type"] is None or handler["type"] == event.notice_type:
await self._run_handler(handler["func"], bot, event)
# --- 请求分发逻辑 ---
async def handle_request(self, bot, event):
"""分发请求事件"""
for handler in self.request_handlers:
if handler["type"] is None or handler["type"] == event.request_type:
await self._run_handler(handler["func"], bot, event)
# --- 通用执行器:自动注入参数 ---
async def _run_handler(self, func, bot, event, args=None):
"""根据函数签名自动注入 bot, event 或 args"""
sig = inspect.signature(func)
params = sig.parameters
kwargs = {}
if "bot" in params: kwargs["bot"] = bot
if "event" in params: kwargs["event"] = event
if "args" in params and args is not None: kwargs["args"] = args
# 执行函数
await func(**kwargs)
# 确保前缀是元组格式
if isinstance(comm_prefixes, list):
comm_prefixes = tuple[Any, ...](comm_prefixes)
elif isinstance(comm_prefixes, str):
comm_prefixes = (comm_prefixes,)
# 实例化全局管理器
qianzhui = global_config.bot.get("command")
matcher = CommandManager(prefixes=(tuple[Any, ...] (comm)))
matcher = CommandManager(prefixes=comm_prefixes)