0

我正在使用 Node.JS 创建一个不和谐机器人,其中使用相同的命令创建了一个新角色和频道。为了简单起见,角色名称和频道名称都是命令的第一个参数:

let chanName = args[0];
let roleName = args[0];

角色/频道名称之后是提及列表 - 这些是接收新角色的用户。角色创建得很好,频道创建得很好,成员被添加到角色中也很好,但是我在设置权限时遇到问题,只有具有新角色或“管理员”角色的用户才能看频道。

通道是这样创建的:

message.guild.channels.create(chanName, {
    type: "text"
})
.then((channel) => {
    //Each new channel is created under the same category
    const categoryID = '18-digit-category-id';
    channel.setParent(categoryID);
})
.catch(console.error);

好像频道没有添加到公会缓存中

let chan = message.guild.channels.cache.find(channel => channel.name === chanName);

因为此代码将“未定义”返回到控制台。

有什么方法可以让上述用户与这个新频道进行交互(查看、发送消息、添加反应......)?

4

1 回答 1

0

创建频道时设置权限非常简单!多亏了GuildChannelManager#create()为我们提供的多个选项,我们可以非常简单地为我们想要的角色和成员添加权限覆盖。我们可以非常简单地获取您的代码并添加一个permissionOverwrites部分来设置频道的权限,如下所示:

message.guild.channels.create(chanName, {
        type: "text",
        permissionOverwrites: [ 
           // keep in mind permissionOverwrites work off id's!
            {
                id: role.id, // "role" obviously resembles the role object you've just created
                allow: ['VIEW_CHANNEL']
            }, 
            {
                id: administratorRole.id, // "administratorRole" obviously resembles your Administrator Role object
                allow: ['VIEW_CHANNEL']
            },
            {
                id: message.guild.id,
                deny: ['VIEW_CHANNEL']
            }
        ]
    })
    .then((channel) => {
        //Each new channel is created under the same category
        const categoryID = '18-digit-category-id';
        channel.setParent(categoryID);
    })
于 2021-01-04T18:01:08.153 回答