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:
14
core/ws.py
14
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 实例
|
||||
|
||||
# 打印日志
|
||||
|
||||
Reference in New Issue
Block a user