I am trying to make a discord bot. When I type ?mute @role, I want my bot to create a 'Muted' role and remove the SEND_MESSAGES
and SPEAK
permissions for that role in every channel in a server. I have it to where it will add the role to the person, but so far I can't get it to set permissions. I'm using discord.js v12. My code is below. Bear with me because I'm not experienced at javascript and I haven't posted questions in StackOverflow before either.
if (!message.member.permissions.has('KICK_MEMBERS'))
return message.channel.send(
"*You don't have permission to use this command.*"
);
const role = message.guild.roles.cache.find((role) => role.name === 'Muted');
const member3 = message.guild.member(user);
if (!role) {
message.guild.roles
.create({
data: {
name: 'Muted',
color: 'GREY',
},
reason: 'Created role to mute member',
})
.then(console.log)
.catch(console.error);
}
if (!user) {
message.channel.send(`There's no person to mute tho`);
return;
}
if (member3.permissions.has('ADMINISTRATOR')) {
return message.channel.send(`I can't mute ${user} because he is staff`);
}
const roleMute = message.guild.roles.cache.find(
(role) => role.name === 'Muted'
);
message.guild.channels.cache.forEach((channel) => {
channel.updateOverwrite(channel.guild.roles.roleMute, {
SEND_MESSAGES: false,
SPEAK: false,
});
});
member3.roles.add(roleMute);
You almost have it right. Try this to create your role and overwrite the permissions. It's good practice to put both in a try...catch
block so if there is an error it gets handled.
try {
role = await message.guild.roles.create({
data: {
name: 'Muted',
color: '#514f48',
permissions: [],
},
});
message.guild.channels.cache.forEach(async (channel, id) => {
await channel.overwritePermissions([
{
id: role.id,
deny: [
'SEND_MESSAGES',
'ADD_REACTIONS',
'SEND_TTS_MESSAGES',
'ATTACH_FILES',
'SPEAK',
],
},
]);
});
} catch (e) {
console.log(e.stack);
}
Once that is done you can leave out this part in your code
message.guild.channels.cache.forEach((channel) => {...}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments