'Setting permissions for commands in discord.js

I am relatively new to javascript.

I am trying to set permissions for commands in discord.js.

The code I have:

const permissions = [ ]

for (i = 0; i < config.author.length; i++) { // config.author is a list of IDs in config.json that want to be able to use admin commands
  permissions.push({
    id: config.author[i],
    type: 'USER',
    permission: false
  })
}

for (const file of commandFiles) {
  const command = require(`./commands/${file}`);
  commands.push(command.data.toJSON());
}
for (const file of adminFiles) {
  const command = require(`./admincmd/${file}`);
  command.permissions.add({ // ERROR HERE
    defaultPermission: false,
    command: slashCommand.id,
    permissions: permissions
  });
  admincmds.push(command.data.toJSON());
}

Whenever I try to run my deploy.js file with this code in it I get the following error:

deploy.js:31
  command.permissions.add({
                      ^

TypeError: Cannot read properties of undefined (reading 'add')
    at Object.<anonymous> (C:\Users\me\Documents\Code\Discord Bots\bot\deploy.js:31:23)
←[90m    at Module._compile (node:internal/modules/cjs/loader:1101:14)←[39m
←[90m    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)←[39m
←[90m    at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)←[39m
←[90m    at node:internal/main/run_main_module:17:47←[39m

command.permissions.add({...}) is what is says at discordjs.guide, so I don't understand what I am doing wrong. Does this code not belong in deploy.js at all?



Solution 1:[1]

In every commands you created, you can set if the member can use this command

const { Permissions } = require('discord.js')
if(!<client>.member.permissions.has(Permissions.FLAGS.YOUR_PERMISSIONS)) return message.reply("YOUR_MESSAGE")

Same with your bot if the bot can use this command

const { Permissions } = require('discord.js')
if(!<client>.guild.me.permissions.has(Permissions.FLAGS.YOUR_PERMISSIONS)) return message.reply("YOUR_MESSAGE")

Is this what you wanted to do?

EDIT:

Using message.author.id method

const allowed = "ID", "ID", "ID"
if (message.author.id === allowed) {
//only executes if the author listed
} else {
//only executes if the author is not listed
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1