如何为命令设置冷却时间? (ModMail)

时间:2021-04-16 00:34:52

标签: javascript node.js discord.js

嘿,我一直在从 GitHub 编辑一个 ModMail 机器人,我想放一个冷却时间。因此,当有人 dms bot 时,他们必须等待 5 分钟才能再次 dming bot(使用 modmail 的东西)。我尝试了很多东西,但效果不佳。


代码:

const Discord = require("discord.js");
const dmBot = new Discord.Client();
const config = require("./config.json");


dmBot.on("ready", async () => {

    console.log(config.READY_MESSAGE);
    dmBot.user.setActivity(config.ACTIVITY_STATUS, {
        type: "WATCHING"
    });

});

dmBot.on("message", (message) => {
    
    
    if (message.channel.type === "dm") { 
        var args = message.content.split(" ").slice(0)
        var args = args.slice(0).join(" ")
        var BOT_ID = dmBot.user.id
        var userID = message.author.id
        if (message.content.startsWith(config.PREFIX)) return message.channel.send(":x: Please use this command in the server!") 
        if (message.author.bot) return;
        message.channel.send("Message has been sent!").then(msg => msg.delete(3000))
        if (message.content.startsWith(config.PREFIX)) return
        if (args.length > 1024) return message.reply("Your message content too many characters (1024 Limit)") 
        var embed = new Discord.RichEmbed()
            .setColor(config.ORANGE)
            .setAuthor("New Message", "https://cdn.discordapp.com/attachments/502649544622735362/520740243133956138/receive.png")
            .addField(`Sent by: ${message.author.username}`, 
            args)
            .setTitle("Message:")
            .setFooter("Sent By: " + message.author.username + " ", message.author.avatarURL)
            .setTimestamp()
        dmBot.guilds.get(config.SERVER_ID).channels.get(config.CHANNEL_ID).send(embed).catch(console.log(`Message recieved from ${userID}!(${message.author.username})`))
        dmBot.guilds.get(config.SERVER_ID).channels.get(config.CHANNEL_ID).send({embed: {
            "description": `${config.PREFIX}reply ${message.author.id} <message>`,
        }
    })
    }else
    
    
    if (message.content.startsWith(config.PREFIX + "reply")) {
        if (message.author.id !== config.YOUR_ID) return message.reply('You cannot use that!')
        var args = message.content.split(" ").slice(0)
        var Rargs = message.content.split(" ").slice(2).join(" ")
        var userID = args[1]
        if (isNaN(args[1])) return message.reply("Make sure to you correctly enter the user's ID!")
        var embed = new Discord.RichEmbed()
            .setColor(config.ORANGE)
            .setAuthor("New Message", "https://cdn.discordapp.com/attachments/502649544622735362/520740243133956138/receive.png")
            .setDescription(Rargs)
            .setTitle("Message:")
            .setFooter("Sent By: " + message.author.username + " ", message.author.avatarURL)
        dmBot.users.get(userID).send(embed).catch(console.log(`Message was successfully sent to ${userID}!`))
        if (message.author.bot) return;
        message.channel.send("Message Sent!").then(msg => msg.delete(3000)).catch(console.error)
    }

});


dmBot.login(config.TOKEN);

TL;DR 如何为该命令添加 5 分钟的冷却时间?

1 个答案:

答案 0 :(得分:0)

首先,你需要在top声明:-

const cooldown = new Set();

然后将此块添加到命令中或外部(任何您想要的位置):-

      if (cooldown.has(message.author.id)) {
        //checks if author is aready in cooldown set
            message.channel.send("Wait five minutes before running it again.");
    } else {
       //your command here...
       
        cooldown.add(message.author.id);
        //adds author in set(cooldown)
        setTimeout(() => {
          // Removes the user from the set after 5 minutes
          cooldown.delete(message.author.id);
        }, 300000);
    }

如果冷却时间是整个公会的,则在任何地方用 message.author.guild.id 替换 message.author.id