Discord.js 命令冷却 + 剩余时间
Discord.js Command Cooldown + Time Remaining
好的,所以我正在努力让冷却时间显示用户需要等待多长时间才能再次工作。冷却有效,但我希望它显示剩余时间,而不是说您需要等待 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);
}
}
}
很遗憾,您当前的系统不会有任何帮助。如果您想使用他们的冷却时间,您必须存储的不仅仅是用户。
让我们为变量使用 Map,这样我们就可以拥有键值对。这将使我们更容易跟踪我们需要的信息
// Replace talkedRecently's declaration with this...
const cooldowns = new Map();
要让用户进入冷却时间,请在冷却时间应该 运行 结束时使用 Map.set()
to add the user and the time at which their cooldown should expire to cooldowns
. Then, use 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);
}
好的,所以我正在努力让冷却时间显示用户需要等待多长时间才能再次工作。冷却有效,但我希望它显示剩余时间,而不是说您需要等待 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);
}
}
}
很遗憾,您当前的系统不会有任何帮助。如果您想使用他们的冷却时间,您必须存储的不仅仅是用户。
让我们为变量使用 Map,这样我们就可以拥有键值对。这将使我们更容易跟踪我们需要的信息
// Replace talkedRecently's declaration with this...
const cooldowns = new Map();
要让用户进入冷却时间,请在冷却时间应该 运行 结束时使用 Map.set()
to add the user and the time at which their cooldown should expire to cooldowns
. Then, use 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);
}