Bot timestamp getting out of sync over time

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

I’ve seen this exact issue before. Your recursive setTimeout is the culprit. When network operations take longer than expected, they don’t pause your timer - they just pile delay on top of it. So if a ping takes 3 seconds instead of milliseconds, your next cycle starts 3 seconds late. This builds up over time. Switch to setInterval instead of setTimeout in the finally block. setInterval keeps a consistent schedule no matter how long each operation takes. Replace that setTimeout line with setInterval and move it outside the function to initialize once. This way Discord gets the actual current time with each .setTimestamp() call instead of a progressively delayed version. I made this change on my server monitoring bot 6 months ago and haven’t had timing drift since.

yeah, same thing happened to me. your ping operations are async, but setTimeout doesn’t wait for them to complete. so if ping takes 2 seconds, your next cycle starts 2 seconds behind schedule because the timer already began counting. use setInterval instead, or better yet, grab a proper scheduler like node-cron rather than recursive timeouts.

The drift happens because setTimestamp() grabs the time when you create the embed, not when it actually gets sent. Your ping operations are async and take different amounts of time, so there’s this growing gap between when you think you’re checking and when the timestamp gets set. I hit the same issue with a monitoring setup. Fixed it by moving setTimestamp() right before the message send/edit operations instead of during embed creation. Now the timestamp shows when the status actually updates, not when the ping started. Network latency and Discord’s API response times make it even worse during peak hours.