refactor: 统一变量命名并优化代码结构
- 将函数名从 handle_admin_command 改为 admin_command_handler 以保持命名一致性 - 将变量名 comm_prefixes 改为 command_prefixes 以提高可读性 - 重命名 full_cmd 为 command_parts 和 cmd_name 为 command_name 以明确用途 - 简化 WebSocket 相关代码,移除未使用的导入 - 优化 main.py 中的初始化逻辑和变量命名
This commit is contained in:
36
main.py
36
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()
|
||||
|
||||
Reference in New Issue
Block a user