简体   繁体   中英

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())
        }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM