diff --git a/src/modules/bot/bot.service.ts b/src/modules/bot/bot.service.ts index 03cd96b..5c3db77 100644 --- a/src/modules/bot/bot.service.ts +++ b/src/modules/bot/bot.service.ts @@ -13,26 +13,33 @@ class BotService { const userPath = paths.get('userData'); const botsPath = path.join(userPath, '/crystelfBots'); const dirData = await fs.readdir(botsPath); - const uins: string[] = []; + const uins: { uin: string; nickName: string }[] = []; for (const fileName of dirData) { - if (!fileName.endsWith('.json')) { - continue; - } + if (!fileName.endsWith('.json')) continue; + try { - const fileContent: string | undefined = await redisService.fetch('crystelfBots', fileName); - if (fileContent) { - uins.push(fileContent); + const raw: [] | undefined = await redisService.fetch('crystelfBots', fileName); + if (!raw) continue; + + let botList: any[]; + try { + botList = raw; + if (!Array.isArray(botList)) { + logger.warn(`${fileName}不是数组,已跳过`); + continue; + } + } catch (e) { + logger.warn(`解析 ${fileName} 出错: ${e}`); + continue; + } + for (const bot of botList) { + if (bot.uin && bot.nickName) { + uins.push({ uin: bot.uin, nickName: bot.nickName }); + } } - /* - const filePath = path.join(botsPath, fileName); - const fileContent = await fs.readFile(filePath, 'utf-8'); - const jsonData = JSON.parse(fileContent); - if (jsonData.uin) { - uins.push(jsonData.uin); - }*/ } catch (err) { - logger.error(`读取或解析${fileName}出错: ${err}`); + logger.error(`读取或解析 ${fileName} 出错: ${err}`); } } logger.debug(uins); diff --git a/src/test/wsTestClient.ts b/src/test/wsTestClient.ts index 8552e92..5da4b77 100644 --- a/src/test/wsTestClient.ts +++ b/src/test/wsTestClient.ts @@ -1,8 +1,9 @@ import WebSocket from 'ws'; import axios from 'axios'; +import logger from '../utils/core/logger'; -const WS_URL = 'ws://server.crystelf.top:4001'; -const WS_SECRET = '520520'; +const WS_URL = 'ws://127.0.0.1:4001'; +const WS_SECRET = '114514'; const CLIENT_ID = 'test'; function createWebSocketClient() { @@ -48,10 +49,10 @@ async function testGetAPI() { async function testPostAPI() { try { - const response = await axios.post('https://core.crystelf.top/api/system/restart', { - token: 114113, + const response = await axios.post('http://127.0.0.1:4000/api/bot/getBotId', { + token: 54188, }); - console.log('[HTTP][POST] Response:', response.data); + logger.info('[HTTP][POST] Response:', response.data); } catch (err) { console.error('[HTTP][POST] Error:', err); }