Search code examples
javascriptnode.jsdiscorddiscord.js

Looking for uptime command


const { Client, Message, MessageEmbed, Permissions } = require("discord.js");
const emojis = require("../../config/emojis.json");
const db = require("quick.db");
const { player } = require("../index");
const embed = require("../structures/embeds");

module.exports = {
  name: "ping",
  aliases: [],
  description: "test",

  /**
   *
   * @param {Client} client
   * @param {Message} message
   * @param {Guild} guild
   */
  run: async (client, message, args, prefix, lang) => {
    try {
      message.reply({
        content: "Ping: " + client.ws.ping + "ms 📶",
      });
    } catch {
      console.log("rexom");
    }
  },
};

this is how i got my ping command, i am looking for a uptime command can someone help?
so i tested many examples it isn't working into my project, i am looking for someone provides me or gives me the code connected to the way how ping code is made because that will make the command to work :)


Solution

  • You can use client.uptime to do so. It returns how long it has been since the client last entered the READY state in milliseconds. See the code below to see how this works:

    const days = Math.floor(client.uptime / 86400000);
    const hours = Math.floor(client.uptime / 3600000) % 24;
    const minutes = Math.floor(client.uptime / 60000) % 60;
    const seconds = Math.floor(client.uptime / 1000) % 60;
    const uptime = `${days} days, ${hours} hours, ${minutes} minutes and ${seconds} seconds`;
    
    console.log(`The bot has been up for ${uptime}`);
    

    The code above makes it into a human-readable format.

    Hoped this helped!