Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

395
Visualizações
How can I add a cooldown on work command (economy, currency)

So im trying to make an economy discord bot but I don't know how to make a 3 hour cooldown on the work command after the user uses it so for the does that don't understand

me: !work

bot: you got 123 money while working.

me: !work

bot: you have to wait 3 hours before working again.

This is my code for the work command

  if (message.content.toLowerCase().startsWith(prefixS.prefix + 'work')) {
    const inventoryS = await inventory.findOne({ userID: message.author.id, guildID: message.guild.id });
    const payment = Math.floor(Math.random() * 200);
    inventoryS.work = parseInt(payment)
    inventoryS.save()
    message.channel.send({ embeds: [new Discord.MessageEmbed().setAuthor(message.author.username).setTitle('⚒️⚒️').setColor('BLUE')] }).then((message) => {
      setTimeout(function () {
        function doRandHT() {
          var rand = [`You worked an extra night and got ${inventoryS.work}`, `You worked an extra day and got ${inventoryS.work}`, `Your boss just gave you ${inventoryS.work} for just sitting in your chair`];

          return rand[Math.floor(Math.random() * rand.length)];
        }
        message.edit({ embeds: [new Discord.MessageEmbed().setTitle(doRandHT()).setColor('BLUE')] }).then(() => {
          inventoryS.currency = parseInt(inventoryS.currency) + parseInt(inventoryS.work)
          inventoryS.save()
            }
          });
        })

      }, 3000)
    })
  }

Thank you

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is the guide for cooldown on discord.js v12, is kind of similar if you are using v13.

In the command file:

module.exports = {
name: 'example',
cooldown: 5,
execute(message) {
    // ...
},
};

In the main file:

client.cooldowns = new Discord.Collection();
const { cooldowns } = client;

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

const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 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.name}\` command.`);
    }
}

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

Make sure execute this code before execute the command.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda