javascriptnode.jsdiscorddiscord.jsuptime

Bot Uptime Discord.JS


I'm trying to get the uptime of my bot. Every time I run it whether on my desktop or on Heroku, all it gives me is "0" for all time formats.

const Discord = require('discord.js');
const moment = require("moment");
const bot = new Discord.Client();
require("moment-duration-format");
module.exports = {
    name: 'stats',
    description: "Bot Stats",
    execute(message, args){ 
        const duration = moment.duration(bot.uptime).format(" D [days], H [hrs], m [mins], s [secs]");
        const statEmbed = new Discord.RichEmbed()
            .setTitle("**  = STATISTICS =**")
            .addField("**Mem Usage  ::**", `**${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)} MB**`)
            .addField("**Uptime**", `**${duration}**`);
        message.channel.send(statEmbed);
    }
}

Solution

  • Why you are create new Discord client in non main file? 1 token - 1 client

    You need run your command execute with (message, args, bot) and then you will got right uptime.

    Your bot arg has undefined property of bot.uptime, because you not loggin in with that "new" Client.