diff --git a/core/handlers/event_handler.py b/core/handlers/event_handler.py index 3b1540e..c447e76 100644 --- a/core/handlers/event_handler.py +++ b/core/handlers/event_handler.py @@ -116,15 +116,15 @@ class MessageHandler(BaseHandler): if not prefix_found: return - full_cmd = raw_text[len(prefix_found):].split() - if not full_cmd: + command_parts = raw_text[len(prefix_found):].split() + if not command_parts: return - cmd_name = full_cmd[0] - args = full_cmd[1:] + command_name = command_parts[0] + args = command_parts[1:] - if cmd_name in self.commands: - command_info = self.commands[cmd_name] + if command_name in self.commands: + command_info = self.commands[command_name] func = command_info["func"] permission = command_info.get("permission") override_check = command_info.get("override_permission_check", False) diff --git a/core/managers/command_manager.py b/core/managers/command_manager.py index 9f6fb19..e1bc3d3 100644 --- a/core/managers/command_manager.py +++ b/core/managers/command_manager.py @@ -12,7 +12,7 @@ from ..handlers.event_handler import MessageHandler, NoticeHandler, RequestHandl # 从配置中获取命令前缀 -comm_prefixes = global_config.bot.get("command", ("/",)) +command_prefixes = global_config.bot.get("command", ("/",)) class CommandManager: @@ -133,11 +133,11 @@ class CommandManager: # --- 全局单例 --- # 确保前缀配置是元组格式 -if isinstance(comm_prefixes, list): - comm_prefixes = tuple(comm_prefixes) -elif isinstance(comm_prefixes, str): - comm_prefixes = (comm_prefixes,) +if isinstance(command_prefixes, list): + command_prefixes = tuple(command_prefixes) +elif isinstance(command_prefixes, str): + command_prefixes = (command_prefixes,) # 实例化全局唯一的命令管理器 -matcher = CommandManager(prefixes=comm_prefixes) +matcher = CommandManager(prefixes=command_prefixes) diff --git a/core/ws.py b/core/ws.py index 2de5244..3cf2cfc 100644 --- a/core/ws.py +++ b/core/ws.py @@ -13,9 +13,7 @@ WebSocket 连接。它是整个机器人框架的底层通信基础。 """ import asyncio import json -import traceback import uuid -from datetime import datetime import websockets @@ -78,7 +76,7 @@ class WS: logger.info(f"{self.reconnect_interval}秒后尝试重连...") await asyncio.sleep(self.reconnect_interval) - async def _listen_loop(self, websocket): + async def _listen_loop(self, websocket_connection): """ 核心监听循环,处理所有接收到的 WebSocket 消息。 @@ -86,9 +84,9 @@ class WS: 判断是 API 响应还是上报的事件,然后分发给相应的处理逻辑。 Args: - websocket: 当前活动的 WebSocket 连接对象。 + websocket_connection: 当前活动的 WebSocket 连接对象。 """ - async for message in websocket: + async for message in websocket_connection: try: data = json.loads(message) @@ -110,7 +108,7 @@ class WS: except Exception as e: logger.exception(f"解析消息异常: {e}") - async def on_event(self, raw_data: dict): + async def on_event(self, event_data: dict): """ 事件处理和分发层。 @@ -121,11 +119,11 @@ class WS: 4. 将事件对象传递给 `CommandManager` (`matcher`) 进行后续处理。 Args: - raw_data (dict): 从 WebSocket 接收到的原始事件字典。 + event_data (dict): 从 WebSocket 接收到的原始事件字典。 """ try: # 使用工厂创建事件对象 - event = EventFactory.create_event(raw_data) + event = EventFactory.create_event(event_data) event.bot = self.bot # 注入 Bot 实例 # 打印日志 diff --git a/main.py b/main.py index 9a27b74..1c1549a 100644 --- a/main.py +++ b/main.py @@ -7,11 +7,6 @@ import asyncio import os import sys import time - -# 将项目根目录添加到 sys.path -ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(0, ROOT_DIR) - from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler @@ -22,7 +17,14 @@ from core.managers.admin_manager import admin_manager from core.ws import WS from core.managers.plugin_manager import load_all_plugins from core.managers.redis_manager import redis_manager -from core.utils.executor import run_in_thread_pool +from core.utils.executor import run_in_thread_pool, initialize_executor +from core.config_loader import global_config as config + +# 将项目根目录添加到 sys.path +ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, ROOT_DIR) + + class PluginReloadHandler(FileSystemEventHandler): @@ -42,21 +44,21 @@ class PluginReloadHandler(FileSystemEventHandler): self.last_reload_time = 0 self.cooldown = 1.0 # 冷却时间,防止短时间内多次重载 - def on_any_event(self, event): + def on_any_event(self, file_system_event): """ 处理所有文件事件 - :param event: watchdog 事件对象 + :param file_system_event: watchdog 事件对象 """ - if event.is_directory: + if file_system_event.is_directory: return # 只监控 py 文件 - if not event.src_path.endswith(".py"): + if not file_system_event.src_path.endswith(".py"): return # 过滤掉一些临时文件 - if "__pycache__" in event.src_path: + if "__pycache__" in file_system_event.src_path: return # 简单的防抖动 @@ -66,7 +68,7 @@ class PluginReloadHandler(FileSystemEventHandler): self.last_reload_time = current_time - logger.info(f"检测到文件变更: {event.src_path}") + logger.info(f"检测到文件变更: {file_system_event.src_path}") logger.info("正在重载插件...") try: @@ -112,13 +114,11 @@ async def main(): logger.warning(f"插件目录不存在 {plugin_path}") try: - bot = WS() + websocket_client = WS() # 初始化代码执行器 - from core.config_loader import global_config as config - from core.utils.executor import initialize_executor - code_executor = initialize_executor(bot, config) - bot.bot.code_executor = code_executor # 将执行器实例附加到 bot.bot 对象上 + code_executor = initialize_executor(websocket_client, config) + websocket_client.bot.code_executor = code_executor # 将执行器实例附加到 bot.bot 对象上 # 启动代码执行器的后台 worker logger.debug("[Main] 检查是否需要启动代码执行 Worker...") @@ -128,7 +128,7 @@ async def main(): else: logger.warning("[Main] 未启动代码执行 Worker,因为 Docker 客户端未初始化或连接失败。") - await bot.connect() + await websocket_client.connect() finally: if observer.is_alive(): observer.stop() diff --git a/plugins/admin.py b/plugins/admin.py index f922db8..9293d91 100644 --- a/plugins/admin.py +++ b/plugins/admin.py @@ -20,7 +20,7 @@ __plugin_meta__ = { @matcher.command("admin", permission=MessageEvent.ADMIN) -async def handle_admin_command(bot: Bot, event: MessageEvent, args: list[str]): +async def admin_command_handler(bot: Bot, event: MessageEvent, args: list[str]): """ 处理 /admin 指令 @@ -40,8 +40,8 @@ async def handle_admin_command(bot: Bot, event: MessageEvent, args: list[str]): await event.reply("当前没有设置任何管理员。") return - admin_list_str = "\n".join(str(admin_id) for admin_id in admins) - await event.reply(f"当前管理员列表 ({len(admins)}):\n{admin_list_str}") + admin_list_text = "\n".join(str(admin_id) for admin_id in admins) + await event.reply(f"当前管理员列表 ({len(admins)}):\n{admin_list_text}") return if action in ("add", "remove"):