fix:变量覆盖问题

This commit is contained in:
Jerry 2025-06-01 01:01:36 +08:00
parent 20a932fd47
commit f0fd57b6ac
3 changed files with 30 additions and 23 deletions

View File

@ -111,11 +111,11 @@ class BotController {
try { try {
const token = req.body.token; const token = req.body.token;
if (tools.checkToken(token.toString())) { if (tools.checkToken(token.toString())) {
const groupId: number = req.body.groupId; const groupId: number = Number(req.body.groupId);
const message: string = req.body.message; const message: string = req.body.message.toString();
const flag: boolean = await BotService.sendMessage(groupId, message); const flag: boolean = await BotService.sendMessage(groupId, message);
if (flag) { if (flag) {
await response.success(res, {}); await response.success(res, { message: '消息发送成功..' });
} else { } else {
await response.error(res); await response.error(res);
} }

View File

@ -149,32 +149,39 @@ class BotService {
} }
for (const [groupId, botEntries] of groupMap.entries()) { for (const [groupId, botEntries] of groupMap.entries()) {
logger.debug(`[群 ${groupId}] 候选Bot列表: ${JSON.stringify(botEntries)}`);
const clientGroups = new Map<string, number[]>(); const clientGroups = new Map<string, number[]>();
botEntries.forEach(({ botId, clientId }) => { botEntries.forEach(({ botId, clientId }) => {
if (!clientGroups.has(clientId)) clientGroups.set(clientId, []); if (!clientGroups.has(clientId)) clientGroups.set(clientId, []);
clientGroups.get(clientId)!.push(botId); clientGroups.get(clientId)!.push(botId);
}); });
const selectedClientId = tools.getRandomItem([...clientGroups.keys()]); const selectedClientId = tools.getRandomItem([...clientGroups.keys()]);
logger.debug(`[群 ${groupId}] 随机选中 Client: ${selectedClientId}`);
const botCandidates = clientGroups.get(selectedClientId)!; const botCandidates = clientGroups.get(selectedClientId)!;
logger.debug(`[群 ${groupId}] 该 Client 下候选 Bot: ${botCandidates}`);
const selectedBotId = tools.getRandomItem(botCandidates); const selectedBotId = tools.getRandomItem(botCandidates);
const delay = tools.getRandomDelay(30_000, 90_000); logger.debug(`[群 ${groupId}] 最终选中 Bot: ${selectedBotId}`);
const delay = tools.getRandomDelay(10_000, 150_000);
//解决闭包导致的变量覆盖问题
((groupId, selectedClientId, selectedBotId, delay) => {
setTimeout(() => { setTimeout(() => {
const sendData = { const sendData = {
type: 'sendMessage', type: 'sendMessage',
data: { data: {
botId: selectedBotId, botId: selectedBotId,
groupId, groupId: groupId,
clientId: selectedClientId, clientId: selectedClientId,
message, message: message,
}, },
}; };
logger.info( logger.info(
`[广播] 向群 ${groupId} 使用Bot ${selectedBotId}(客户端 ${selectedClientId})发送消息,延迟 ${delay / 1000}` `[广播] 向群 ${groupId} 使用Bot ${selectedBotId}(客户端 ${selectedClientId})发送消息${message},延迟 ${delay / 1000}`
); );
wsClientManager.send(selectedClientId, sendData).catch((e) => { wsClientManager.send(selectedClientId, sendData).catch((e) => {
logger.error(`发送到群${groupId}失败:`, e); logger.error(`发送到群${groupId}失败:`, e);
}); });
}, delay); }, delay);
})(groupId, selectedClientId, selectedBotId, delay);
} }
} }

View File

@ -50,8 +50,8 @@ async function testGetAPI() {
async function testPostAPI() { async function testPostAPI() {
try { try {
const response = await axios.post('https://core.crystelf.top/api/bot/getGroupInfo', { const response = await axios.post('https://core.crystelf.top/api/bot/getGroupInfo', {
token: '阿弥诺斯', token: '114113',
groupId: '1042721418', groupId: 796070855,
}); });
logger.info('[HTTP][POST] Response:', response.data); logger.info('[HTTP][POST] Response:', response.data);
} catch (err) { } catch (err) {