Bot timestamp getting out of sync with repeated status checks

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);
        }
    }
};

I’ve hit this same issue with production monitoring systems. Your timing’s off because you’re not accounting for how long each operation actually takes. Ping operations have unpredictable latency, and Discord API calls can take anywhere from milliseconds to several seconds depending on rate limits and server load.

What fixed it for me: use setInterval with Date.now() checks to keep intervals consistent no matter how long individual operations take. Also consider splitting your ping logic from the update logic - run pings independently, cache the results, then use a separate timer just for updating the embed. This way network delays won’t mess with your timing.

Watch out for Discord rate limits too - they’ll add delays that stack up over time.

totally agree! using setInterval instead of setTimeout might help keep the timing more accurate. also, using a lib like moment.js for timestamps could really help with that drift issue you’re facing. good luck!

Your drift is happening because you’re using setTimeout in the finally block after async ping operations. Each ping takes different amounts of time, and setTimeout only starts counting after the ping finishes - so you’re not getting true 5-second intervals. The delays keep adding up.

I ran into this same problem with a monitoring bot last year. Fixed it by switching to setInterval for the main loop and handling pings separately. Discord’s API also adds its own delays when editing messages, especially during busy periods. Try adding some logging to see how long each ping actually takes - bet you’ll find they’re all over the place, which is causing your drift.