抽象send方法,添加注释
This commit is contained in:
45
core/ws.py
45
core/ws.py
@@ -1,3 +1,8 @@
|
||||
"""
|
||||
WebSocket 核心模块
|
||||
|
||||
负责与 OneBot 实现端建立 WebSocket 连接,处理消息接收、事件分发和 API 调用。
|
||||
"""
|
||||
import asyncio
|
||||
import json
|
||||
import traceback
|
||||
@@ -8,12 +13,20 @@ import websockets
|
||||
|
||||
from models import Event
|
||||
|
||||
from .bot import Bot
|
||||
from .command_manager import matcher
|
||||
from .config_loader import global_config
|
||||
|
||||
|
||||
class WS:
|
||||
"""
|
||||
WebSocket 客户端类,负责与 OneBot 实现端建立连接并处理通信
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
初始化 WebSocket 客户端
|
||||
"""
|
||||
# 读取参数
|
||||
cfg = global_config.napcat_ws
|
||||
self.url = cfg.get("uri")
|
||||
@@ -22,9 +35,12 @@ class WS:
|
||||
|
||||
self.ws = None
|
||||
self._pending_requests = {}
|
||||
self.bot = Bot(self)
|
||||
|
||||
async def connect(self):
|
||||
"""主连接循环"""
|
||||
"""
|
||||
主连接循环,负责建立连接和自动重连
|
||||
"""
|
||||
headers = {"Authorization": f"Bearer {self.token}"} if self.token else {}
|
||||
|
||||
while True:
|
||||
@@ -50,7 +66,11 @@ class WS:
|
||||
await asyncio.sleep(self.reconnect_interval)
|
||||
|
||||
async def _listen_loop(self, websocket):
|
||||
"""核心监听循环"""
|
||||
"""
|
||||
核心监听循环,处理接收到的 WebSocket 消息
|
||||
|
||||
:param websocket: WebSocket 连接对象
|
||||
"""
|
||||
async for message in websocket:
|
||||
try:
|
||||
data = json.loads(message)
|
||||
@@ -72,10 +92,15 @@ class WS:
|
||||
print(f" 解析消息异常: {e}")
|
||||
|
||||
async def on_event(self, raw_data: dict):
|
||||
"""事件分发层:根据 post_type 调用 matcher 对应的处理器"""
|
||||
"""
|
||||
事件分发层:根据 post_type 调用 matcher 对应的处理器
|
||||
|
||||
:param raw_data: 原始事件数据字典
|
||||
"""
|
||||
try:
|
||||
# 解析为 Event 对象
|
||||
event = Event.from_dict(raw_data)
|
||||
event.bot = self.bot
|
||||
|
||||
# 格式化时间用于打印
|
||||
t = datetime.fromtimestamp(event.time).strftime("%H:%M:%S")
|
||||
@@ -87,19 +112,19 @@ class WS:
|
||||
print(
|
||||
f" [{t}] [消息] {event.message_type} | {event.user_id}: {event.raw_message}"
|
||||
)
|
||||
await matcher.handle_message(self, event)
|
||||
await matcher.handle_message(self.bot, event)
|
||||
|
||||
# B. 通知事件 (Notice)
|
||||
elif event.post_type == "notice":
|
||||
print(
|
||||
f" [{t}] [通知] {event.notice_type} | 来自: {event.group_id or '私聊'}"
|
||||
)
|
||||
await matcher.handle_notice(self, event)
|
||||
await matcher.handle_notice(self.bot, event)
|
||||
|
||||
# C. 请求事件 (Request)
|
||||
elif event.post_type == "request":
|
||||
print(f" [{t}] [请求] {event.request_type} | 内容: {event.comment}")
|
||||
await matcher.handle_request(self, event)
|
||||
await matcher.handle_request(self.bot, event)
|
||||
|
||||
# D. 元事件 (Meta Event) - 通常用来心跳检测,可不处理
|
||||
elif event.post_type == "meta_event":
|
||||
@@ -110,7 +135,13 @@ class WS:
|
||||
traceback.print_exc()
|
||||
|
||||
async def call_api(self, action: str, params: dict = None):
|
||||
"""调用 OneBot API"""
|
||||
"""
|
||||
调用 OneBot API
|
||||
|
||||
:param action: API 动作名称
|
||||
:param params: API 参数
|
||||
:return: API 响应结果
|
||||
"""
|
||||
if not self.ws:
|
||||
return {"status": "failed", "msg": "websocket not initialized"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user