简体   繁体   English

Discord.js 命令冷却时间与 ms 和 quick.db

[英]Discord.js command cooldowns with ms and quick.db

I want to set cooldown on the /rep command.我想在 /rep 命令上设置冷却时间。 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.但是当用户第二次运行命令时,第二次冷却时间是错误的,是52年但必须是12小时。 Why?为什么? There is my code.有我的代码。 I have setted 15 seconds cooldown for testing, but it show 52 years from second cooldown and onwards.我为测试设置了 15 秒的冷却时间,但它显示从第二个冷却时间起为 52 年。 There isn't any error and console.没有任何错误和控制台。 Discord.js v13 and pretty-ms v7.0.1 Discord.js v13 和 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.这可能是因为您在代码中放置db.add()而不是db.set() 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.它可能有效,告诉我是否无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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