Add cooldowns to slash commands
I was thinking it would be a nice feature to add cooldowns onto slash commands like how we have cooldowns on channels and show how many seconds they have left before they can use the commands again. (I know we can easily add cooldowns via code but it would be nice if we had some UI on the slash command discovery)
-
Yes, this would be useful to protect the server against spamming and to some extent protection against fake links.
2 -
So i can only run it once every 10 seconds. So i'm going to go into my code here. And i'm going to add another property this will be called cooldown. And i'm going to say 10.
0 -
Here is the code which I used and it worked:
First you have to create a Set
const cooldown = new Set();
///This is 1 minute, you can change it to whatever value
const cooldownTime = 60000;
Then this is the code:if (cooldown.has(interaction.user.id)) {
/// If the cooldown did not end
interaction.reply({ content: "Please wait for the cooldown to end", ephemeral: true });
} else {
/// Else give the reply
interaction.reply({ content: "What is this uwu", ephemeral: true });//now, set cooldown
cooldown.add(interaction.user.id);
setTimeout(() => {
// Removes the user from the set after 1 minute
cooldown.delete(interaction.user.id);
}, cooldownTime);
}0 -
First, create a discord.js collection to store users' cooldown and set cooldown time.
// index.js client.cooldowns = new Discord.Collection(); client.COOLDOWN_SECONDS = 10; // replace with desired cooldown time in seconds
When a user executes a command, check if the user exists in the cooldown. If so, notify it to the user. If not, run the comnand and set cooldown. After the time you specified, remove it.
// command file async run(client, interaction, message) { if (client.cooldowns.has(interaction.user.id)) { // cooldown not ended interaction.reply({ content: "Please wait for cooldown to end", ephemeral: true }); } else { // no cooldown // do something here //now, set cooldown client.cooldowns.set(interaction.user.id, true); // After the time you specified, remove the cooldown setTimeout(() => { client.cooldowns.delete(interaction.user.id); }, client.COOLDOWN_SECONDS * 1000); } }
1 -
Here is the code which I used and it worked:
First you have to create a Set
const cooldown = new Set(); ///This is 1 minute, you can change it to whatever value const cooldownTime = 60000;
Then this is the code:
if (cooldown.has(interaction.user.id)) { /// If the cooldown did not end interaction.reply({ content: "Please wait for the cooldown to end", ephemeral: true }); } else { /// Else give the reply interaction.reply({ content: "What is this uwu", ephemeral: true }); //now, set cooldown cooldown.add(interaction.user.id); setTimeout(() => { // Removes the user from the set after 1 minute cooldown.delete(interaction.user.id); }, cooldownTime); }
-1 -
Thank you. Your example didn't work for me but I managed to fix it, which was basically the same thing!
0 -
I faced similar kind of issue last time, I am still searching for some proper solution Same issue still no fix to this.
0 -
After answering all the questions over there, while making the submission, you need to give all your contact details over there. MCDVOICE
0 -
Thanks for the code,
It really going to be really nice feature.
0
Iniciar sesión para dejar un comentario.
Comentarios
9 comentarios