Skip to content

CIRCE / DOCUMENTATION

Guards

Preconditions run before a handler, and the built-in guards.

Documentation menu
On this page

A guard returns true to allow, or a string with the reason it denies. The first denial stops execution; the reason is shown ephemerally unless an on*GuardDenied hook says otherwise.

type GuardResult = true | string;
type Guard = (ctx: CommandContext) => GuardResult | Promise<GuardResult>;

Commands, subcommands, context menus, components and modals accept guards: [...]. Group guards run before subcommand guards. Components and modals receive their own context type.

Built-in

Guard Signature Denies when
guildOnly() () => Guard Used outside a server.
hasPermission(...flags) (...PermissionResolvable[]) => Guard The member lacks any permission.
invokerOnly(options?) ({ resolveOwnerId? }) => Guard The user is not the one who triggered the original interaction. An owner that cannot be established is denied.
cooldown(options) See Cooldown. The key is still cooling down.
guards: [guildOnly(), hasPermission("ManageMessages")];

Built-ins read only the part of the context they need, so one instance works on every interaction kind that carries it.

Writing your own

A guard is a factory returning a function. circe add guard admin generates the skeleton in src/guards/.

import type { Guard } from "@sxnnyside/circe";

export function adminGuard(): Guard {
  return (ctx) => (ctx.member?.permissions.has("Administrator") ? true : "Administrators only.");
}

There is no defineGuard.

Denial hooks

onGuardDenied (commands), onContextMenuGuardDenied, onComponentGuardDenied and onModalGuardDenied live in the error boundary.