Cooldowns

Cooldowns

Cooldowns are useful for preventing users from repeating the same actions in a short period of time. To use cooldowns in your command, you can simply add a cooldown property to your command module.

import { SlashCommandModule, SlashCommandBuilder } from 'reciple';

class ExampleCommand extends SlashCommandModule {
  data = new SlashCommandBuilder()
    .setName('test')
    .setDescription('Test command')
    .toJSON();

  cooldown = 1000 * 10; // 10 seconds

  async execute(interaction) {
    await interaction.reply('Hello world!');
  }
}

export default new ExampleCommand();

The cooldown property takes in a number in milliseconds.

By default, when a command is cooled-down, the command will simply not execute without any feedback to the user. To change this behavior, you can handle the cooldown with a command postcondition.

Cooldown Postcondition

import { CommandPostconditionReason, CommandType, PostconditionModule, type CommandPostcondition } from 'reciple';
import { time } from 'discord.js';

export class CooldownHandler extends PostconditionModule {
  async execute(data) {
    if (data.reason !== CommandPostconditionReason.Cooldown) return false; // Only handle cooldowns

    switch (data.executeData.type) {
      case CommandType.Message:
        const message = data.executeData.message;

        await message.reply(`You cannot use this command for ${time(data.cooldown.endsAt, 'R')}`);
        return true;
      case CommandType.Slash:
      case CommandType.ContextMenu:
        const interaction = data.executeData.interaction;

        await interaction.reply(`You cannot use this command for ${time(data.cooldown.endsAt, 'R')}`);
        return true;
    }
  }
}

export default new CooldownHandler();

Custom Cooldown Adapter

You can create a custom cooldown manager by extending the BaseCooldownAdapter class.

For reference, the CooldownAdapter class is a built-in and default cooldown adapter that uses the Collection class to store cooldowns in memory.