【问题标题】:How to make a cooldown for a specific user for all commands (Discord.js)如何为所有命令的特定用户进行冷却(Discord.js)
【发布时间】:2020-11-30 12:38:05
【问题描述】:

一个用户正在向我的命令发送垃圾邮件,我该如何让所有命令对该特定用户具有相同的冷却时间?

这是我当前的代码

if (talkedRecently.has(msg.author.id)) {
            msg.channel.send("Wait before getting typing this again. - " + msg.author);
    } else {

           message
channel.send('Cooldown over')

        
        talkedRecently.add(msg.author.id);
        setTimeout(() => {
          
          talkedRecently.delete(msg.author.id);
        }, 6000);
    }

它不起作用,因为只有 1 个命令,我希望该特定用户的所有命令冷却。

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    您可以创建一个函数以在用户运行以下命令时执行:

    
    var usersOnCooldown = {};
    
    function setCooldown(userId){
        usersOnCooldown[userId] = true;
        setTimeout(() => {
            usersOnCooldown[userId] = false;
        }, 5000);
    }
    
    function checkUserCooldown(userId){
        return !(userId in usersOnCooldown) || !usersOnCooldown[userId];
    }
    

    您可以在每个命令之后调用setCooldown()。在每个命令之前checkUserCooldown()

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 2021-06-17
      • 1970-01-01
      • 2018-07-04
      • 2021-02-11
      • 2021-02-16
      • 2020-10-11
      • 2021-10-10
      • 1970-01-01
      相关资源
      最近更新 更多