Skip to content
GitHub

Guard

function in meocord/decorator Since 4.0.0

Guard(
  options?: GuardOptions,
): (target: any, propertyKey?: string | symbol) => void

Marks a class as a guard, which decides whether a handler runs.

Use it on a class that implements GuardInterface, then apply the class with UseGuard or @MeoCord({ guards }). For facts about the handler a guard reads, use createMetadata; to limit how often a handler runs, use Cooldown instead.

Where it runs

  • Guards — where @UseGuard or @MeoCord({ guards }) applies it, the global ones first

Parameters

NameTypeDefaultSinceDescription
options?GuardOptions4.1.0

types, the context types the guard runs for; every type unless given.

options.types?readonly ExecutionContextType[]every type4.1.0

The context types the guard runs for, as ExecutionContext.getType() reports them; it is skipped for any other call. A subclass inherits them unless it declares its own. A global guard also runs before @On handlers, so one that reads an interaction declares ['interaction'].

Returns

Returns a property decorator.

(target: any, propertyKey?: string | symbol) => void

Throws

  • Error when types is empty, as the decorator applies.

Examples

TypeScript
@Guard({ types: ['interaction'] })
export class OwnerOnlyGuard implements GuardInterface {
  canActivate(interaction: ButtonInteraction, { ownerId }: { ownerId: string }): boolean {
    return interaction.user.id === ownerId
  }
}