Search code examples
javascriptnode.jsdiscord.js

Discord Embed Command Cooldown


im trying to create a cooldown for my status command, because they spam a lot on the server the status command and i want a cooldown like 5m and that send a message like, "you need to wait 5m more to use this command im really newbie to javascript, so if anyone can help me

Here its my entire command:


const Discord = require("discord.js");
const yaml = require("js-yaml");

const supportbot = yaml.load(
  fs.readFileSync("./Configs/supportbot.yml", "utf8")
);
const cmdconfig = yaml.load(fs.readFileSync("./Configs/commands.yml", "utf8"));

const Command = require("../Structures/Command.js");
const { timeStamp } = require("console");
var request = require('request');

const Gamedig = require('gamedig');

let state = null;


setInterval(() => {


  Gamedig.query({
    type: 'minecraft',
    host: 'mc.latinplay.net',
    port: '25565'
  })
  .then((updatedState) => {
    state = updatedState;
    players = state.players.length;
}).catch((error) => {
  console.log("Server is offline");
});
}, 6000);

module.exports = new Command({
  name: cmdconfig.EstadoCommand,
  description: cmdconfig.EstadoCommandDesc,


  async run(interaction) {

      const LatinEstado = new Discord.MessageEmbed() 
    .setColor('RANDOM') 
      .setTitle('π‹πšπ­π’π§ππ₯𝐚𝐲 𝐍𝐞𝐭𝐰𝐨𝐫𝐀 **Estado del Servidor**')
      .setURL("https://store.latinplay.net/")
      .addField('**Jugadores en linea:**', `${players || "0"}`) 
      .addField('**Estado Actual:**', "**En LineaπŸ’š**", true)
      .setThumbnail('https://media0.giphy.com/media/5RQ6coK6GVXGfPPq69/giphy.gif?cid=790b76115867f0dba05d9cf2aceb80efed5b0e494387e3b2&rid=giphy.gif&ct=g')
      .setFooter({ text: 'LatinBot | Version 1.0 ', iconURL: 'https://cdn.discordapp.com/attachments/953043417234026547/988805760458821642/Diseno_sin_titulo.png' });
      interaction.reply({
        embeds: [LatinEstado],
      });
  
    }
  },
);```

Solution

  • Just store last command run time in a map.

    const cooldown={}; // Add this
    

    ...

    module.exports = new Command({
        name: cmdconfig.EstadoCommand,
        description: cmdconfig.EstadoCommandDesc,
        async run(interaction) {
            //
            if (interaction.user.id in cooldown && cooldown[interaction.user.id] - Date.now() > 0) // in cool down
                return interaction.reply("In cooldown! Please wait 5 min!");
            cooldown[interaction.user.id] = Date.now() + 300000; // 5 min in ms.
            //
            const LatinEstado = new Discord.MessageEmbed()
                .setColor('RANDOM')
                .setTitle('π‹πšπ­π’π§ππ₯𝐚𝐲 𝐍𝐞𝐭𝐰𝐨𝐫𝐀 **Estado del Servidor**')
                .setURL("https://store.latinplay.net/")
                .addField('**Jugadores en linea:**', `${players || "0"}`)
                .addField('**Estado Actual:**', "**En LineaπŸ’š**", true)
                .setThumbnail('https://media0.giphy.com/media/5RQ6coK6GVXGfPPq69/giphy.gif?cid=790b76115867f0dba05d9cf2aceb80efed5b0e494387e3b2&rid=giphy.gif&ct=g')
                .setFooter({
                    text: 'LatinBot | Version 1.0 ',
                    iconURL: 'https://cdn.discordapp.com/attachments/953043417234026547/988805760458821642/Diseno_sin_titulo.png'
                });
            interaction.reply({
                embeds: [LatinEstado],
            });
    
        }
    });