简体   繁体   中英

show time left on cooldown

I am looking to display the time left in the cooldown but I am not sure if it can be done using the format that I currently have. Is it possible? Or is there a better way to use a cooldown? I cut out most of the code surrounding this as it was not relevant.

let cooldown = new Set();
if(msgObject.content.startsWith("-eat")) {        

    let amount = Math.floor(Math.random() * 2500) + 1;
    let Channel = msgObject.channel.id

    if(Channel != `623195917083738136` && Channel != `623195981919289344` ){//bot test
        msgObject.channel.send('Cannot use command here, ' + msgObject.author)
            .then(msg => {
                (msg as Discord.Message).delete(20000)
                    .catch(console.error);
            })
        return;
    }

    if(cooldown.has(msgObject.author.id)){
        msgObject.channel.send(`You have to wait 3 hours to use this command again!`)
            .then(msg => {
                (msg as Discord.Message).delete(20000)
                    .catch(console.error);
            })
        return;
    }
    if(msgObject.content.startsWith("-eat")){
        cooldown.add(msgObject.author.id)

    }

    setTimeout(() => {
        cooldown.delete(msgObject.author.id)
    }, cdseconds * 1000);

    {let embed = new Discord.RichEmbed()

        .setAuthor(`${msgObject.author.tag}`, msgObject.author.displayAvatarURL) 

        .setDescription(`${msgObject.author}, ${(eat[index1])}${amount} DinoBucks`)

        .setColor("RANDOM");

        msgObject.channel.send(embed)
            .then(msg => {
                (msg as Discord.Message).delete(20000)
                    .catch(console.error);
            })  
        }      
    } 
}

This is not possible with the setup you currently have. However, you could use something like what's on the Discord.js guide :

// a collection of user ids to when (the timestamp) they used the command
const timestamps = new Discord.Collection<string, number>();

if (msgObject.content.startsWith("-eat")) {
    const now = Date.now();
    const cooldownAmount = cdseconds * 1000;

    if (timestamps.has(msgObject.author.id)) {
        const expirationTime = timestamps.get(msgObject.author.id) + cooldownAmount;

        if (now < expirationTime) {
            const timeLeft = (expirationTime - now) / 1000;
            return msgObject.reply(`You have to wait ${timeLeft.toFixed(1)} seconds to use this command again!`)
        }
    }

    // for v11:
    // const embed = new Discord.RichEmbed()
    const embed = new Discord.MessageEmbed()
        .setAuthor(`${msgObject.author.tag}`, msgObject.author.displayAvatarURL)
        // rest of command...

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

Updated for Discord.js v13.

With your cooldown definition format this is not possible. I would recommend you use discord.colletion for this, there is a great example implementation.

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