Reputation: 59
So I was making a mute command, and I couldn't find a way to set permissions for every single channel and exclude changing permissions for private channels.
So for example, setting channel permissions is as follows:
message.channel.updateOverwrite(role1, {
SEND_MESSAGES: false,
SPEAK: false,
ADD_REACTIONS: false,
READ_MESSAGE_HISTORY: true
});
message.channel.updateOverwrite(role2, {
SEND_MESSAGES: null,
SPEAK: null,
ADD_REACTIONS: null,
})
This only sets the permissions for the channel in which the command was executed, I would like to know if there is a way to set permissions for every single channel in the server (except private channels ← if this isn't possible then ignore it), and I know you could set permissions for every single channel using channel IDs but that would be limited to 1 discord server and I don't want that.
Upvotes: 1
Views: 1693
Reputation: 183
You could loop through all channels in the server and mute them using something like this
message.guild.channels.cache.forEach(channel => { //Get each channel
if (channel.type === "text") { //Check if it's a text channel
try {
channel.updateOverwrite(role1, {
SEND_MESSAGES: false,
SPEAK: false,
ADD_REACTIONS: false,
READ_MESSAGE_HISTORY: true
});
channel.updateOverwrite(role2, {
SEND_MESSAGES: null,
SPEAK: null,
ADD_REACTIONS: null,
});
} catch (error) { //Run this if there was an error setting the permissions
//Error handling code here
};
};
});
All this code is doing is getting a list of every channel in the server, ignoring the voice channels, and setting the permissions. If your bot doesn't have permission to edit the channel, the catch
block is executed. You can simply have it return
, send an error message, whatever you want.
Upvotes: 1