Im trying to add permission to commands in such way, that only users with specific role can use it. At first im creating commands like its said in documentation.
Then Im trying to get each command and adding to them new permissions:
await commands.forEach(command => {
const permissions2 = [
{
id: guild.roles.everyone.id,
type: 'ROLE',
permission: false,
}
];
const permissions1 = [
{
id: botRole.id,
type: 'ROLE',
permission: true,
},
];
console.log(`Changing command ${command.id}`);
command.permissions.add(permissions2);
command.permissions.add(permissions1);
});
But whatever I do I get this error: TypeError [INVALID_TYPE]: Supplied permissions is not an Array of ApplicationCommandPermissionData.
Ive also tried running this code as shown in documentation but got same result:
await commands.forEach(command => {
...
console.log(`Changing command ${command.id}`);
command.permissions.add({permissions2});
command.permissions.add({permissions1});
});
Changing code to this helped:
const permissions2 = {
id: guild.roles.everyone.id,
type: 'ROLE',
permission: false,
};
const permissions1 = {
id: botRole.id,
type: 'ROLE',
permission: true,
};
let commandsList = await guild.commands.fetch();
await commandsList.forEach(slashCommand => {
console.log(`Changing command ${slashCommand.id}`);
//set the permissions for each slashCommand
guild.commands.permissions.add({
command: slashCommand.id,
permissions: [permissions1, permissions2]
});
});
I dont think you can use the forEach loop like you did.
commands.permissions#set consists of an object with the id of the command you want to edit, and an array with the permissions.
So you would have to rewrite your code to this:
//create the permissions objects
const permissions2 = {
id: guild.roles.everyone.id,
type: 'ROLE',
permission: false,
};
const permissions1 = {
id: botRole.id,
type: 'ROLE',
permission: true,
};
//loop through all the slashCommands
await commands.forEach(slashCommand => {
console.log(`Changing command ${slashCommand.id}`);
//set the permissions for each slashCommand
client.application.commands.permissions.set({command: slashCommand.id, permissions: [permissions1, permissions2]});
});
Read more about setting permissions for slashCommands here