diff --git a/src/app.ts b/src/app.ts index b629bca..3248bc9 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,6 +7,7 @@ import config from './utils/core/config'; import './services/ws/wsServer'; import compression from 'compression'; import testController from './modules/test/test.controller'; +import BotController from './modules/bot/bot.controller'; const apps = { async createApp() { @@ -25,6 +26,7 @@ const apps = { { path: '/api/sample', name: '测试模块', controller: sampleController }, { path: '/public', name: '文件模块', controller: imageController }, { path: '/test', name: '测试', controller: testController }, + { path: '/api/bot', name: '寄气人模块', controller: BotController }, ]; modules.forEach((module) => { diff --git a/src/modules/bot/bot.controller.ts b/src/modules/bot/bot.controller.ts new file mode 100644 index 0000000..79fd487 --- /dev/null +++ b/src/modules/bot/bot.controller.ts @@ -0,0 +1,31 @@ +import express from 'express'; +import response from '../../utils/core/response'; +import BotService from './bot.service'; + +class BotController { + private readonly router: express.Router; + + constructor() { + this.router = express.Router(); + this.init(); + } + + public getRouter(): express.Router { + return this.router; + } + + private init(): void { + this.router.post(`/getBotId`, this.postBotsId); + } + + private postBotsId = async (req: express.Request, res: express.Response): Promise => { + try { + const result = await BotService.getBotId(); + await response.success(res, result); + } catch (err) { + await response.error(res, `请求失败..`, 500, err); + } + }; +} + +export default new BotController(); diff --git a/src/modules/bot/bot.service.ts b/src/modules/bot/bot.service.ts new file mode 100644 index 0000000..6fc1242 --- /dev/null +++ b/src/modules/bot/bot.service.ts @@ -0,0 +1,33 @@ +import logger from '../../utils/core/logger'; +import paths from '../../utils/core/path'; +import fs from 'fs/promises'; +import path from 'path'; + +class BotService { + public async getBotId() { + logger.debug('GetBotId..'); + const userPath = paths.get('userData'); + const botsPath = path.join(userPath, '/crystelfBots'); + const dirData = await fs.readdir(botsPath); + const uins: string[] = []; + + for (const fileName of dirData) { + if (!fileName.endsWith('.json')) { + continue; + } + try { + 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 (e) { + logger.error(`读取或解析${fileName}出错`); + } + } + return uins; + } +} + +export default new BotService(); diff --git a/src/test/wsTestClient.ts b/src/test/wsTestClient.ts index dd05db1..9f1a46f 100644 --- a/src/test/wsTestClient.ts +++ b/src/test/wsTestClient.ts @@ -1,7 +1,7 @@ import WebSocket from 'ws'; import axios from 'axios'; -const WS_URL = 'ws://127.0.0.1:3001'; +const WS_URL = 'ws://127.0.0.1:4001'; const WS_SECRET = '114514'; const CLIENT_ID = 'test'; @@ -39,7 +39,7 @@ function createWebSocketClient() { async function testGetAPI() { try { - const response = await axios.get('http://localhost:3000/api/sample/hello'); + const response = await axios.get('http://localhost:4000/api/sample/hello'); console.log('[HTTP][GET] Response:', response.data); } catch (err) { console.error('[HTTP][GET] Error:', err); @@ -48,9 +48,7 @@ async function testGetAPI() { async function testPostAPI() { try { - const response = await axios.post('http://localhost:3000/api/sample/greet', { - name: 'Jerry', - }); + const response = await axios.post('http://localhost:4000/api/bot/getBotId', {}); console.log('[HTTP][POST] Response:', response.data); } catch (err) { console.error('[HTTP][POST] Error:', err);