【问题标题】:Discord.js - Cooldown for a command for each user not all usersDiscord.js - 每个用户的命令冷却时间,而不是所有用户
【发布时间】:2018-07-04 01:35:28
【问题描述】:

我正在开发一个 discord.js 机器人,我想为命令设置冷却时间。

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

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

有可能吗?

谢谢!

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

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

    在你的 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);
        }
    

    【讨论】:

    • 我也在搞冷却。这很有帮助,但我有一个问题。我有一个命令处理程序,我必须把他放在我的索引中还是命令本身中?或者可能在其他地方,比如消息事件?
    • 如果您想将违反冷却时间 3 次的人放到一个单独的列表中,您将如何调整此解决方案?
    • 我猜为什么这段代码对我不起作用。我正在使用 v13
    【解决方案2】:

    您可以使用包quick.db 以防您想跟踪冷却时间,即使在重新启动后也是如此。

      let cooldown = 43200000; // 12 hours in ms
    
      let lastDaily = await db.fetch(`daily_${message.author.id}`);
    
      if (lastDaily !== null && cooldown - (Date.now() - lastDaily) > 0) {
        // If user still has a cooldown
        let timeObj = ms(cooldown - (Date.now() - lastDaily)); // timeObj.hours = 12
    } else {
        // Otherwise they'll get their daily
      }
    

    【讨论】:

      【解决方案3】:

      您可以使用 wokcommands 或 discord.js-commando,它们是一个非常有用的包,可轻松进行命令和事件处理。它有一个内置的冷却时间,您可以使用它。每个用户的冷却时间和全局冷却时间。此外,突击队具有节流功能,例如限速。这就像允许用户使用命令 4 次或您输入的任何内容,然后执行冷却。

      如果你想在机器人重启时跟踪你的冷却时间,Wokcommands 支持 mongodb 并且有一个内置的冷却集合。

      https://www.npmjs.com/package/wokcommands

      https://discordjs.guide/commando/

      【讨论】:

      • Commando 最近已从 discord.js 中删除
      猜你喜欢
      • 2021-02-16
      • 2021-06-17
      • 2021-02-11
      • 1970-01-01
      • 2021-05-20
      • 2021-06-09
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多