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.
Console output
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);Map(1) { 'work' => Collection(0) [Map] {} }
1659993437416
Collection(0) [Map] {}Map(1) { 'work' => Collection(0) [Map] {} }
1659993437416
Collection(0) [Map] {}