优化getBotId路由

This commit is contained in:
Jerry 2025-05-06 13:38:31 +08:00
parent 4c9e06deeb
commit 952f9711eb
2 changed files with 28 additions and 20 deletions

View File

@ -13,26 +13,33 @@ class BotService {
const userPath = paths.get('userData'); const userPath = paths.get('userData');
const botsPath = path.join(userPath, '/crystelfBots'); const botsPath = path.join(userPath, '/crystelfBots');
const dirData = await fs.readdir(botsPath); const dirData = await fs.readdir(botsPath);
const uins: string[] = []; const uins: { uin: string; nickName: string }[] = [];
for (const fileName of dirData) { for (const fileName of dirData) {
if (!fileName.endsWith('.json')) { if (!fileName.endsWith('.json')) continue;
continue;
}
try { try {
const fileContent: string | undefined = await redisService.fetch('crystelfBots', fileName); const raw: [] | undefined = await redisService.fetch('crystelfBots', fileName);
if (fileContent) { if (!raw) continue;
uins.push(fileContent);
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) { } catch (err) {
logger.error(`读取或解析${fileName}出错: ${err}`); logger.error(`读取或解析 ${fileName} 出错: ${err}`);
} }
} }
logger.debug(uins); logger.debug(uins);

View File

@ -1,8 +1,9 @@
import WebSocket from 'ws'; import WebSocket from 'ws';
import axios from 'axios'; import axios from 'axios';
import logger from '../utils/core/logger';
const WS_URL = 'ws://server.crystelf.top:4001'; const WS_URL = 'ws://127.0.0.1:4001';
const WS_SECRET = '520520'; const WS_SECRET = '114514';
const CLIENT_ID = 'test'; const CLIENT_ID = 'test';
function createWebSocketClient() { function createWebSocketClient() {
@ -48,10 +49,10 @@ async function testGetAPI() {
async function testPostAPI() { async function testPostAPI() {
try { try {
const response = await axios.post('https://core.crystelf.top/api/system/restart', { const response = await axios.post('http://127.0.0.1:4000/api/bot/getBotId', {
token: 114113, token: 54188,
}); });
console.log('[HTTP][POST] Response:', response.data); logger.info('[HTTP][POST] Response:', response.data);
} catch (err) { } catch (err) {
console.error('[HTTP][POST] Error:', err); console.error('[HTTP][POST] Error:', err);
} }