Error after following the cooldown guide
Hi. New to discord.js, I thus followed the guide. Everything worked well, but I then arrived to the cooldown part. And now, I get this error when running
And here is my client on event create part :
But my code is basically the exact same as the guide one, so... I really don't get what happens ;-;
Thank you for your time ^^
node index.js
:
node:events:489
throw er; // Unhandled 'error' event
^
TypeError: Cannot read properties of undefined (reading 'has')
at Client.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\index.js:45:17)
at Client.emit (node:events:511:28)
at InteractionCreateAction.handle (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:354:31)
at WebSocketManager.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:238:12)
at WebSocketManager.emit (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@discordjs\ws\dist\index.js:1103:51)
at WebSocketShard.emit (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.onMessage (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@discordjs\ws\dist\index.js:938:14)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
node:events:489
throw er; // Unhandled 'error' event
^
TypeError: Cannot read properties of undefined (reading 'has')
at Client.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\index.js:45:17)
at Client.emit (node:events:511:28)
at InteractionCreateAction.handle (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:354:31)
at WebSocketManager.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:238:12)
at WebSocketManager.emit (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@discordjs\ws\dist\index.js:1103:51)
at WebSocketShard.emit (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.onMessage (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@discordjs\ws\dist\index.js:938:14)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
const { cooldowns } = client;
if (!cooldowns.has(command.data.name)) {
cooldowns.set(command.data.name, new Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.data.name);
const defaultCooldownDuration = 2;
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;
if (timestamps.has(interaction.user.id)) {
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;
if (now < expirationTime) {
const expiredTimestamp = Math.round(expirationTime / 1000);
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, ephemeral: true });
}
}
timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
const { cooldowns } = client;
if (!cooldowns.has(command.data.name)) {
cooldowns.set(command.data.name, new Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.data.name);
const defaultCooldownDuration = 2;
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;
if (timestamps.has(interaction.user.id)) {
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;
if (now < expirationTime) {
const expiredTimestamp = Math.round(expirationTime / 1000);
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, ephemeral: true });
}
}
timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
5 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!Did you define cooldowns in the client?
Do you mean in each individual command file ?
well apparently not, thank you, how did I miss that ;-;