I built a Discord bot that monitors server uptime and updates an embed message every 5 seconds. The bot checks if a website is reachable and shows the current timestamp. Everything works fine at first, but after running for a while, I notice the timestamp starts drifting. It begins with small delays of a minute or two, but eventually becomes hours behind the actual time. I suspect this timing issue is related to my 5-second interval checks somehow accumulating delays.
const ping = require('net-ping');
const { EmbedBuilder } = require('discord.js');
let trackedMessage = null;
module.exports = {
monitorSite: async (botClient) => {
try {
const targetSite = {
hostname: "example-site",
address: "example.org",
portNum: 443
};
ping.createSession().pingHost(targetSite.address, function(error, target) {
if (error) {
console.log('Ping failed:', error);
return;
}
const isUp = !error;
const statusIcon = isUp ? "✅ Active" : "❌ Down";
const embedColor = isUp ? "#32CD32" : "#DC143C";
const statusText = isUp ? `${targetSite.address} is responding normally.` : `${targetSite.address} is not responding.`;
const statusEmbed = new EmbedBuilder()
.setTitle(`Monitor: ${targetSite.hostname}`)
.setDescription(`${statusText}\n\nCurrent Status: ${statusIcon}\n\u200B`)
.setThumbnail('https://i.imgur.com/sample.png')
.setTimestamp()
.setColor(embedColor);
if (!trackedMessage) {
botClient.channels.cache.get('9876543210987654321').send({ embeds: [statusEmbed] })
.then(msg => {
trackedMessage = msg.id;
});
} else {
botClient.channels.cache.get('9876543210987654321').messages.fetch(trackedMessage)
.then(msg => {
msg.edit({ embeds: [statusEmbed] });
})
.catch(err => console.error(err));
}
});
} catch (err) {
console.error('Site monitoring error:', err);
} finally {
setTimeout(() => module.exports.monitorSite(botClient), 5000);
}
}
};