授予角色与新创建的频道 Discord.JS 交互的权限

时间:2021-01-04 17:41:41

标签: javascript node.js discord.js

我正在使用 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);

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

有什么方法可以让我提到的用户与这个新频道互动(查看、发送消息、添加反应...)?

1 个答案:

答案 0 :(得分: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);
    })
相关问题