I'm following the Discord.js guide to create slash commands for my bot, but I'm stuck at this point here:
https://discordjs.guide/interactions/slash-command-permissions.html#user-permissions
There are two things I can't figure out:
I'm creating my commands using the SlashCommandBuilder and a deploy-commands.js script as described here:
https://discordjs.guide/creating-your-bot/creating-commands.html#command-deployment-script
and here:
https://discordjs.guide/interactions/registering-slash-commands.html#guild-commands
If you can help me on either one of these two things, that would be great!
Thanks!
I've looked into the codes of the @discordjs/builders and @discordjs/rest and there is no way to set custom permissions with these packages. What you can do is create the slash commands with the Discord.js package. By creating them in the Discord.js package the id of the slash command will be returned in the fullfilled Promise. With this id you can set the permissions for the command. The only problem by doing it in this way is that it can take a while before the slash commands are working again. Here is an example:
const { Client } = require('discord.js');
const client = new Client({intents: ['Your Intents here']});
client.once('ready', () => {
client.application.commands.create({
name: 'your_command',
description: "Your command's description"
}).then(id => {
client.application.commands.set({command: id, permissions: [
id: 'some_user_id',
type: 'USER',
permission: false // Can not use the slash command
]}).catch(console.log);
});
});
client.login('Your token here');
I thought that there is another way to do it, but I'm not pretty sure. If I'm right you can also fetch all the commands after you've refreshed them with the @discordjs/builders and @discordjs/rest packages. Once you've fetched them the Promise will return a Collection once it's got fullfilled. In the Collection will be all the ids of all the slash commands which you can use to set the permissions. So if this theory works, this will be the example:
const { Client } = require('discord.js');
const client = new Client({intents: ['Your Intents here']});
client.once('ready', () => {
// Your refresh code here
client.application.commands.fetch().then(collection => {
collection.forEach(command => {
if(command.name === `The specified command name`){
client.application.commands.permissions.set({command: command.id, permissions: [
{
id: 'some_user_id',
type: 'USER',
permission: false // Can not use the slash command
}
]}).catch(console.log);
}
});
}).catch(console.log);
});
client.login('Your token here');