【问题标题】:Is there a way to add a global cooldown to my commands so when one user uses the command, all users have to wait the specific time?有没有办法为我的命令添加全局冷却时间,这样当一个用户使用该命令时,所有用户都必须等待特定时间?
【发布时间】:2021-04-07 17:53:50
【问题描述】:

所以,我有一个专为我的 Discord 服务器设计的 Call All Staff 系统,但我想要它,所以当一个用户使用它时,每个用户必须等待 30 分钟才能再次使用它。 这是我的代码:

  var callallstaff = ['<@&791604815620603904>']

  if (callallstaff.some(w => ` ${message.content.toLowerCase()} `.includes(` ${w} `))) {

    var warn = new Discord.MessageEmbed()
      .setTitle('Call All Staff')
      .setDescription('Your about to mention all staff members of this server. \n \n **ONLY use for emergencies**, abusing this will result in a warn. \n \n Do you want to continue? Respond with \`yes`\ or \`no`\ ')
      .setFooter(`Requested by ${message.author.tag}`)
      .setColor('#ff0000')
    message.channel.send(warn)
    message.channel.awaitMessages(m => m.author.id == message.author.id, {max: 1, time: 30000}).then(collected => {
      if (collected.first().content.toLowerCase() == 'yes') {
        message.channel.send(`<@&757974725359042600> -Requested by ${message.author}`)
      }
      else {
        message.channel.send('Canceled!')
      }
    }).catch(() => {
      message.channel.send('Timed out!')
    })
  }

但是,我确实知道如何为一个用户设置冷却时间,以便只有该用户获得冷却时间,但我不确定如何设置它,所以所有用户都必须等待, 谢谢!

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    我会这样做:

    • 获取用户发送命令时当前时间的时间戳
    • 计算 30 分钟后的时间戳
    • 当用户尝试命令时,再次获取当前时间
    • 如果当前时间大于您计算的时间戳,则说明已经过了三十分钟

    现在,我并没有为此付出太多努力,但这里有一些可以帮助您进行开发的基本代码:

    let cooldown = 0; // define this somewhere above
    // call all staff command detected
    let currentTime = new Date(Date.now());
    if (currentTime.getTime() < cooldown) {
        message.reply("cooldown active, please try again later!");
    } else {
        // do commmand
        cooldown = currentTime.getTime() + (30 * 60 * 1000); // adds 30 mins to current time
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 2021-06-17
      • 2022-01-18
      • 2021-03-18
      • 1970-01-01
      相关资源
      最近更新 更多