command cooldown

I am trying to make a command cooldown, please note I am using slash commands here and the code below is in the interactionCreate.js event file. The event works perfectly fine, it is just the cooldown. Here is the cooldown code.
//Above execute
const cooldowns = new Map();
//After execute
const current_time = Date.now();
console.log(cooldowns)
console.log(current_time)
const time_stamps = cooldowns.get(interaction.commandName);
console.log(time_stamps)
const cooldown_amount = (interaction.cooldown) * 3600000;
if(time_stamps.has(interaction.user.id)){
const expiration_time = time_stamps.get(interaction.user.id) + cooldown_amount;
console.log(expiration_time)
if(current_time < expiration_time){
const time_left = Duration(expiration_time - Date.now(), { units: ['h', 'm', 's'], round:true })
console.log(time_left)
}
}

//If the author's id is not in time_stamps then add them with the current time.
time_stamps.set(interaction.user.id, current_time);
//Delete the user's id once the cooldown is over.
setTimeout(() => time_stamps.delete(interaction.user.id), cooldown_amount);
//Above execute
const cooldowns = new Map();
//After execute
const current_time = Date.now();
console.log(cooldowns)
console.log(current_time)
const time_stamps = cooldowns.get(interaction.commandName);
console.log(time_stamps)
const cooldown_amount = (interaction.cooldown) * 3600000;
if(time_stamps.has(interaction.user.id)){
const expiration_time = time_stamps.get(interaction.user.id) + cooldown_amount;
console.log(expiration_time)
if(current_time < expiration_time){
const time_left = Duration(expiration_time - Date.now(), { units: ['h', 'm', 's'], round:true })
console.log(time_left)
}
}

//If the author's id is not in time_stamps then add them with the current time.
time_stamps.set(interaction.user.id, current_time);
//Delete the user's id once the cooldown is over.
setTimeout(() => time_stamps.delete(interaction.user.id), cooldown_amount);
Console output
Map(1) { 'work' => Collection(0) [Map] {} }
1659993437416
Collection(0) [Map] {}
Map(1) { 'work' => Collection(0) [Map] {} }
1659993437416
Collection(0) [Map] {}
3 Replies
d.js docs
d.js docs3y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Dagan
Dagan3y ago
Why dont you just make a collection in the work with just for every id a bool thats set to true and reseted to false when the cooldown is over? I dont get why youre doing so much complecated things when you could just make it few lines of code
PAdventures
PAdventuresOP3y ago
i like the method above
Want results from more Discord servers?
Add your server