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:
2026-01-07 23:02:15 +08:00
parent 56b1014419
commit c3b3541694
5 changed files with 39 additions and 41 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 实例
# 打印日志