javascript - 如何为命令设置冷却时间?(模组邮件)
问题描述
嘿,所以我一直在从 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 分钟的冷却时间?
解决方案
首先,您需要在顶部声明:-
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.id 替换为 message.author.guild.id
推荐阅读
- angular - 角度数据源未在 HTML 模板中加载
- javascript - 如何使用 window.postmessage 与父窗口通信
- flutter - Dart中Function类型参数的默认值
- perl - Parallel::ForkManager 给出 - 子进程 STDIN 不是真正的系统错误
- docker - 从 DockerHub 拉取时,.NetCore Api Docker Image 不工作
- javascript - 使用 Enzyme mount() 访问嵌套组件
- python - Flask URL 重定向,其中参数是 URL 的一部分
- python - 我正在尝试使用正则表达式获取以 < 开头并以 > 结尾的子字符串并删除它们以创建一个新字符串
- node.js - How do you update a thumbnailPhoto in Active Directory using NodeJS and ldapjs?
- database - Unable to install Sample OE schema in Pluggable DB in Oracle 19c