'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 1:[1]
You can use Node's built-in process.uptime() function for this:
module.exports = {
name: "uptime",
aliases: [],
description: "Returns the bot's uptime in milliseconds",
/**
*
* @param {Client} client
* @param {Message} message
* @param {Guild} guild
*/
run: async (client, message, args, prefix, lang) => {
try {
// multiply process.uptime() return value by 1000, and round it
// to a whole number to get ms.
message.reply({
content: "Uptime: " + Math.round(process.uptime() * 1000) + "ms ?",
});
} catch {
console.log("rexom");
}
},
};
Edit (03/16/2021)
Since you asked in the comments for code that responds with a days/hours/minutes/seconds string as uptime instead of just the number of milliseconds like your code implied, here is how you would do that, though, in the future you should be more clear with your question:
function uptimeString(seconds) {
let days = Math.floor(seconds / (3600*24));
seconds -= days*3600*24;
let hours = Math.floor(seconds / 3600);
seconds -= hours*3600;
let minutes = Math.floor(seconds / 60);
seconds -= minutes*60;
return `${days} Days, ${hours} Hours, ${minutes} Minutes, and ${seconds} seconds`;
}
module.exports = {
name: "uptime",
aliases: [],
description: "Returns the bot's uptime in milliseconds",
/**
*
* @param {Client} client
* @param {Message} message
* @param {Guild} guild
*/
run: async (client, message, args, prefix, lang) => {
try {
// call the function defined above with process.uptime() as the
// parameter. Floor the parameter to round down.
message.reply({
content: uptimeString(Math.floor(process.uptime())),
});
} catch {
console.log("rexom");
}
},
};
This is what the response looks like in Discord:
My bot uses slash commands, but the code I provided will work with message.reply().
Solution 2:[2]
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!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | K.K Desgins |

