Discord.js正常运行时间命令
在djs中编写一个命令,显示我的正常运行时间。当我运行它时,它会显示总秒数、分钟数、小时数等,但它不会将秒数、分钟数限制为秒,或小时数限制为24Discord.js正常运行时间命令,discord.js,Discord.js,在djs中编写一个命令,显示我的正常运行时间。当我运行它时,它会显示总秒数、分钟数、小时数等,但它不会将秒数、分钟数限制为秒,或小时数限制为24 const seconds = Math.floor(message.client.uptime / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.fl
const seconds = Math.floor(message.client.uptime / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if(command === "uptime") {
message.channel.send(`The bot has been up for` + ` ` + `${days} days,` + ` ` + `${hours} hours,` + ` ` + `${minutes} minutes` + ` ` + `${seconds} seconds.`)
return;
}
为什么部门会限制人数?5678000/1000将始终提供5678。您要做的是模(
%
)而不是除法(/
)
不过,有一个更好的解决方案。将数字转换为日期格式,并使用它直接获取分钟、小时、天等-唯一的缺点是它给出了一个月中的某一天,而不是从JS Epoch time开始经过的天数,因此(除非有更干净的解决方案,我不知道)您需要初始化另一个变量
另外,如果可以简单地传递带有参数的单个字符串,那么为什么要连接字符串呢
不管怎样,以下是我对您问题的解决方案:
var uptime = new Date(message.client.uptime);
const days = Math.floor(message.client.uptime / (60 * 1000 * 60 * 24));
if(command === "uptime") {
message.channel.send(`The bot has been up for ${days} days, ${uptime.getHours()} hours, ${uptime.getMinutes()} minutes ${uptime.getSeconds()} seconds.`)
return;
}
var uptime = new Date(message.client.uptime);
const days = Math.floor(message.client.uptime / (60 * 1000 * 60 * 24));
if(command === "uptime") {
message.channel.send(`The bot has been up for ${days} days, ${uptime.getHours()} hours, ${uptime.getMinutes()} minutes ${uptime.getSeconds()} seconds.`)
return;
}