#Message Cooldown

20 messages · Page 1 of 1 (latest)

naive ice

having issues with the following code:

const cooldowns = new Map();

module.exports = {
  name: 'slots',
  description: 'slot machine',
  async execute(message) {
    const cooldownTime = 10 * 1000; 

    if (cooldowns.has(message.author.id)) {
      const remainingTime = (cooldowns.get(message.author.id) - Date.now()) / 1000;
      return message.channel.send(`Try again in ${remainingTime.toFixed(1)} seconds.`);
    }

    const slot1 = Math.floor(Math.random() * 10) + 1;
    const slot2 = Math.floor(Math.random() * 10) + 1;
    const slot3 = Math.floor(Math.random() * 10) + 1;

    const isWin = (slot1 === slot2) && (slot2 === slot3);
    const userID = message.author.id;

    const winStatus = isWin ? 1 : 0;

    if (winStatus === 1) {
      message.channel.send(`# ${slot1} - ${slot2} - ${slot3} | YOU WIN, <@${userID}>!`);
    } else {
      return message.channel.send(`## ${slot1} - ${slot2} - ${slot3}`);
    }

    const { EmbedBuilder } = require('discord.js');

    const channelID = '1238236155271446528';
    const username = message.author.username;

    const embed = new EmbedBuilder()
      .setColor(0x0000ff)
      .setTitle(`Timeout Ticket Won By <@${userID}>`)
      .setTimestamp()
      .setFooter({ text: username });

    const channel = message.guild.channels.cache.get(channelID);

    channel.send({ embeds: [embed] });


    cooldowns.set(message.author.id, Date.now() + cooldownTime);
    setTimeout(() => cooldowns.delete(message.author.id), cooldownTime);

  },
};
golden jungleBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
  • Marked as resolved by OP
naive ice

the cooldown worked in this screenshot:

so I really don't get what the problem is

the only difference is that I added the slot logic

visual belfry
naive ice

cooldown

the cooldown does not work

visual belfry

Great so we've gotten nowhere

naive ice

sorry I forgot to mention that

visual belfry

I understand the cooldown doesn't work, what part of it doesn't work

naive ice

nothing, I use the command multiple times rapidly without it stopping me, the code for the cooldown is the exact same as in the screenshot (where it worked), but it now just doesn't, as if it wasn't there

visual belfry
  1. Does the code run?
  2. Does cooldowns.set ever get called?
  3. Is the code saved?

I'm on mobile so that code block is awkward to look at

naive ice

I understand, the code does run, I do not know if cooldowns.set gets called, I have no idea how to even log that, sorry.

visual belfry

The same way you log anything?

Best thing to do to see the flow of your code is to add console.log(x) on each line where you can and replace x with incrementing numbers, e.g. your first one would be 1, then 2

naive ice

I did not ever think of that, thank you, that actually fixed my issue, I was able to see that it stopped reading the rest of the code because it ends where winStatus is checked

thank you.