寻找正常运行时间命令

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");
    }
  },
};

这就是我获得 ping 命令的方式,我正在寻找正常运行时间命令有人可以帮忙吗?
所以我测试了很多例子,它在我的项目中不起作用,我正在寻找有人提供给我或给我与 ping 代码的制作方式相关的代码,因为这将使命令工作:)

您可以为此使用 Node 的 built-in process.uptime() 函数:


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");
    }
  },
};

编辑 (03/16/2021)

由于您在评论中询问了使用 days/hours/minutes/seconds 字符串作为正常运行时间响应的代码,而不是像您的代码所暗示的那样只是毫秒数,所以这里是您将来如何做的你应该更清楚你的问题:


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");
    }
  },
};

这是 Discord 中的响应:

我的机器人使用斜杠命令,但我提供的代码可以与 message.reply() 一起使用。

您可以使用 client.uptime 这样做。它 returns 自客户端上次进入 READY 状态以来已经过了多长时间(以毫秒为单位)。请参阅下面的代码以了解其工作原理:

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}`);

上面的代码使其成为human-readable格式。

希望对您有所帮助!