Search code examples
javascriptnode.jsdiscorddiscord.js

Get Uptime of Discord.JS bot


I am right now making a Discord bot command for runtime, I was wondering what the most compacted (and still correct) way of doing an runtime to catch how long the bot has actually been online and return it in 24hr format.


Solution

  • You don't need to manually save when the bot started. You can use client.uptime and you will get how many milliseconds the bot is up.

    From there you can do something like this:

    let totalSeconds = (client.uptime / 1000);
    let days = Math.floor(totalSeconds / 86400);
    totalSeconds %= 86400;
    let hours = Math.floor(totalSeconds / 3600);
    totalSeconds %= 3600;
    let minutes = Math.floor(totalSeconds / 60);
    let seconds = Math.floor(totalSeconds % 60);
    

    Then you'll have days, hours, minutes and seconds ready to use.

    let uptime = `${days} days, ${hours} hours, ${minutes} minutes and ${seconds} seconds`;