Skip to content

Commit 4d2719d

Browse files
committed
refacotr: 清理# type ignore, 处理self.ws 可能为None的情况
1 parent 1f4aeb2 commit 4d2719d

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

astrbot/core/platform/sources/kook/kook_client.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import base64
3-
import json
43
import os
54
import random
65
import time
@@ -188,7 +187,12 @@ async def listen(self):
188187
try:
189188
while self.running:
190189
try:
191-
msg = await asyncio.wait_for(self.ws.recv(), timeout=10) # type: ignore
190+
if self.ws is None:
191+
logger.warning(
192+
"[KOOK] 尚未连接kook WebSocket服务器, 跳过消息监听流程"
193+
)
194+
continue
195+
msg = await asyncio.wait_for(self.ws.recv(), timeout=10)
192196

193197
if isinstance(msg, bytes):
194198
try:
@@ -326,9 +330,16 @@ async def _heartbeat_loop(self):
326330

327331
async def _send_ping(self):
328332
"""发送心跳PING"""
333+
if self.ws is None:
334+
logger.warning("[KOOK] 尚未连接kook WebSocket服务器, 跳过发送心跳包流程")
335+
return
329336
try:
330-
ping_data = {"s": 2, "sn": self.last_sn}
331-
await self.ws.send(json.dumps(ping_data)) # type: ignore
337+
ping_data = KookWebsocketEvent(
338+
s=KookMessageSignal.PING,
339+
d=None,
340+
sn=self.last_sn,
341+
)
342+
await self.ws.send(ping_data.to_json())
332343
except Exception as e:
333344
logger.error(f"[KOOK] 发送心跳失败: {e}")
334345

0 commit comments

Comments
 (0)