I’m working on a Discord bot command that updates the website’s online status every five seconds, along with the current timestamp. However, I have noticed that after running for a while, the timestamps start to lag, sometimes by minutes and eventually by hours. I’m suspecting that the interval of five seconds for status checks might be causing this issue.
const tcpp = require('tcp-ping');
const { MessageEmbed } = require('discord.js');
let currentStatusMessage = null;
module.exports = {
verifyWebsite: async (bot) => {
try {
const siteInfo = {
name: "exampleSite",
url: "example.com",
port: 80
};
tcpp.probe(siteInfo.url, siteInfo.port, function(err, isAvailable) {
if (err) {
throw new Error(`Unable to check website: ${err}`);
}
const statusText = isAvailable ? "🟢 Active" : "🔴 Inactive";
const colorCode = isAvailable ? "#00FF00" : "#FF0000";
const messageDetail = isAvailable ? `The ${siteInfo.url} is operational.` : `The ${siteInfo.url} is down.`;
const embedMessage = new MessageEmbed()
.setTitle(`Current Status of ${siteInfo.name}`)
.setDescription(`${messageDetail}\n\nStatus: ${statusText}\n\u200B`)
.setThumbnail('https://i.imgur.com/AfFp7pu.png')
.setTimestamp()
.setColor(colorCode);
if (!currentStatusMessage) {
bot.channels.cache.get('1232582546932633610').send({ embeds: [embedMessage] })
.then(msg => {
currentStatusMessage = msg.id;
});
} else {
bot.channels.cache.get('1232582546932633610').messages.fetch(currentStatusMessage)
.then(msg => {
msg.edit({ embeds: [embedMessage] });
})
.catch(console.error);
}
});
} catch (error) {
console.error(`Error while verifying website status: ${error}`);
} finally {
setTimeout(() => module.exports.verifyWebsite(bot), 5000);
}
}
};