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

243
Visualizações
Discord.js command cooldowns with ms and quick.db

I want to set cooldown on the /rep command. But I have the problem. When user use command, the cooldown is correct. But when user run command secondly, the second cooldown is wrong, it is 52 years but it must be 12 hours. Why? There is my code. I have setted 15 seconds cooldown for testing, but it show 52 years from second cooldown and onwards. There isn't any error and console. Discord.js v13 and pretty-ms v7.0.1

const { SlashCommandBuilder } = require("@discordjs/builders");
const { MessageEmbed } = require("discord.js");
const db = require("quick.db");
const ms = require("pretty-ms");
const yaml = require('js-yaml');
const fs = require("fs");
const lang = yaml.load(fs.readFileSync('lang.yml', 'utf8'));

module.exports = {
    data: new SlashCommandBuilder()
        .setName('rep')
        .setDescription(lang.rep.cmdDesc)
        .addUserOption(option =>
            option.setName('user')
                .setDescription(lang.rep.optionDesc)
                .setRequired(true)),
    async execute(interaction) {
        let lastDaily = await db.fetch(`repCheck_${interaction.user.id}`);
        const cooldown = 15000;
        
        if (lastDaily !== null && cooldown - (Date.now() - lastDaily) > 0) {
            const timeLeftRep = ms(cooldown - (Date.now() - lastDaily))
            const timerr = new MessageEmbed()
            .setDescription(lang.rep.cooldownErr.replace('{{cooldownRep}}', timeLeftRep))
            .setColor('RED')
            await interaction.reply({ embeds: [timerr], ephemeral: true })
        } else if (interaction.options.getUser('user').bot) {
            const boterr = new MessageEmbed()
            .setDescription(lang.rep.mentionErr)
            .setColor('RED')
            await interaction.reply({ embeds: [boterr], ephemeral: true });
        } else if (interaction.options.getUser('user').id == interaction.user.id) {
            const selfmen = new MessageEmbed()
            .setDescription(lang.rep.selfMention)
            .setColor('RED')
            await interaction.reply({ embeds: [selfmen], ephemeral: true })
        } else {
            const member = interaction.options.getUser('user').id;
            let reputation = db.fetch(`rep_${member}`)
            db.add(`rep_${member}`, 1)
            const repembed = new MessageEmbed()
            .setDescription(lang.rep.successMsg.replace('{{user}}', interaction.options.getUser('user').username))
            await interaction.reply({embeds: [repembed]});
            db.add(`repCheck_${interaction.user.id}`, Date.now())
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As I can't comment. It might be because you put db.add() instead of db.set() in your code. It will add your last cooldown time with the current cooldown time that you just set. Which is will make it longer. You can try to change your code :

from

db.add(`repCheck_${interaction.user.id}`, Date.now());

to

db.set(`repCheck_${interaction.user.id}`, Date.now());

And it maybe work, tell me if its not.

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