const cooldowns = new Map();
client.on("interactionCreate", async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const { commandName, guildId } = interaction;
const now = Date.now();
const cooldownAmount = 3 * 1000; // Cooldown time in milliseconds (3 seconds here)
if (!cooldowns.has(commandName)) {
cooldowns.set(commandName, new Map());
}
const timestamps = cooldowns.get(commandName);
const expirationTime = timestamps.get(guildId) + cooldownAmount;
if (timestamps.has(guildId)) {
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return interaction.reply({
content: `Please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${commandName}\` command.`,
ephemeral: true,
});
}
}
timestamps.set(guildId, now);
setTimeout(() => timestamps.delete(guildId), cooldownAmount);
// Your command logic here
// Existing command logic follows...
});
Explanation
Cooldown Map: A cooldowns map is initialized to keep track of cooldowns for each command.
Interaction Handling: For each command, it checks if the user is on cooldown. If so, it replies with the remaining cooldown time. Otherwise, it sets a cooldown for the user and proceeds with the command execution.
Command Logic: The existing command logic remains unchanged and is executed if the user is not on cooldown.
Additional Information
code above is from chatGPT
remember to disable roles in side bar
Note:
If you are planning to work on this feature, please comment on this issue to express your interest.
add a cooldown for /role and make the roles not apear in the side bar
Rationale
server owners are complaining about /role getting spammed, filling the role slots and side bar
this would get more servers to enable /role
Implementation Details
Cooldowns | discord.js Guide using maps for per server cooldown
Explanation
Cooldown Map: A cooldowns map is initialized to keep track of cooldowns for each command.
Interaction Handling: For each command, it checks if the user is on cooldown. If so, it replies with the remaining cooldown time. Otherwise, it sets a cooldown for the user and proceeds with the command execution.
Command Logic: The existing command logic remains unchanged and is executed if the user is not on cooldown.
Additional Information
code above is from chatGPT remember to disable roles in side bar
Note: