0
votes

I'm using Node.JS to create a discord bot in which a new role and channel are created with the same command. For the sake of simplicity, both the name of the role and the name of the channel are the first argument of the command:

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

Following the name of the role/channel is a list of mentions - these are the users that receive the new role. The role is created just fine, the channel is created just fine, and the members are added to the role just fine, but I'm having trouble setting up permissions where only users with the new role OR the 'administrator' role are able to see the channel.

The channel is created like so:

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);

It appears as though the channel is not added to the guild cache

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

because this code returns 'undefined' to the console.

Is there any way I can allow the mentioned users to interact (view, send messages, add reactions...) with this new channel?

1

1 Answers

0
votes

Permissions are very simple to set up upon creating a channel! Thanks to the multiple options GuildChannelManager#create() gives us, we're very simply able to add permission overwrites to our desired roles and members. We could very simply take your code and add a permissionOverwrites section to set the channel's permissions, as follows:

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);
    })