简体   繁体   English

如何动态设置所有通道 discord.js 的权限

[英]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.这仅设置执行命令的通道的权限,我想知道是否有一种方法可以为服务器中的每个通道设置权限(私有通道除外←如果不可能,则忽略它) ,而且我知道您可以使用频道 ID 为每个频道设置权限,但这仅限于 1 个 discord 服务器,我不希望这样。

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.如果您的机器人没有编辑频道的权限,则会执行catch块。 You can simply have it return , send an error message, whatever you want.您可以简单地让它return ,发送错误消息,无论您想要什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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