javascript - 显示冷却剩余时间

标签 javascript typescript discord discord.js

我希望显示冷却时间中剩余的时间,但我不确定是否可以使用我当前拥有的格式来完成。是否可以?或者有更好的方法来使用冷却时间吗?我删除了大部分与此相关的代码,因为它不相关。

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

最佳答案

这对于您当前的设置来说是不可能的。但是,您可以使用类似 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);
}

已更新 Discord.js v13。

关于javascript - 显示冷却剩余时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60700515/

相关文章:

javascript - 如何在 IE 7/8 中使用 javascript 从客户端验证文件大小?

javascript - Angular 4 组件 : call javascript/jquery code from another file

python - 选择随机 xx 答案 python 机器人

typescript - 如何在 TypeScript 1.8 中导入 jQuery npm 模块

Angular 2 : how to open ngx-popover programmatically

python - 让机器人加入 vc 并播放音乐

javascript - 有没有办法找出用户拥有的最高角色?

javascript - 我如何找到 typescript 版本支持的 ecmascript 版本列表?

javascript - Mongoose 使用 .select() 方法

javascript - 在 Couchdb 中组合 JavaScript 对象