Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!