Overwrite permissions for discord bot to mute a role in all text/voice channels

Seth Finch

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);
Worthy Alpaca

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Discord bot add role

From Dev

Sending a message to all channels — Discord.py

From Dev

Discord python bot problem with giveng role

From Dev

Discord Python Bot Add Color to Role

From Dev

Im trying to do a reaction role bot on discord

From Dev

Grant a role through Discord Bot in C#?

From Dev

discord bot removing one role but not the rest

From Dev

Slack Bot to see all Direct Message Channels

From Dev

Discord bot fails to add a role to a user using discord.py

From Dev

Discord Bot Auto Deletes Https Links In certain Channels

From Dev

How to make a bot join voice channels discord.py

From Dev

Finding all members with a role in Discord.js

From Dev

Programming a Discord bot in Python- How would I make it so my mute command is timed?

From Dev

Programming a Discord bot in Python- How do I make a mute command?

From Dev

Discord.py bot, assign role when a user adds reaction

From Dev

discord.py make bot kick users with specified role

From Dev

Discord - How to give my bot permissions. [Javascript]

From Dev

How to display all permissions a role has in laravel 5 entrust?

From Dev

How to display all permissions a role has in laravel 5 entrust?

From Dev

How to overwrite TextChannel permissions

From Dev

C# bot doesn't connect to voice channels (Discord.Net)

From Dev

How to make my bot give send messages permissions to the specified role id , when prompted "=heist roleid"

From Dev

Advanced Role Permissions in Rails

From Dev

Ansible overwrite hosts in role dependencies

From Dev

How to create bot command that give role to mentioned user | Discord.py

From Dev

How do I get my discord bot to check if a member has a role?

From Dev

Mute all <audio> elements with jQuery

From Dev

Discord mute command received TypeError: fn is not a function

From Dev

Mute command not working, no error message; Discord python

Related Related

  1. 1

    Discord bot add role

  2. 2

    Sending a message to all channels — Discord.py

  3. 3

    Discord python bot problem with giveng role

  4. 4

    Discord Python Bot Add Color to Role

  5. 5

    Im trying to do a reaction role bot on discord

  6. 6

    Grant a role through Discord Bot in C#?

  7. 7

    discord bot removing one role but not the rest

  8. 8

    Slack Bot to see all Direct Message Channels

  9. 9

    Discord bot fails to add a role to a user using discord.py

  10. 10

    Discord Bot Auto Deletes Https Links In certain Channels

  11. 11

    How to make a bot join voice channels discord.py

  12. 12

    Finding all members with a role in Discord.js

  13. 13

    Programming a Discord bot in Python- How would I make it so my mute command is timed?

  14. 14

    Programming a Discord bot in Python- How do I make a mute command?

  15. 15

    Discord.py bot, assign role when a user adds reaction

  16. 16

    discord.py make bot kick users with specified role

  17. 17

    Discord - How to give my bot permissions. [Javascript]

  18. 18

    How to display all permissions a role has in laravel 5 entrust?

  19. 19

    How to display all permissions a role has in laravel 5 entrust?

  20. 20

    How to overwrite TextChannel permissions

  21. 21

    C# bot doesn't connect to voice channels (Discord.Net)

  22. 22

    How to make my bot give send messages permissions to the specified role id , when prompted "=heist roleid"

  23. 23

    Advanced Role Permissions in Rails

  24. 24

    Ansible overwrite hosts in role dependencies

  25. 25

    How to create bot command that give role to mentioned user | Discord.py

  26. 26

    How do I get my discord bot to check if a member has a role?

  27. 27

    Mute all <audio> elements with jQuery

  28. 28

    Discord mute command received TypeError: fn is not a function

  29. 29

    Mute command not working, no error message; Discord python

HotTag

Archive