ruff
This commit is contained in:
@@ -1,39 +1,47 @@
|
||||
import inspect
|
||||
from typing import Any, Tuple, Dict, List, Callable
|
||||
from typing import Any, Callable, Dict, List, Tuple
|
||||
|
||||
from .config_loader import global_config
|
||||
|
||||
# 从配置中获取命令前缀
|
||||
comm_prefixes = global_config.bot.get("command", ("/",))
|
||||
|
||||
|
||||
class CommandManager:
|
||||
def __init__(self, prefixes: Tuple[str, ...] = ("/",)):
|
||||
self.prefixes = prefixes
|
||||
self.commands: Dict[str, Callable] = {} # 存储消息指令
|
||||
self.notice_handlers: List[Dict] = [] # 存储通知处理器
|
||||
self.request_handlers: List[Dict] = [] # 存储请求处理器
|
||||
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
|
||||
|
||||
# --- 消息分发逻辑 ---
|
||||
@@ -41,24 +49,24 @@ class CommandManager:
|
||||
"""解析并分发消息指令"""
|
||||
if not event.raw_message:
|
||||
return
|
||||
|
||||
|
||||
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
|
||||
return
|
||||
|
||||
# 2. 拆分指令和参数
|
||||
full_cmd = raw_text[len(prefix_found):].split()
|
||||
full_cmd = raw_text[len(prefix_found) :].split()
|
||||
if not full_cmd:
|
||||
return
|
||||
|
||||
|
||||
cmd_name = full_cmd[0]
|
||||
args = full_cmd[1:]
|
||||
|
||||
@@ -87,14 +95,18 @@ class CommandManager:
|
||||
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
|
||||
|
||||
|
||||
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)
|
||||
@@ -102,4 +114,4 @@ elif isinstance(comm_prefixes, str):
|
||||
comm_prefixes = (comm_prefixes,)
|
||||
|
||||
# 实例化全局管理器
|
||||
matcher = CommandManager(prefixes=comm_prefixes)
|
||||
matcher = CommandManager(prefixes=comm_prefixes)
|
||||
|
||||
Reference in New Issue
Block a user