显示冷却时间

时间:2020-03-16 04:57:28

标签: 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);
            })  
        }      
    } 
}

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

无法使用冷却时间定义格式。我建议您为此使用discord.colletion,这是一个不错的example实现。