冷却 discord.js

时间:2021-02-22 06:08:10

标签: javascript discord.js

这是我的代码,如果该命令在 10 秒内使用过两次,我希望它发送一条消息。 Idk 但这是非常错误的

        var bumpEmbedTwo = new Discord.MessageEmbed()
        .setTitle('Cool Down!!!')
        .setColor(0xFF0000)
        .setDescription('Please waitt 30 more seconds before you use this command again')
            setTimeout(() => {
            message.channel.send(bumpEmbedTwo)
        }, 5000)
        var bumpEmbed = new Discord.MessageEmbed()
        .setTitle('Time to Bump!')
        .setColor(0xFF0000)
        .setDescription('Please use the command `!d bump` to bump the server!!!')
            setTimeout(() => {
            message.channel.send('<@&812133021590880316>')
            message.channel.send(bumpEmbed)
        }, 1000)

2 个答案:

答案 0 :(得分:0)

解决此问题的一种方法是将用户+命令+时间保存在数据库或某些全局变量中。

然后进行检查。

伪代码:

spider_idle

答案 1 :(得分:0)

您提供的代码不正确,因为您在没有等待的情况下设置了超时,因此它会完全忽略超时的目的。此外,无论何时使用 .on('message') 函数,它仍然会执行命令,您需要做的是创建一个对象:const cooldowns = new Discord.Collection(); 并且每当触发 'message' 事件时,您必须添加任何你想要的冷却量。

代码如下:

const now = Date.now();
let cooldownAmount = 5 * 1000; // In this case, it is 5 seconds
    if (cooldowns.has(message.author.id)) {
      const expirationTime = cooldowns.get(message.author.id) + cooldownAmount;
      if (now < expirationTime) {
        const timeLeft = (expirationTime - now) / 1000;
        return message.channel.send(`Please wait ${timeLeft.toFixed(1)} more second(s) before using another command`);
      }
    }
    if(!cooldowns.has(message.author.id)){
    <command>.run() // Be sure to edit <command to whatever the fetched command is> and add to the (), e.g. message, args, bot, etc. 
    }
    cooldowns.set(message.author.id, now);
    setTimeout(() => cooldowns.delete(message.author.id), cooldownAmount);

PS:箭头符号中的客户端供您更改。 如果有任何额外的问题,您可以在此帖子中评论!或者 dm me on discord: N̷i̷g̷h̷t̷m̷a̷r̷e̷ ̷ Jeff#2616