Skip to content
Centurion

Guards

A command guard is simply a function that can be used to determine if a command should be executed.

Guards can be assigned in three ways:

  • At the class-level, which assigns the guard to all commands in the class. Guards assigned at the class-level will be executed before method-level guards.
  • At the method-level, which assigns the guard to a single command.
  • Through the guards option when starting Centurion, which assigns the guard to all commands.

A guard function is passed the same CommandContext that will be passed to the command. You can use this to access data such as the command path or executor, and you can send a reply using its reply and error methods.

A guard function should return a boolean - true if the command should be executed, false if not.

const isAdmin: CommandGuard = (ctx) => {
if (ctx.executor.UserId !== 1) {
ctx.error("Insufficient permission!");
return false;
}
return true;
};
@Register()
class KickCommand {
@Command({
name: "kick",
description: "Kick a player",
arguments: [
{
name: "player",
description: "Player to kick",
type: CenturionType.Player
}
]
})
@Guard(isAdmin)
kick(ctx: CommandContext, player: Player) {
player.Kick("You have been kicked from the server.");
ctx.reply(`Successfully kicked ${player.Name}`);
}
}