Slash Command Cooldowns are not working

// command cooldowns
client.slashCooldowns = new Discord.Collection();
const { slashCooldowns } = client;
if (!slashCooldowns.has(interaction.commandName)) {
slashCooldowns.set(interaction.commandName, new Discord.Collection());
}

const now = Date.now();
const timestamps = slashCooldowns.get(interaction.commandName);
console.log('timestamp ', timestamps)
const cooldownAmount = (command.cooldown || 1) * 1000;
console.log('cooldownAmount ', cooldownAmount);
if (timestamps.has(interaction.user.id)) {
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;
console.log('expirationTime ', expirationTime)

if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
console.log('timeLeft ', timeLeft)
return interaction.reply({content: `Please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`});
}
}

timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
// command cooldowns
client.slashCooldowns = new Discord.Collection();
const { slashCooldowns } = client;
if (!slashCooldowns.has(interaction.commandName)) {
slashCooldowns.set(interaction.commandName, new Discord.Collection());
}

const now = Date.now();
const timestamps = slashCooldowns.get(interaction.commandName);
console.log('timestamp ', timestamps)
const cooldownAmount = (command.cooldown || 1) * 1000;
console.log('cooldownAmount ', cooldownAmount);
if (timestamps.has(interaction.user.id)) {
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;
console.log('expirationTime ', expirationTime)

if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
console.log('timeLeft ', timeLeft)
return interaction.reply({content: `Please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`});
}
}

timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
I can still use commands like /help consecutively even when I have the cooldown set to 3000 seconds (to test). As you can see I am logging and it logs up until expirationTime that one does not log. just timestamp and cooldownAmount.
timestamp Collection(0) [Map] {}
cooldownAmount 3000000
timestamp Collection(0) [Map] {}
cooldownAmount 3000000
That's how those log. Not sure why.
4 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.
조아오
조아오3y ago
You are assigning a new collection each time a command runs? Bc if you are You are resetting all the cooldowns Each time a command is run
Erin
ErinOP3y ago
That was it. THank you!
조아오
조아오3y ago
Np
Want results from more Discord servers?
Add your server