node.js - 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);
}
我的问题是,如果命令运行不正确,我如何不添加冷却时间?
解决方案
推荐阅读
- python - IndexError:列表索引超出范围/ Facenet
- git - smartgit上分支窗口中的箭头
- sql - 编写查询以从表 A 派生表 B
- firebase - Flutter firebase_messaging iOS应用程序在前台应用程序时未收到推送通知
- node.js - 为什么console.log在nodejs的app.js中出现两次
- mysql - mysql - 计算两个日期之间的差异
- c# - LINQ order by another property is first property is equal to a given value
- search - 将列表转换为节点指针
- kubernetes - 使用 Katacoda 跟踪 Kubernetes
- c# - Angular7 - .NET Core v2 抛出 http 415(不支持的媒体类型)