0
我有一個不和諧機器人運行以下setInterval
:對...的循環停止無緣無故
client.setInterval(async() => {
try {
const response = await fetch('http://pso2.kaze.rip/eq/');
if (response.status !== 200) return;
const data = await response.json();
const cache = JSON.parse(await fs.readFile("./cache.json"));
if (data[0]["time"] !== cache["time"]) {
const guilds = client.guilds.filter(guild => { return client.provider.get(guild, "alerts") });
let i = 0;
for (let guild of guilds) {
i = i + 1;
let settings = await client.provider.get(guild[1], "alerts");
let eqs = data[0]["eqs"].filter(item => { return settings["ships"].includes(item["ship"]) });
let format = [];
if (!client.channels.get(settings['channel'])) return;
let channel = client.channels.get(settings['channel']);
if (eqs.length <= 0) return;
if (eqs.length > 0 && eqs.length !== 10) {
for (let eq of eqs) {
format.push(`\`SHIP ${eq['ship']}:\` ${eq['name']} (${eq['jpName']})`);
}
}
else {
format.push(`\`ALL SHIPS:\` ${eqs[0]['name']} (${eqs[0]['jpName']})`);
}
let time = moment(data[0]["when"]);
let string = `:watch:**IN 40 MINUTES:** (${time.format("HH:mm")} JST)\n${format.join('\n')}`;
if (channel.type == "text" && channel.permissionsFor(client.user).has("SEND_MESSAGES")) {
//await client.channels.get(settings['channel']).send(string);
console.log(`${i} Sending alert to guild ${guild[1]}`);
}
else {
console.log(`${i} Skipping guild ${guild[1]}`);
}
}
console.log('writing file...');
await fs.writeFile("cache.json", `{ "time" : "${data[0]["time"]}" }`);
console.log('done');
}
} catch (err) {
console.error(err);
}
}, 10000, client);
一切工作正常,除了第93迭代循環停止,沒有別的後被執行(直到發生setInterval
的下一次迭代)。這很奇怪,因爲guilds
數組中有超過400個對象,我甚至在第94個元素中檢查了我在循環內進行的檢查,並通過了所有這些元素。什麼可能導致這個?
也許因爲在'guilds' 93元。 –
有超過400個。我忘了提及,我甚至檢查了第94個元素,檢查了我在循環中進行的檢查,結果全部通過了。讓我用這些細節來編輯問題。 –
如果你沒有使用'guild'變量,而想使用索引'i',那麼爲什麼不用一個普通的'for(let i = 0; ...)'? –