Discord.js-每个用户(不是所有用户)的命令冷却

usa*_*ber 2 javascript node.js discord discord.js

我正在开发discord.js机器人,并且想对命令进行冷却。

我在Google上看到了很多有关如何执行此操作的教程,但是所有这些教程都是针对所有命令执行的(因此,当用户键入!mycmd时,所有用户都必须等待X分钟/秒才能再次键入它)。

但我想为每个用户执行此操作(当用户键入!mycmd时,仅此用户必须等待X分钟/秒,直到该用户可以再次键入它)。

可能吗?

谢谢!

Mat*_*w08 5

是的,这很容易而且可能。

在您的JS文件的顶部添加此:

// First, this must be at the top level of your code, **NOT** in any event!
const talkedRecently = new Set();
Run Code Online (Sandbox Code Playgroud)

现在在命令事件中添加以下内容:

    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);
    }
Run Code Online (Sandbox Code Playgroud)