Slash command permissions

Slash commands have their own permissions system. This system allows you to set the default level of permissions required for a user to execute a command when it is first deployed or your bot is added to a new server.

The slash command permissions for guilds are defaults only and can be altered by guild administrators, allowing them to configure access however best suits their moderation and server roles. Your code should not try to enforce its own permission management, as this can result in a conflict between the server-configured permissions and your bot's code.

WARNING

It is not possible to prevent users with Administrator permissions from using any commands deployed globally or specifically to their guild. Think twice before creating "dev-only" commands such as eval.

Member permissions

You can use SlashCommandBuilder#setDefaultMemberPermissions()open in new window to set the default permissions required for a member to run the command. Setting it to 0 will prohibit anyone in a guild from using the command unless a specific overwrite is configured or the user has the Administrator permission flag.

For this, we'll introduce two common and simple moderation commands: ban and kick. For a ban command, a sensible default is to ensure that users already have the Discord permission BanMembers in order to use it.

const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');

const data = new SlashCommandBuilder()
	.setName('ban')
	.setDescription('Select a member and ban them.')
	.addUserOption(option =>
		option
			.setName('target')
			.setDescription('The member to ban')
			.setRequired(true))
	.setDefaultMemberPermissions(PermissionFlagsBits.BanMembers);










 
1
2
3
4
5
6
7
8
9
10
11

For a kick command however, we can allow members with either BanMembers or KickMembers to execute the command, so we'll list both flags here joined by the bitwise OR operator.

TIP

If you want to learn more about the | bitwise OR operator you can check the Wikipediaopen in new window and MDNopen in new window articles on the topic.

const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');

const data = new SlashCommandBuilder()
	.setName('kick')
	.setDescription('Select a member and kick them.')
	.addUserOption(option =>
		option
			.setName('target')
			.setDescription('The member to kick')
			.setRequired(true))
	.setDefaultMemberPermissions(PermissionFlagsBits.BanMembers | PermissionFlagsBits.KickMembers);










 
1
2
3
4
5
6
7
8
9
10
11

In reality, you'll probably want to have an additional confirmation step before a ban actually executes. Check out the button components section of the guide to see how to add confirmation buttons to your command responses, and listen to button clicks.

DM permission

By default, globally-deployed commands are also available for use in DMs. You can use SlashCommandBuilder#setDMPermission()open in new window to disable this behaviour. Commands deployed to specific guilds are not available in DMs.

It doesn't make much sense for your ban command to be available in DMs, so you can add setDMPermission(false) to the builder to remove it:

const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');

const data = new SlashCommandBuilder()
	.setName('ban')
	.setDescription('Select a member and ban them.')
	.addUserOption(option =>
		option
			.setName('target')
			.setDescription('The member to ban')
			.setRequired(true))
	.setDefaultMemberPermissions(PermissionFlagsBits.BanMembers)
	.setDMPermission(false);










 
 
1
2
3
4
5
6
7
8
9
10
11
12

And that's all you need to know on slash command permissions!