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.