Delay in timestamps for Discord bot status updates

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

I’ve encountered similar challenges before, and a hybrid approach worked for me. Besides adjusting the intervals, ensure your bot is running on a reliable platform with enough resources, as underpowered hardware can exacerbate timing issues. Another point is to avoid blocking I/O operations since they can delay subsequent process executions. Consider implementing logic to reduce the frequency of updates during off-peak hours, which can help balance the load. Additionally, API rate limits from Discord could contribute, so verify those constraints as well.

It seems likely that the delay is due to the accumulation of asynchronous execution combined with the setTimeout function not accounting for the time taken by each process. Instead of using setTimeout, consider using setInterval to ensure the next function call happens exactly after the defined interval, minimizing drifts over time. Furthermore, check for memory leaks or performance issues in updating messages, which may also contribute to delays. Monitoring and profiling your application may help pinpoint the exact source of the lag.

Also, try comparing the timestamps in ur bot code with server time to detect drift issues. Make sure the server clock is nt drifting. Also think of lightening up your code a bit, maybe switch to a stateles real-time communication service lie websockets.