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.

const { Command } = require('@sapphire/framework');
const { PermissionFlagsBits } = require('discord.js');

class BanCommand extends Command {
constructor(context) {
super(context, {
// ...
requiredUserPermissions: [PermissionFlagsBits.BanMembers]
});
}
}
module.exports = {
BanCommand
};

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.

const { Command } = require('@sapphire/framework');
const { PermissionFlagsBits } = require('discord.js');

class BanCommand extends Command {
constructor(context) {
super(context, {
// ...
requiredUserPermissions: [PermissionFlagsBits.BanMembers],
requiredClientPermissions: [PermissionFlagsBits.BanMembers]
});
}
}
module.exports = {
BanCommand
};

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.