Get Uptime of Discord.JS bot
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`;
Much better solution
const moment = require("moment");
require("moment-duration-format");
const duration = moment.duration(client.uptime).format(" D [days], H [hrs], m [mins], s [secs]");
console.log(duration);
//Output = 1 hr, 16 mins, 8 secs