Skip to content
Advertisement

How to add permissions to user to channel by command? Discord.js

How to give permissions to a specific channel by command? Sorry, I’m new at discord.js so any help would be appreciated.

const Discord = require('discord.js');

module.exports = {
 name: 'addrole',
 run: async (bot, message, args) => {
  //!addrole @user RoleName
  let rMember =
   message.guild.member(message.mentions.users.first()) ||
   message.guild.members.cache.get(args[0]);
  if (!rMember) return message.reply("Couldn't find that user, yo.");
  let role = args.join(' ').slice(22);
  if (!role) return message.reply('Specify a role!');
  let gRole = message.guild.roles.cache.find((r) => r.name === role);
  if (!gRole) return message.reply("Couldn't find that role.");

  if (rMember.roles.has(gRole.id));
  await rMember.addRole(gRole.id);

  try {
   const oofas = new Discord.MessageEmbed()
    .setTitle('something')
    .setColor(`#000000`)
    .setDescription(`Congrats, you have been given the role ${gRole.name}`);
   await rMember.send(oofas);
  } catch (e) {
   message.channel.send(
    `Congrats, you have been given the role ${gRole.name}. We tried to DM `
   );
  }
 },
};

Advertisement

Answer

You can use GuildChannel.updateOverwrites() to update the permissions on a channel.

// Update or Create permission overwrites for a message author
message.channel.updateOverwrite(message.author, {
 SEND_MESSAGES: false
})
  .then(channel => console.log(channel.permissionOverwrites.get(message.author.id)))
  .catch(console.error);

(From example in the discord.js docs)

Using this function, you can provide a User or Role Object or ID of which to update permissions (in your case, you can use gRole).

Then, you can list the permissions to update followed by true, to allow, or false, to reject.

Here is a full list of permission flags you can use

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement