Skip to main content

Handling permissions

One of the most basic needs of a Discord bot is to be able to deny command access to users based on their permissions or lack thereof. A common example would be moderation commands. Most people wouldn't want regular users to be able to ban people, so we can restrict usage using the requiredUserPermissions option.

import { Command } from '@sapphire/framework';

export class BanCommand extends Command {
public constructor(context: Command.Context) {
super(context, {
// ...
requiredUserPermissions: ['BAN_MEMBERS']
});
}
}

Users without the BAN_MEMBERS permission will now be unable to use the command!

Handling Client Permissions

It's also a good idea to verify the inverse: does the bot have the BAN_MEMBERS permission? We can use the requiredClientPermissions option the same way to prevent the command from being used if not.

import { Command } from '@sapphire/framework';

export class BanCommand extends Command {
public constructor(context: Command.Context) {
super(context, {
// ...
requiredUserPermissions: ['BAN_MEMBERS'],
requiredClientPermissions: ['BAN_MEMBERS']
});
}
}

With these changes, BanCommand now requires the command executor and the client to have the BAN_MEMBERS permission to execute!

tip

To learn how to send a message to the command executor when a precondition fails, see Reporting Precondition Failure.