Skip to main content
Version: 2.3

Guards

A guard is a function that will be executed before an handler (event, slash, simple command, etc) is called.

If you're familiar with the back-end and APIs development, think of it as middlewares.

The guard will call the next handler if and only if the function next() is called within it. Otherwise, it will stop the process.

Simple example of a guard function that is meant to be used alongside the @On('messageCreate') and that will validate it only if a certain regex is matched:

src/guards/match.ts
import type { ArgsOf, GuardFunction } from "discordx"

/**
* Pass only when the message match with a passed regular expression
* @param regex The regex to test
*/
export const Match = (regex: RegExp) => {

const guard: GuardFunction<
| ArgsOf<"messageCreate">
> = async ([message], client, next) => {

if (message.content.match(regex)) next()
}

return guard
}

And use it like this:

src/commands/General/foo.ts
import { Client } from "discordx"

import { Discord, On } from "@/decorators"
import { Guard, Match } from "@/guards"

@Discord()
@Category('General')
export default class Tests {

@On('messageCreate')
@Guard(
Match(/foo/gm)
)
async matchFoo(): Promise<void> {

console.log('"foo" found!')
}
}

important

For more information, head over to the official discordx documentation.

Built-in guards

Prevent interaction from running when it is disabled, except for devs.

Usage example:

@Discord()
export default class PingCommand {

@Slash({
name: 'ping',
description: 'Pong!'
})
@Guard(
Disabled()
)
async ping(
interaction: CommandInteraction
) {

interaction.reply('Pong!')
}
}