1

I made a 5 minute(300 seconds) cooldown for my discord.js bot, but when someone uses it more than once in under 5 minutes, it sends something like: @Edward, please wait 250.32 second(s) until you can use this command! Is there any way to change 250.32 seconds to something like 4 minutes 10 seconds or something close to that? I'm a noob at Node.js so help would be greatly appreciated.

if (!cooldowns.has(command)) {
cooldowns.set(command, new Discord.Collection());
}

const now = Date.now();
const timestamps = cooldowns.get(command);
const cooldownAmount = 1 * 300 * 1000;

if (timestamps.has(message.author.id)) {
  const expirationTime = timestamps.get(message.author.id) + cooldownAmount;

  if (now < expirationTime) {
    const timeLeft = (expirationTime - now) / 1000;
    return message.reply(`Please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command}\` command.`);
  }
}

timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
Inlowik
  • 59
  • 6

1 Answers1

1

Read this discussion.

Basically you need to divide seconds with 60 to get minutes and then you use % to get the reminder:

    const cooldownAmount = 1 * 275.45 * 1000;
const timeLeft = cooldownAmount / 1000;

var quotient = Math.floor(timeLeft / 60); //minutes
var remainder = Math.floor(timeLeft % 60); //seconds

console.log(quotient); // 4
console.log(remainder); // 35

This code should work in your case:

    if (!cooldowns.has(command)) {
  cooldowns.set(command, new Discord.Collection());
  }

  const now = Date.now();
  const timestamps = cooldowns.get(command);
  const cooldownAmount = 1 * 300 * 1000;

  if (timestamps.has(message.author.id)) {
    const expirationTime = timestamps.get(message.author.id) + cooldownAmount;

    if (now < expirationTime) {
      const timeLeft = (expirationTime - now) / 1000;
      var quotient = Math.floor(timeLeft / 60); //minutes
      var remainder = Math.floor(timeLeft % 60); //seconds
      return message.reply(`Please wait ${quotient} minutes and ${remainder} seconds before reusing the \`${command}\` command.`);
    }
  }

  timestamps.set(message.author.id, now);
  setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
anzepintar
  • 126
  • 1
  • 10