• Guide
  • Core Concepts
  • Adding guards

Adding Guards

The @spark.ts/guards package adds easy to use premade plugins. For example, one of the helpers allow you to specify that the command must be ran in a specific guild.

Installation

npm install @spark.ts/guards
yarn add @spark.ts/guards
pnpm add @spark.ts/guards

Setup

Here's a basic example of a guard that tests if the command is ran in the test guild.

src/guards/inTestGuild.ts
import { Guard, Helpers } from '@spark.ts/guards';
 
const TEST_GUILD_ID = '123123';
 
export const inTestGuild = Guard({
  condition: Helpers.InGuild([TEST_GUILD_ID]),
  onFalse: 'This command can only be ran in the test guild!',
});

You can view all of the helpers by using the intellisense in your IDE.

Usage

Guards work on any type of command

You can use this guard in your command by importing the guard file, and using that in the plugins option.

src/commands/test.ts
import { SparkCommand } from '@spark.ts/handler';
import { inTestGuild } from '../../guards/inTestGuild.js';
 
export default new SparkCommand({
  plugins: [inTestGuild],
  run({ interaction }) {
    // This would only be executed if the guard passed
    interaction.reply('👋');
  },
});