Skip to content
Advertisement

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.

JavaScript

Advertisement

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:

JavaScript

enter image description here enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement