代码读取下一个结果并无缘无故地在两者之间循环
Code reads next result and loops between the two for no apparent reason
即使我已将代码设置为只允许它 运行 一次(完成 = 真),直到它再次循环该函数,即使我让它比较最后一个,它仍然循环通过两者。这是我的代码:
client.on('ready', () => {
console.log('Bot is ready!')
checkLiveUpdating();
});
function checkLiveUpdating() {
if(config.Server.AutoRefresh.Enabled == true) {
const guild = client.guilds.get(config.Server.GuildId);
if(guild) {
const channel = guild.channels.find("name", config.Server.AutoRefresh.ChannelName);
if(channel) {
liveUpdating(channel);
} else {
console.log("Valid channel not found! Please check your settings and the bot's permissions.");
}
} else {
console.log("Guild not found! Please check the id and make sure the bot is joined to the guild.");
}
}
}
async function liveUpdating(Channel) {
var LatestMessage = "";
while(true) {
request('https://' + config.Logger.Subdomain + "." + activitylink, function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('.activity-type-edit').each(function(i, element) {
if($("strong a", element).text().indexOf("User:") === -1 && done == false) {
var whatWasEdited = $("table tbody tr td em", element).text();
if(whatWasEdited !== "") {} else {whatWasEdited = "Page";}
var Message = $("strong a", element).text() + " - " + $("cite span", element).text() + ". Edited: " + whatWasEdited;
if(LatestMessage !== Message) {
Channel.send(`New edit on the wikia:\n${Message}`);
LatestMessage = Message;
done = true;
}
}
});
}
});
await delay(1000)
done = false;
}
}
我在#logger 中的输出是:
New edit on the wikia:
Red Counter - edited by TomurA5 10 minutes ago. Edited: Added photo
New edit on the wikia:
Eijiro Kirishima - edited by TomurA5 12 minutes ago. Edited: Summary
无休止地重复,当我只应该得到:
New edit on the wikia:
Red Counter - edited by TomurA5 10 minutes ago. Edited: Added photo
我看不出我到底做错了什么。任何帮助将不胜感激。
编辑:
我更改了代码:
async function liveUpdating(Channel) {
var Message = "";
while(true) {
itemFound = false;
request('https://' + config.Logger.Subdomain + "." + activitylink, function (error, response, html) {
if(!error && response.statusCode === 200) {
var $ = cheerio.load(html);
var list = $('div[id="list"]');
var i = 0;
while(itemFound == false) {
i = i + 1;
var nextItem = list.children(i);
if($("strong a", nextItem).text().indexOf("User:") === -1) {
itemFound = true;
var newMessage = `New Edit on the Wikia:\n${$("strong a", nextItem).text()} - ${$("cite span", nextItem).text()}`;
if(newMessage !== Message) {
Channel.send(newMessage);
Message = newMessage;
}
}
}
}
});
await delay(1000);
}
}
现在我的输出是这样的:
New Edit on the Wikia:
-
看来问题是您有 while(true)
。根据定义,这将永远是正确的,这意味着它将 运行 无数次。
即使我已将代码设置为只允许它 运行 一次(完成 = 真),直到它再次循环该函数,即使我让它比较最后一个,它仍然循环通过两者。这是我的代码:
client.on('ready', () => {
console.log('Bot is ready!')
checkLiveUpdating();
});
function checkLiveUpdating() {
if(config.Server.AutoRefresh.Enabled == true) {
const guild = client.guilds.get(config.Server.GuildId);
if(guild) {
const channel = guild.channels.find("name", config.Server.AutoRefresh.ChannelName);
if(channel) {
liveUpdating(channel);
} else {
console.log("Valid channel not found! Please check your settings and the bot's permissions.");
}
} else {
console.log("Guild not found! Please check the id and make sure the bot is joined to the guild.");
}
}
}
async function liveUpdating(Channel) {
var LatestMessage = "";
while(true) {
request('https://' + config.Logger.Subdomain + "." + activitylink, function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('.activity-type-edit').each(function(i, element) {
if($("strong a", element).text().indexOf("User:") === -1 && done == false) {
var whatWasEdited = $("table tbody tr td em", element).text();
if(whatWasEdited !== "") {} else {whatWasEdited = "Page";}
var Message = $("strong a", element).text() + " - " + $("cite span", element).text() + ". Edited: " + whatWasEdited;
if(LatestMessage !== Message) {
Channel.send(`New edit on the wikia:\n${Message}`);
LatestMessage = Message;
done = true;
}
}
});
}
});
await delay(1000)
done = false;
}
}
我在#logger 中的输出是:
New edit on the wikia:
Red Counter - edited by TomurA5 10 minutes ago. Edited: Added photo
New edit on the wikia:
Eijiro Kirishima - edited by TomurA5 12 minutes ago. Edited: Summary
无休止地重复,当我只应该得到:
New edit on the wikia:
Red Counter - edited by TomurA5 10 minutes ago. Edited: Added photo
我看不出我到底做错了什么。任何帮助将不胜感激。
编辑:
我更改了代码:
async function liveUpdating(Channel) {
var Message = "";
while(true) {
itemFound = false;
request('https://' + config.Logger.Subdomain + "." + activitylink, function (error, response, html) {
if(!error && response.statusCode === 200) {
var $ = cheerio.load(html);
var list = $('div[id="list"]');
var i = 0;
while(itemFound == false) {
i = i + 1;
var nextItem = list.children(i);
if($("strong a", nextItem).text().indexOf("User:") === -1) {
itemFound = true;
var newMessage = `New Edit on the Wikia:\n${$("strong a", nextItem).text()} - ${$("cite span", nextItem).text()}`;
if(newMessage !== Message) {
Channel.send(newMessage);
Message = newMessage;
}
}
}
}
});
await delay(1000);
}
}
现在我的输出是这样的:
New Edit on the Wikia:
-
看来问题是您有 while(true)
。根据定义,这将永远是正确的,这意味着它将 运行 无数次。