Discord.js Bot速率限制



所以我想在线上传我的discord机器人,但我想防止用户滥发它的命令。所以让延迟为5秒,如果用户运行!help,机器人会回答,但如果用户在延迟到期前运行!help,机器人会说:wait some time before using this command again。此外,我希望延迟只适用于消息作者,而不影响其他用户。我正在使用命令处理程序,所以有可能制作类似module.exports.delay的东西吗?

要做到这一点,您可以使用时间戳,如:
let msgTimestamp = [];
if(message.content === "?" + "help") {
if(typeof msgTimestamp[0] !== "undefined") {
if(msgTimestamp[0] + 5000 < Date.now()) {
message.channel.send("Here embed of command help.");
msgTimestamp = [];
} else {
message.channel.send("wait some time before using this command again.");
}
} else {
msgTimestamp.push(Date.now());
message.channel.send("Here embed of command help.");
}
}

在这个coode中,您使用时间戳,检查消息的内容是否与您想要使用的命令一致,如果用户已经发送了消息,则包含消息时间戳的变量已满,然后检查变量的typeof是否未定义,然后检查id用户发送消息的时间戳低于实际时间戳+您想要的延迟时间!

最新更新