Create Dynamic Channels and Roles with a Discord Bot

Lucas Minter
InstructorLucas Minter
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Let's now learn how to create dynamic channels and roles with our Discord bot.

We want to be able to create new roles and channels for the book clubs and their attendants that we create in our server.

We'll be using methods from the interaction object to create the role and channel. And, we'll make sure to create both the role and channel each with the correct permissions.

Instructor: [0:01] We'll start off by creating our role, which will be interaction.guild.roles.create. This will be an object. First thing we'll add in is our name for the role, which will be Book Club. Then we need to add in the permissions for that role. This will be an object. First thing will be permissions bitfield. Make sure to import that.

[0:35] On here we'll add Flags.ViewChannel, since we want our users to be able to view channels. Then we need them to be able to send messages. With that created, let's now create our channel, interaction.guild.channels.create.

[0:58] Again, the first thing we'll add in is the name. This will be Book Club, just like before. We'll add in the type. Now, this will be the type of channel. We want this to be a text channel, so channel type, 'GUILD_TEXT'. Make sure to import channel type as well. Then below that, we need to set the permissions.

[1:26] Now, currently, when we create this channel, anyone's going to have access. We only want people in our Book Club to have access to this. We'll first overwrite the permissions, Overwrites. This will be an array of objects. The first object, we want the ID of everybody in our guild, so interaction.guild.id.

[1:55] We want to deny them from being able to view the channel, Flags.ViewChannel. Then, for our second object, we want the ID of our Book Club role. We'll do bookClubRoleId, which, we'll create that variable in just a second, and allow them to be able to send messages and view this channel.

[2:30] All right. Now, let's create that Book Club role ID, const bookClubRoleId. This will be set to interaction.guild.roles.cache.find. This will be a function where we're trying to find the role where the name is equal to Book Club and get the id of that. Now we'll save.

[2:58] If we run this as is, we're going to get an error most of the time. That is because, if you remember a couple of lessons ago, I talked about how Discord.js is promise based. Back up in our Execute, we want to add async. Then we're going to add await to our role creation, await for getting our ID, and await for channel creation.

[3:29] Let's go ahead and save that and head over to the Discord application, where we will run our command again. Testing, one, two, three, with three people and one hours, hit enter.

[3:43] You can see we've created our channel. Click on that. You can see we also have our role created and added to our channel. Then over on the right, you can see which members have been added to this channel.

[3:57] Currently, it's myself and a couple of our other bots that are selected as admins. To prove to you that nobody else can see this, I have another account open right here. I don't see that channel here at all.

egghead
egghead
~ 4 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today