1
votes

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);
2

2 Answers

1
votes

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.updateOverwrite(role, {
      SEND_MESSAGES: false,
      SPEAK: false,
      ADD_REACTIONS: false,
      SEND_TTS_MESSAGES: false,
      ATTACH_FILES: false 
  })
 });
} 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) => {...}
0
votes

Your problem is within these lines:

message.guild.channels.cache.forEach((channel) => {
 channel.updateOverwrite(channel.guild.roles.roleMute, {
  SEND_MESSAGES: false,
  SPEAK: false,
 });
});

You're trying to access the roleMute variable as if it was a property of the GuildRoleManager object (which it is not). Since you just defined roleMute as a singular variable:

const roleMute = message.guild.roles.cache.find(
 (role) => role.name === 'Muted'
);

All you have to do is provide that variable as a parameter.

message.guild.channels.cache.forEach((channel) => {
 channel.updateOverwrite(roleMute, {
  SEND_MESSAGES: false,
  SPEAK: false,
 });
});