简体   繁体   中英

How to dynamically set permissions for ALL channels discord.js

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM