[英]Bot Uptime Discord.JS
我正在尝试获得我的机器人的正常运行时间。 每次我在桌面或 Heroku 上运行它时,它给我的所有时间格式都是“0”。
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);
}
}
对不起,我也不知道这个问题,也许是你的“时刻持续时间格式”有问题? 我从未与它合作过。
我像这样计算了我的机器人的正常运行时间,希望对您有所帮助
let totalSeconds = (bot.uptime / 1000);
let days = Math.floor(totalSeconds / 86400);
let hours = Math.floor(totalSeconds / 3600);
totalSeconds %= 3600;
let minutes = Math.floor(totalSeconds / 60);
let seconds = totalSeconds % 60;
为什么要在非主文件中创建新的 Discord 客户端? 1 个令牌 - 1 个客户端
您需要使用(message, args, bot)
运行您的命令,然后您将获得正确的正常运行时间。
您的 bot arg 具有
bot.uptime
undefined
属性,因为您没有使用该“新”客户端bot.uptime
。
这是我为我的机器人所做的:
我使用了client.uptime()
模块。
const Discord = require("discord.js");
const client = new Discord.Client();
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ + /);
const command = args.shift().toLowerCase();
if (command === 'uptime') {
if (!message.content.startsWith(prefix) || message.author.bot) return;
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);
const embed = new Discord.MessageEmbed()
.setTitle(`Uptime`)
.addField("Days", `${days}`)
.addField("Hours", `${hours}`)
.addField("Minutes", `${minutes}`)
.addField("Seconds", `${seconds}`)
message.channel.send(embed);
}
});
client.login("YOUR TOKEN");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.