好的,我希望这样做,以便冷却时间显示用户需要等待多长时间才能再次工作。冷静的工作,但我希望它显示时间保持不变,而不是说您需要等待15分钟才能输入此命令。可能吗?
const { RichEmbed } = require("discord.js");
const { stripIndents } = require("common-tags");
const { prefix } = require("../../botconfig.json");
const db = require('quick.db')
let bal = require("../../database/balance.json");
let works = require('../../database/works.json');
const fs = require('fs');
const talkedRecently = new Set();
//Set cooldown
module.exports = {
name: "work",
aliases: [],
category: "economy",
description: "Gets you money",
usage: "[command | alias]",
run: async (client, message, args) => {
if (talkedRecently.has(message.author.id)) {
message.channel.send("You have to wait TIME minutes before you can work again")
} else {
if(!bal[message.author.id]){
bal[message.author.id] = {
balance: 0
};
}
if(!works[message.author.id]) {
works[message.author.id] = {
work: 0
};
}
const Jwork = require('../../work.json');
const JworkR = Jwork[Math.floor(Math.random() * Jwork.length)];
var random = Math.floor(Math.random() * 20) + 3;
let curBal = bal[message.author.id].balance
bal[message.author.id].balance = curBal + random;
let curWork = works[message.author.id].work
works[message.author.id].work = curWork + 1;
fs.writeFile('././database/works.json', JSON.stringify(works, null, 2), (err) => {
if (err) console.log(err)
})
fs.writeFile('././database/balance.json', JSON.stringify(bal, null, 2), (err) => {
let embed = new RichEmbed()
.setColor("RANDOM")
.setDescription(`
**\? | ${message.author.username}**, ${JworkR} ? **${random}**
`)
message.channel.send(embed)
if (err) console.log(err)
});
// Adds the user to the set so that they can't talk for a minute
talkedRecently.add(message.author.id);
setTimeout(() => {
// Removes the user from the set after a minute
talkedRecently.delete(message.author.id);
}, 900000);
}
}
}
答案 0 :(得分:1)
不幸的是,您当前的系统没有任何帮助。如果要使用其冷却时间,则不仅需要存储用户,还可以存储更多的信息。
让我们使用Map作为我们的变量,以便我们可以拥有键值对。这将使跟踪所需信息变得更加容易
// Replace talkedRecently's declaration with this...
const cooldowns = new Map();
要使用户处于冷却状态,请使用Map.set()
添加用户以及其冷却时间应过期到cooldowns
的时间。然后,当冷却时间用尽时,请使用Map.delete()
,以允许用户再次访问该命令。
// Replace the talkedRecently.add(...) section with this...
cooldowns.set(message.author.id, Date.now() + 900000);
setTimeout(() => cooldowns.delete(message.author.id), 900000);
为了确定冷却时间,我们必须从冷却时间中减去当前时间。但是,这将给我们几毫秒的时间,使我们无法读取该值。将持续时间转换为单词的一种简单方法是使用humanize-duration
((moment
也可以选择))。最后,我们可以发送所需的消息,让用户知道他们的冷却时间还剩下多少时间。
// Put this where you require your other dependencies...
const humanizeDuration = require('humanize-duration');
// Replace the if (talkedRecently.has(...)) part with this...
const cooldown = cooldowns.get(message.author.id);
if (cooldown) {
const remaining = humanizeDuration(cooldown - Date.now());
return message.channel.send(`You have to wait ${remaining} before you can work again`)
.catch(console.error);
}