javascript - 显示命令的剩余时间
问题描述
所以我得到了这个代码:
const Discord = require("discord.js");
const config = require("../config.json");
const talkedRecently = new Set();
let coins = require("../coins.json");
module.exports.run = async (bot, message, args) => {
if (message.channel.id === '669657436789014550') {
if (message.content.indexOf(config.prefix) !== 0) return;
var minute = 60000;
if (talkedRecently.has(message.author.id)) {
message.channel.send(`Ai fost deja într-o explorare. Mai ai de așteptat ` + Math.round((talkedRecently[message.author.id] - Date.now)/minute) + ` minute până să poți merge din nou.`);
} else {
if(!coins[message.author.id]){
coins[message.author.id] = {
coins: 0
};
}
let coinAmt = Math.floor(Math.random() * 450) + 1;
let sCoins = coins[message.author.id].coins;
var caut = [ //something
];
coins[message.author.id] = {
coins: coins[message.author.id].coins + coinAmt
};
message.channel.send(`**${message.author.tag}**,` + (caut[Math.floor(Math.random() * caut.length)]))
.then(() => {
console.log(`Hopa! Cuiva îi place să meargă-n explorări!`);
})
}
talkedRecently.add(message.author.id);
setTimeout(() => {
talkedRecently.delete(message.author.id);
}, 18000000);
}
}
module.exports.help = {
name: 'caut',
aliases: []
};
问题是它没有显示用户还有多少剩余时间,它显示的是“NaN”而不是分钟。有人知道我该如何解决这个问题吗?它不会在控制台中显示任何错误。我想我必须在这里提到这一点。
解决方案
它将是,因为除了作者的 ID 之外NaN
,您没有向您的任何内容添加任何内容。Set
我的建议是使用Map,而不是添加包含 id 和时间戳的对象:
const talkedRecently = new Map();
if (talkedRecently.has(message.author.id)) {
// talkedRecently.get(message.author.id) returns the Date object
}
talkedRecently.set(message.author.id, Date.now());
推荐阅读
- java - 为什么这两段 Java 代码会产生 2 个不同的结果?
- docker - 使用 Quarkus,我如何在 docker 镜像中构建原生镜像(使用 Kotlin 和 Gradle)
- python - 从网站中提取数据
- c# - 如何从 HTML 插入 dan 更新值
- excel - 如何编写一个循环来在excel中复制和粘贴多行?
- python - 可以对具有无效模块/包 Python 名称的文件进行 pytest 测试吗?
- python - 如何在 python 中读取差异文件,提取添加的行并删除它们(更改为旧补丁)?
- c - strncpy 的分段错误
- nginx - 如何在 lua nginx 中解码 json 字符串?
- swift - Swift - 在没有 ViewController 的情况下以编程方式 pushViewController