From f0fd57b6ac09d9ee50e6c7dbf3eb6fffdde91655 Mon Sep 17 00:00:00 2001 From: Jerry Date: Sun, 1 Jun 2025 01:01:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=8F=98=E9=87=8F=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/bot/bot.controller.ts | 6 ++--- src/modules/bot/bot.service.ts | 43 ++++++++++++++++++------------- src/test/wsTestClient.ts | 4 +-- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/modules/bot/bot.controller.ts b/src/modules/bot/bot.controller.ts index bc5f901..eb3b34f 100644 --- a/src/modules/bot/bot.controller.ts +++ b/src/modules/bot/bot.controller.ts @@ -111,11 +111,11 @@ class BotController { try { const token = req.body.token; if (tools.checkToken(token.toString())) { - const groupId: number = req.body.groupId; - const message: string = req.body.message; + const groupId: number = Number(req.body.groupId); + const message: string = req.body.message.toString(); const flag: boolean = await BotService.sendMessage(groupId, message); if (flag) { - await response.success(res, {}); + await response.success(res, { message: '消息发送成功..' }); } else { await response.error(res); } diff --git a/src/modules/bot/bot.service.ts b/src/modules/bot/bot.service.ts index 2eab80e..8b67746 100644 --- a/src/modules/bot/bot.service.ts +++ b/src/modules/bot/bot.service.ts @@ -149,32 +149,39 @@ class BotService { } for (const [groupId, botEntries] of groupMap.entries()) { + logger.debug(`[群 ${groupId}] 候选Bot列表: ${JSON.stringify(botEntries)}`); const clientGroups = new Map(); botEntries.forEach(({ botId, clientId }) => { if (!clientGroups.has(clientId)) clientGroups.set(clientId, []); clientGroups.get(clientId)!.push(botId); }); const selectedClientId = tools.getRandomItem([...clientGroups.keys()]); + logger.debug(`[群 ${groupId}] 随机选中 Client: ${selectedClientId}`); const botCandidates = clientGroups.get(selectedClientId)!; + logger.debug(`[群 ${groupId}] 该 Client 下候选 Bot: ${botCandidates}`); const selectedBotId = tools.getRandomItem(botCandidates); - const delay = tools.getRandomDelay(30_000, 90_000); - setTimeout(() => { - const sendData = { - type: 'sendMessage', - data: { - botId: selectedBotId, - groupId, - clientId: selectedClientId, - message, - }, - }; - logger.info( - `[广播] 向群 ${groupId} 使用Bot ${selectedBotId}(客户端 ${selectedClientId})发送消息,延迟 ${delay / 1000} 秒` - ); - wsClientManager.send(selectedClientId, sendData).catch((e) => { - logger.error(`发送到群${groupId}失败:`, e); - }); - }, delay); + logger.debug(`[群 ${groupId}] 最终选中 Bot: ${selectedBotId}`); + const delay = tools.getRandomDelay(10_000, 150_000); + //解决闭包导致的变量覆盖问题 + ((groupId, selectedClientId, selectedBotId, delay) => { + setTimeout(() => { + const sendData = { + type: 'sendMessage', + data: { + botId: selectedBotId, + groupId: groupId, + clientId: selectedClientId, + message: message, + }, + }; + logger.info( + `[广播] 向群 ${groupId} 使用Bot ${selectedBotId}(客户端 ${selectedClientId})发送消息${message},延迟 ${delay / 1000} 秒` + ); + wsClientManager.send(selectedClientId, sendData).catch((e) => { + logger.error(`发送到群${groupId}失败:`, e); + }); + }, delay); + })(groupId, selectedClientId, selectedBotId, delay); } } diff --git a/src/test/wsTestClient.ts b/src/test/wsTestClient.ts index e7c6158..2e4c508 100644 --- a/src/test/wsTestClient.ts +++ b/src/test/wsTestClient.ts @@ -50,8 +50,8 @@ async function testGetAPI() { async function testPostAPI() { try { const response = await axios.post('https://core.crystelf.top/api/bot/getGroupInfo', { - token: '阿弥诺斯', - groupId: '1042721418', + token: '114113', + groupId: 796070855, }); logger.info('[HTTP][POST] Response:', response.data); } catch (err) {