Add cooldowns to slash commands

Comentarios

9 comentarios

  • 1wascreated

    Yes, this would be useful to protect the server against spamming and to some extent protection against fake links. 

    2
  • Michael627

    So i can only run it once every 10 seconds. So i'm going to go into my code here. And i'm going to add another property this will be called cooldown. And i'm going to say 10.

    DGCustomerFirst.com

    0
  • dgmode

    Here is the code which I used and it worked:

    First you have to create a Set

    const cooldown = new Set();
    ///This is 1 minute, you can change it to whatever value
    const cooldownTime = 60000; 
    Then this is the code:

    if (cooldown.has(interaction.user.id)) {
        /// If the cooldown did not end
        interaction.reply({ content: "Please wait for the cooldown to end", ephemeral: true });
        
      } else {
        /// Else give the reply
        interaction.reply({ content: "What is this uwu", ephemeral: true });

        //now, set cooldown
       cooldown.add(interaction.user.id);
            setTimeout(() => {
              // Removes the user from the set after 1 minute
              cooldown.delete(interaction.user.id);
            }, cooldownTime);
        }

    DGCustomerfirst.com

    0
  • myaccountaccess

     

    First, create a discord.js collection to store users' cooldown and set cooldown time.

    // index.js
    client.cooldowns = new Discord.Collection();
    client.COOLDOWN_SECONDS = 10; // replace with desired cooldown time in seconds
    

    When a user executes a command, check if the user exists in the cooldown. If so, notify it to the user. If not, run the comnand and set cooldown. After the time you specified, remove it.

    // command file
    async run(client, interaction, message) {
      if (client.cooldowns.has(interaction.user.id)) {
        // cooldown not ended
        interaction.reply({ content: "Please wait for cooldown to end", ephemeral: true });
      } else {
        // no cooldown
        // do something here
    
        //now, set cooldown
        client.cooldowns.set(interaction.user.id, true);
    
        // After the time you specified, remove the cooldown
        setTimeout(() => {
          client.cooldowns.delete(interaction.user.id);
        }, client.COOLDOWN_SECONDS * 1000);
      }
    }

    Myaccountaccess

     

    1
  • quickmode

     

    Here is the code which I used and it worked:

    First you have to create a Set

    const cooldown = new Set();
    ///This is 1 minute, you can change it to whatever value
    const cooldownTime = 60000; 
    

    Then this is the code:

    if (cooldown.has(interaction.user.id)) {
        /// If the cooldown did not end
        interaction.reply({ content: "Please wait for the cooldown to end", ephemeral: true });
        
      } else {
        /// Else give the reply
        interaction.reply({ content: "What is this uwu", ephemeral: true });
    
        //now, set cooldown
       cooldown.add(interaction.user.id);
            setTimeout(() => {
              // Removes the user from the set after 1 minute
              cooldown.delete(interaction.user.id);
            }, cooldownTime);
        }

    www.quickpayportal.com

     

    -1
  • myaarpmode

    Thank you. Your example didn't work for me but I managed to fix it, which was basically the same thing!

    MyAARPMedicare

    0
  • mycardpage

    I faced similar kind of issue last time, I am still searching for some proper solution Same issue still no fix to this.

    MyCardStatement

    0
  • tinallen

    After answering all the questions over there, while making the submission, you need to give all your contact details over there. MCDVOICE

    0
  • Clarkmark

    Thanks for the code, 

    It really going to be really nice feature.

    myvikingjourney

     

    0

Iniciar sesión para dejar un comentario.