Discord.js-每个用户(不是所有用户)的命令冷却 [英] Discord.js - Cooldown for a command for each user not all users
问题描述
我正在开发discord.js机器人,并且想对命令进行冷却。
I am developing a discord.js bot and I want to make a cooldown for a command.
我在Google上看到了很多关于如何做的教程。 ,但是所有这些教程都针对所有命令执行此操作(因此,当用户键入!mycmd时,所有用户都必须等待X分钟/秒才能再次键入它。)。
I saw a lot of tutorials on how to do it on Google, but all those tutorials do it for all the commands (so when a user type !mycmd all the users have to wait X minutes/seconds until it can be typed again).
但是我想为每个用户执行此操作(当用户键入!mycmd时,仅此用户必须等待X分钟/秒,直到该用户可以再次键入它为止)。
But I want to do it for each user (when a user type !mycmd , ONLY this user have to wait X minutes/seconds until THE USER can type it again).
有可能吗?
谢谢!
推荐答案
是
将此添加到JS文件的顶部:
Add this at the top of your JS file:
// First, this must be at the top level of your code, **NOT** in any event!
const talkedRecently = new Set();
现在在命令事件中添加以下内容:
Now in the command event add this:
if (talkedRecently.has(msg.author.id)) {
msg.channel.send("Wait 1 minute before getting typing this again. - " + msg.author);
} else {
// the user can type the command ... your command code goes here :)
// Adds the user to the set so that they can't talk for a minute
talkedRecently.add(msg.author.id);
setTimeout(() => {
// Removes the user from the set after a minute
talkedRecently.delete(msg.author.id);
}, 60000);
}
这篇关于Discord.js-每个用户(不是所有用户)的命令冷却的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!