首页 > 解决方案 > Discord JS Cooldown 与验证

问题描述

我目前正在编写一个机器人不和谐的代码,我发现自己遇到了一个问题,我希望能够及时限制命令,所以我设置了一个有效的冷却系统,问题是在我的命令中我有类似的检查如果 userID 等于 X,或者第二个参数中有一个数字,并且如果不满足这些“ifs”,我会返回一条错误消息,但命令会进入冷却:

啤酒命令示例:

async run(message, args) {
        if (message.author.id === "299998913606451211" || message.author.id === "407970992015671296") {

            if (args.length < 2) {
                return message.channel.send(`Vous devez ajouter un nom d'utilisateur ainsi que le temps désiré.`).then(msg => {
                    msg.delete({ timeout: 10000, });
                });
            }
    
            if (isNaN(args[1])) {
                return message.channel.send(`Vous devez ajouter un nombre en deuxième argument.`).then(msg => {
                    msg.delete({ timeout: 10000, });
                });
            }

            if(args[1] > 60 || args[1] <= 0) { return message.channel.send("Le nombre doit être situé entre 1 et 60."); }

            if(args[0].toLowerCase() == "krosnoz") { return message.channel.send("T'es fou toi"); }
            
            const drunkUser = this.client.users.cache.find(user => user.username.toString().toLowerCase() == args[0].toString().toLowerCase());
            if (drunkUser) {
                const profil = await profileModel.findOne({ userID: drunkUser.id })
                profil.drunk = true;
                profil.save();
                
                setTimeout(() =>  {
                    profil.drunk = false;
                    profil.save();
                }, args[1] * 1000);

                //Run correctly
                return message.channel.send(drunkUser.username + " est maintenant bourré (zebi).");
            } else { return message.channel.send("Utilisateur introuvable."); }
        } else {
            return message.channel.send("Vous n'avez pas les permissions requises pour effectuer cette action.").then(msg => {
                msg.delete({ timeout: 10000, });
            });
        }
    }

我如何管理冷却时间:

if (command) {

            const { cooldowns } = this.client;

            if (!cooldowns.has(command.name)) {
                cooldowns.set(command.name, new Collection());
            }

            const now = Date.now();
            const timestamps = cooldowns.get(command.name);

            const cooldownAmount = command.cooldown * 1000;

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

                if (now < expirationTime) {
                    const timeLeft = (expirationTime - now) / 1000;
                    return message.reply(`Il faut encore attendre ${timeLeft.toFixed(1)} secondes avant de pouvoir réutiliser cette commande.`);
                }
            }

            console.log(command);

            timestamps.set(message.author.id, now);

            setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);

            command.run(message, args);
        }

我的问题是,如果命令运行不正确,我如何不添加冷却时间?

标签: node.jsdiscord

解决方案


推荐阅读