我正在开发一个 discord.js 机器人,我想为命令设置冷却时间。
我在 Google 上看到了很多关于如何执行此操作的教程,但是所有这些教程都是针对所有命令执行的(因此,当用户键入 !mycmd 时,所有用户都必须等待 X 分钟/秒,直到可以键入它再次)。
但我想为每个用户都这样做(当用户键入 !mycmd 时,只有该用户必须等待 X 分钟/秒,直到用户可以再次键入它)。
这可能吗?
谢谢!
最佳答案
是的,这既简单又可能。
在你的 JS 文件的顶部添加:
// First, this must be at the top level of your code, **NOT** in any event!
const talkedRecently = new Set();
现在在命令事件中添加:
if (talkedRecently.has(msg.author.id)) {
msg.channel.send("Wait 1 minute before getting typing this again. - " + msg.author);
} else {
// the user can type the command ... your command code goes here :)
// Adds the user to the set so that they can't talk for a minute
talkedRecently.add(msg.author.id);
setTimeout(() => {
// Removes the user from the set after a minute
talkedRecently.delete(msg.author.id);
}, 60000);
}
关于javascript - Discord.js - 每个用户而非所有用户的命令冷却时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48432102/