javascript - Discord.js - 每个用户而非所有用户的命令冷却时间

标签 javascript node.js discord discord.js

我正在开发一个 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/

相关文章:

javascript - 淡出元素然后淡入所选

javascript - 如何让机器人在 react 后发送消息到另一个 channel | Discord.js

node.js - User.findOrCreate 不是函数 Passport-facebook

node.js - 添加 webpack 时 react 项目依赖错误

node.js - 图像未保存在 Palm 设备上

java - JDA 语音 Activity 跟踪

javascript - 如何为发送消息的人添加角色(discord JS)

php - 如何将来自不同文件的返回值包含在变量中

Javascript console.log 返回 c() 而不是对象的值

javascript - 加载文件包含使用文档就绪功能不起作用