How to create discord category with permissions using discord.js?

by giovanny.lueilwitz , in category: Javascript , 21 days ago

How to create discord category with permissions using discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by domenico , 21 days ago

@giovanny.lueilwitz 

To create a discord category with permissions using discord.js, you will need to use the Discord.js library in Node.js. Here is an example code snippet showing how to create a category called "My Category" with permissions for a specific role:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const Discord = require('discord.js');
const client = new Discord.Client();
const { token } = require('./config.json');

client.on('ready', () => {
    console.log('Bot is online!');
});

client.on('message', async message => {
    if (message.content === '!createCategory') {
        const guild = message.guild;

        // Create the category
        const category = await guild.channels.create('My Category', {
            type: 'category',
            permissionOverwrites: [
                {
                    id: message.guild.roles.cache.find(role => role.name === 'RoleName').id,
                    allow: ['VIEW_CHANNEL']
                },
                {
                    id: message.guild.id,
                    deny: ['VIEW_CHANNEL']
                }
            ]
        });

        message.channel.send('Category created successfully!');
    }
});

client.login(token);


Make sure to replace 'RoleName' with the name of the role you want to give permissions to and replace 'token' in the client.login function with your own Discord Bot Token.


This code will create a new category called "My Category" with permissions for the specified role to view the channel, and deny permission for everyone else to view the channel. You can modify the permissionOverwrites array to set different permissions for other roles or users.