Skip to content

Create and assign role Discord.js

I’m creating a Discord bot with discord.js, but I don’t find how to create a role and assign it automatically.

Example, a client writes: ?insc NAME @pseudo1 @pseudo2 @pseudo3

This command create the role NAME, then a category with channelVoice and channelText with access only for NAME.

For the moment, this is my code. I have successfully created my category with channelVoice and channelText without private access.

    module.exports = {
    name: 'insc',
    description: 'Inscription des equipe',
    execute(message, args, user) {
        const { PREFIX } = require('../config.js');
        const name = message.content.replace(`${PREFIX}` + this.name, '')
        message.guild.roles.create({
            data: {
                name: name,
            },
        })

        message.guild.channels.create(name, {
            type: 'category',
            permissionOverwrites: [
                {
                    id: message.guild.id,
                    deny: ['VIEW_CHANNEL'],
                    // allow: ['VIEW_CHANNEL'], //autorise @everyone à voir ce channel
                }]
        }).then(cat => {
            message.guild.channels.create(name, {
                type: 'text',
                parent: cat,
                permissionOverwrites: [
                    {
                        id: message.guild.id,
                        deny: ['VIEW_CHANNEL'],
                    }]
            })
            message.guild.channels.create(name, {
                type: 'voice',
                parent: cat,
                permissionOverwrites: [
                    {
                        id: message.guild.id,
                        deny: ['VIEW_CHANNEL'],
                    }]
            })
        })

        return message.channel.send(`L'inscription à bien été effetué pour l'équipe ${name}`);

        // return message.channel.send(`${name} existe déjà`)

    }
}

Answer

roles.create returns a promise so you need to resolve it first to get the created role’s ID. You can use this ID in your permissionOverwrites for your category and channels.

When you create a channel using channels.create, you can add an array of permissionOverwrites. This way you can tell Discord who you want to allow or deny to view the channel. In your example, you tried to deny the VIEW_CHANNEL permission for everyone, which is correct, you just need to add another object in that array with the new role’s ID.

In my example I created a permissionOverwrites array, so I only have to write it once and use it with the category, the text channel, and the voice channel.

Also, if you want to add the roles to the mentioned members, you can loop over them and add the role one by one.

Check out the following working example:

module.exports = {
  name: 'insc',
  description: 'Inscription des equipe',
  async execute(message, args, user) {
    // you already receive the args, the first one is the team name
    const name = args[0];

    // create role
    const role = await message.guild.roles.create({ data: { name } });
    const permissionOverwrites = [
      // deny access to everyone
      { id: message.guild.id, deny: ['VIEW_CHANNEL'] },
      // use the role id to allow users with the role to access the channel
      { id: role.id, allow: ['VIEW_CHANNEL'] },
    ];

    // add role to mentioned members if there are any
    message.mentions.members.each((member) => {
      member.roles.add(role.id);
    });

    // create a category and grab the created channel
    const category = await message.guild.channels.create(name, {
      type: 'category',
      permissionOverwrites,
    });

    message.guild.channels.create(name, {
      type: 'text',
      parent: category,
      permissionOverwrites,
    });

    message.guild.channels.create(name, {
      type: 'voice',
      parent: category,
      permissionOverwrites,
    });

    return message.channel.send(`L'inscription à bien été effetué pour l'équipe ${name}`);
  },
};

enter image description here enter image description here