mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 06:39:18 +00:00
fix:变量覆盖问题
This commit is contained in:
parent
20a932fd47
commit
f0fd57b6ac
@ -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);
|
||||
}
|
||||
|
@ -149,32 +149,39 @@ class BotService {
|
||||
}
|
||||
|
||||
for (const [groupId, botEntries] of groupMap.entries()) {
|
||||
logger.debug(`[群 ${groupId}] 候选Bot列表: ${JSON.stringify(botEntries)}`);
|
||||
const clientGroups = new Map<string, number[]>();
|
||||
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);
|
||||
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: groupId,
|
||||
clientId: selectedClientId,
|
||||
message,
|
||||
message: message,
|
||||
},
|
||||
};
|
||||
logger.info(
|
||||
`[广播] 向群 ${groupId} 使用Bot ${selectedBotId}(客户端 ${selectedClientId})发送消息,延迟 ${delay / 1000} 秒`
|
||||
`[广播] 向群 ${groupId} 使用Bot ${selectedBotId}(客户端 ${selectedClientId})发送消息${message},延迟 ${delay / 1000} 秒`
|
||||
);
|
||||
wsClientManager.send(selectedClientId, sendData).catch((e) => {
|
||||
logger.error(`发送到群${groupId}失败:`, e);
|
||||
});
|
||||
}, delay);
|
||||
})(groupId, selectedClientId, selectedBotId, delay);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user