Gaute945 / Overmounting

Overmounting is a feature rich discord bot with discord's api and complex slash commands
GNU General Public License v3.0
5 stars 2 forks source link

/role spam #54

Open Gaute945 opened 1 month ago

Gaute945 commented 1 month ago

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

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

Additional Information

code above is from chatGPT remember to disable roles in side bar


Note: