Cooldown filter precondition

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.)
Solution:
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...
Jump to solution
5 Replies
Favna
Favna•4mo ago
Extend the class and override the methods
fluffy
fluffy•4mo ago
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;
}
}
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;
}
}
Favna
Favna•4mo ago
That restricts outright rather than being a cooldown but other than that, yes If you're going that route I would recommend just extending Precondition instead of SubcommandPreconditions.PluginSubcommandCooldown
Solution
fluffy
fluffy•4mo ago
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
Favna
Favna•4mo ago
The code is open source on github 🙂 you can just copy pasta code
Want results from more Discord servers?
Add your server