cooldown

I get that message on the cooldown command, im not sure why it is undefined, here is my code:
const {
Collection
} = require('discord.js');

module.exports = {
name: "interactionCreate",

async execute(interaction, client) {
if (!interaction.isChatInputCommand()) return;

const command = client.commands.get(interaction.commandName);

if (!command) {
return interaction.reply({ content: "This command is outdated. Please try again later", ephemeral: true});
}

const { cooldowns } = client;

if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Collection());
}

const now = Date.now();
const timestamps = cooldowns.get(command.name);
const defaultCooldownDuration = 3;
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 <t:${expiredTimestamp}:R> more second(s) before reusing the \`${command.name}\` command.`, 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 });
}
}
}
const {
Collection
} = require('discord.js');

module.exports = {
name: "interactionCreate",

async execute(interaction, client) {
if (!interaction.isChatInputCommand()) return;

const command = client.commands.get(interaction.commandName);

if (!command) {
return interaction.reply({ content: "This command is outdated. Please try again later", ephemeral: true});
}

const { cooldowns } = client;

if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Collection());
}

const now = Date.now();
const timestamps = cooldowns.get(command.name);
const defaultCooldownDuration = 3;
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 <t:${expiredTimestamp}:R> more second(s) before reusing the \`${command.name}\` command.`, 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 });
}
}
}
40 Replies
d.js toolkit
d.js toolkit2y 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.
JollyJohn
JollyJohnOP2y ago
[email protected] Node.js v18.12.1
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
👍
{
folder: 'Utility',
cooldown: 10,
data: SlashCommandBuilder {
name: 'show-tags',
description: 'Shows all tags.',
options: [],
default_permission: undefined,
default_member_permissions: undefined,
dm_permission: true,
nsfw: undefined
},
execute: [AsyncFunction: execute]
}
{
folder: 'Utility',
cooldown: 10,
data: SlashCommandBuilder {
name: 'show-tags',
description: 'Shows all tags.',
options: [],
default_permission: undefined,
default_member_permissions: undefined,
dm_permission: true,
nsfw: undefined
},
execute: [AsyncFunction: execute]
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
module.exports = { cooldown: 10, data: new SlashCommandBuilder() .setName('show-tags') .setDescription('Shows all tags.') .setDMPermission(true),
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
oh, alr, thank you ye ima fix it thanks!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
wait so to make sure i understand you, i am defining it liek this ${command.data.name}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
ye ok thanks
d.js docs
d.js docs2y ago
Message formatting (mentions, timestamps, emoji, etc.): learn more
JollyJohn
JollyJohnOP2y ago
ok time to figure out what format i want to use
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
yeah, okay, thank you I was also just gonna do please wait x seconds until reusing this command what does that come out to look like? if you dont mind showing
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
alr thats what i was planning on doing 😄
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
yes ok, thanks
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
Okay, thank you for your help, appreciate it.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
im gonna make a pr to have it fixed, whats ur github so i can give u credit as well
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
Ok, ill give u credit as well cheers!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
ofc would be rude if i just took it for myself
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
Ok, will do
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
ye im trying to think how it should look You are on cooldown, you can use this command again (timestamp) cuz the timestamp says in maybe?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
word
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
ye thats what i was thinking but then it might look strange after the date is pasted you can use this command again 3 hours ago!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
its better then it is now Please wait, you are on a cooldown for `${command.data.name}`. You can reuse it <t:${expiredTimestamp}:R> that better yo uthink?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JollyJohn
JollyJohnOP2y ago
i think its better without a timestmap ngl
Want results from more Discord servers?
Add your server