Home > Back-end >  Setting permissions for commands in discord.js
Setting permissions for commands in discord.js

Time:03-21

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?

CodePudding user response:

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?

CodePudding user response:

Please check the documentation

const { Permissions } = require('discord.js');
const permissions = new Permissions([
    Permissions.FLAGS.VIEW_CHANNEL,
    Permissions.FLAGS.EMBED_LINKS,
    Permissions.FLAGS.ATTACH_FILES,
    Permissions.FLAGS.READ_MESSAGE_HISTORY,
    Permissions.FLAGS.MANAGE_ROLES,
]);

console.log(permissions.has(Permissions.FLAGS.KICK_MEMBERS));
// output: false

permissions.add(Permissions.FLAGS.KICK_MEMBERS);
console.log(permissions.has(Permissions.FLAGS.KICK_MEMBERS));
// output: true

permissions.remove(Permissions.FLAGS.KICK_MEMBERS);
console.log(permissions.has(Permissions.FLAGS.KICK_MEMBERS));
// output: false
  • Related