How to give permissions to a specific channel by command? Sorry, I’m new at discord.js so any help would be appreciated.
JavaScript
x
32
32
1
const Discord = require('discord.js');
2
3
module.exports = {
4
name: 'addrole',
5
run: async (bot, message, args) => {
6
//!addrole @user RoleName
7
let rMember =
8
message.guild.member(message.mentions.users.first()) ||
9
message.guild.members.cache.get(args[0]);
10
if (!rMember) return message.reply("Couldn't find that user, yo.");
11
let role = args.join(' ').slice(22);
12
if (!role) return message.reply('Specify a role!');
13
let gRole = message.guild.roles.cache.find((r) => r.name === role);
14
if (!gRole) return message.reply("Couldn't find that role.");
15
16
if (rMember.roles.has(gRole.id));
17
await rMember.addRole(gRole.id);
18
19
try {
20
const oofas = new Discord.MessageEmbed()
21
.setTitle('something')
22
.setColor(`#000000`)
23
.setDescription(`Congrats, you have been given the role ${gRole.name}`);
24
await rMember.send(oofas);
25
} catch (e) {
26
message.channel.send(
27
`Congrats, you have been given the role ${gRole.name}. We tried to DM `
28
);
29
}
30
},
31
};
32
Advertisement
Answer
You can use GuildChannel.updateOverwrites()
to update the permissions on a channel.
JavaScript171// Update or Create permission overwrites for a message author
2message.channel.updateOverwrite(message.author, {
3SEND_MESSAGES: false
4})
5.then(channel => console.log(channel.permissionOverwrites.get(message.author.id)))
6.catch(console.error);
7
(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.