【发布时间】:2020-05-06 23:39:08
【问题描述】:
我想知道如何为单个命令而不是所有其他命令进行命令冷却?如果有人帮助我解决这个问题,将不胜感激,在此先感谢您。
【问题讨论】:
标签: bots discord discord.js
我想知道如何为单个命令而不是所有其他命令进行命令冷却?如果有人帮助我解决这个问题,将不胜感激,在此先感谢您。
【问题讨论】:
标签: bots discord discord.js
您将必须创建一个Collection(),其中将包含已执行命令的用户,然后有一个client.setTimeout() 函数在设定的时间后从Collection() 中删除用户,以便他们可以使用再次命令。
这是来自this guide的示例:
const cooldowns = new Discord.Collection();
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (timestamps.has(message.author.id)) {
// ...
}
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
}
}
【讨论】: