Guard
function in meocord/decorator Since 4.0.0
Guard(
options?: GuardOptions,
): (target: any, propertyKey?: string | symbol) => voidMarks 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
@UseGuardor@MeoCord({ guards })applies it, the global ones first
Parameters
| Name | Type | Default | Since | Description |
|---|---|---|---|---|
options? | GuardOptions | 4.1.0 |
| |
options.types? | readonly ExecutionContextType[] | every type | 4.1.0 | The context types the guard runs for, as |
Returns
Returns a property decorator.
(target: any, propertyKey?: string | symbol) => voidThrows
Error when
typesis empty, as the decorator applies.
Examples
@Guard({ types: ['interaction'] })
export class OwnerOnlyGuard implements GuardInterface {
canActivate(interaction: ButtonInteraction, { ownerId }: { ownerId: string }): boolean {
return interaction.user.id === ownerId
}
}