discord.js

discord.js v14 add member to a channel


I would like to add some members to the specific channel in Guild so only they are able to see the channel. I am creating a new channel via this script

const channel = await guild.channels.create({
    parent: category.id,
    name: 'test-room',
    type: ChannelType.GuildText,
});

I would also like to add some specific role to this channel so each member of the role can see the channel

More clarification: imagine you have a channel for each member joined to the discord. Something like a support channel. Each one member has its own channel that only he and the support team can see. So I need to create a channel and add permissions for the support team and for the given user to be able to see and write to that channel


Solution

  • You can add permission overwrites to the channel:

    const channel = await guild.channels.create({
        parent: category.id,
        name: 'test-room',
        type: ChannelType.GuildText,
        permissionOverwrites: [
            { // disallow everyone to see the channel
                id: guild.id,
                deny: [PermissionsBitField.Flags.ViewChannel],
            },
            { // allow the user to see the channel
                id: theUser.id,
                allow: [PermissionsBitField.Flags.ViewChannel],
            },
            { // allow the user to see the channel
                id: supportTeamRole.id,
                allow: [PermissionsBitField.Flags.ViewChannel],
            },
        ],
    });
    

    Then only the user and the admin can see this channel.
    Further Information: Adding overwrites