I built a Discord bot that monitors server status every 5 seconds and updates an embed message. The timestamp shows the current time when the check happens. Everything works fine at first, but after running for a while, the timestamp starts falling behind. First, it’s off by a minute or two, then eventually it can be hours behind the actual time. I think the 5-second interval might be causing this drift somehow. Has anyone else run into this kind of timing issue?
const ping = require('net-ping');
const { EmbedBuilder } = require('discord.js');
let lastMessageId = null;
module.exports = {
monitorServer: async (bot) => {
try {
const target = {
hostname: "example.org",
address: "example.org",
port: 443
};
ping.createSession().pingHost(target.address, function(error, host) {
if (error) {
console.log(`Ping failed: ${error}`);
return;
}
const isOnline = !error;
const statusIcon = isOnline ? "✅ Active" : "❌ Down";
const embedColor = isOnline ? "#32CD32" : "#DC143C";
const statusText = isOnline ? `${target.hostname} is responding normally.` : `${target.hostname} is not responding.`;
const statusEmbed = new EmbedBuilder()
.setTitle(`Server Monitor - ${target.hostname}`)
.setDescription(`${statusText}\n\nCurrent Status: ${statusIcon}\n\u200B`)
.setThumbnail('https://i.imgur.com/sample.png')
.setTimestamp()
.setColor(embedColor);
if (!lastMessageId) {
bot.channels.cache.get('9876543210123456789').send({ embeds: [statusEmbed] })
.then(msg => {
lastMessageId = msg.id;
});
} else {
bot.channels.cache.get('9876543210123456789').messages.fetch(lastMessageId)
.then(msg => {
msg.edit({ embeds: [statusEmbed] });
})
.catch(err => console.error(err));
}
});
} catch (err) {
console.error(`Monitor error: ${err}`);
} finally {
setTimeout(() => module.exports.monitorServer(bot), 5000);
}
}
};