#Cooldown filter precondition

1 messages · Page 1 of 1 (latest)

weary zealot
#

I wish to create a cooldown for a command but instead of filtering by user (cooldownFilteredUsers), I want to filter it with a precondition (ie user has a certain role in the guild). What is the best way to do this?

(I don't want to deny the command usage, just apply a cooldown for users who do not meet the precondition.)

median ironBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

frigid arrow
#

Extend the class and override the methods

weary zealot
#

Would it be something like this?

import { Precondition } from "@sapphire/framework";
import {
  Subcommand,
  SubcommandPreconditions,
} from "@sapphire/plugin-subcommands";
import type { ChatInputCommandInteraction } from "discord.js";

const allowedRole = "920834418459967548";

export class UserPrecondition extends SubcommandPreconditions.PluginSubcommandCooldown {
  public constructor(
    context: Precondition.LoaderContext,
    options: Precondition.Options
  ) {
    super(context, {
      ...options,
      name: "CustomCooldown",
    });
  }

  public override async chatInputRun(
    interaction: ChatInputCommandInteraction,
    subcommand: Subcommand,
    context: SubcommandPreconditions.PluginSubcommandCooldownContext
  ) {
    if (
      interaction.inCachedGuild() &&
      interaction.guild.roles.cache.get(allowedRole)
    ) {
      return this.ok();
    }
    return super.chatInputRun(interaction, subcommand, context);
  }
}

declare module "@sapphire/framework" {
  interface Preconditions {
    CustomCooldown: never;
  }
}
frigid arrow
#

If you're going that route I would recommend just extending Precondition instead of SubcommandPreconditions.PluginSubcommandCooldown

weary zealot
#

Yeah i realised afterwards that it didn't work as intended. and the precondition didn't run 😦

I wanted to extend the cooldown precondition method because I'm not sure how to code the cooldown myself. So i thought to just add to the super method

frigid arrow
#

The code is open source on github 🙂 you can just copy pasta code