[英]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.
每次我在桌面或 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);
}
}
Sorry i dont know the issue either, maybe its a problem with your "moment-duration-format"?对不起,我也不知道这个问题,也许是你的“时刻持续时间格式”有问题? I never worked with it.
我从未与它合作过。
I calculated the uptime of my bot like this, hopefully it helps you我像这样计算了我的机器人的正常运行时间,希望对您有所帮助
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;
Why you are create new Discord client in non main file?为什么要在非主文件中创建新的 Discord 客户端? 1 token - 1 client
1 个令牌 - 1 个客户端
You need run your command execute with (message, args, bot)
and then you will got right uptime.您需要使用
(message, args, bot)
运行您的命令,然后您将获得正确的正常运行时间。
Your bot arg has
undefined
property ofbot.uptime
, because you not loggin in with that "new" Client.您的 bot arg 具有
bot.uptime
undefined
属性,因为您没有使用该“新”客户端bot.uptime
。
Here is how I did it for my bot:这是我为我的机器人所做的:
I used the client.uptime()
module.我使用了
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.