Search code examples
discord.js

How do I add a slash command for certain users to use?


How do I make a slash command for a specific user that has the role "Moderator"? When other users try to use it it will be disabled.


Solution

  • You can add permissions to a certain slash command by using the command.permissions.add() function. Your code will look like this:

    const command = client.application?.commands.fetch('YOUR_COMMAND_ID');
    
    const permissions = [{
        id: 'ROLE_ID',
        type: 'ROLE',
        permission: true,
    }];
    
    await command.permissions.add({permissions});
    

    Source: https://discordjs.guide/interactions/slash-command-permissions.html#role-permissions

    Edit:

    As mentioned by @SollyBunny in the comments, the page has been removed. Here is another source: https://discord.js.org/#/docs/discord.js/main/class/ApplicationCommandPermissionsManager