Add Roles to Users using a Discord.js Collector

Lucas Minter
InstructorLucas Minter
Share this video with your friends

Social Share Links

Send Tweet
Published a year ago
Updated a year ago

Now, let's learn how to add roles to users in a Discord server using a Discord.js collector.

By the end of this lesson, you will be able to create a collector to gather users who click on a specific button, filter out users who do not have a certain role, and add a specific role to the users who meet the filter criteria.

Instructor: [0:00] To add our roles to our users, we're going to use a collector. To do that, let's create a variable called Collector. That's going to be set to our finished embed message. We're going to add our Collector onto this message. Let's turn it into a variable-const-finished embed message = interaction.

[0:27] We need to make sure to await this or else it won't work. We will add our create message Component Collector. The reason we're using this specific Collector is because we are using a button, which is our component, as our method of collecting our users.

[0:48] In this Collector, we're going to add three things -- a filter, a max, which will be our number of people, and time, which will be when our Component Collector is finished. This time will be in milliseconds.

[1:06] Wait time in milliseconds. Make sure you spell correctly. Then, let's go and create that first. Wait time in milliseconds, which will be our wait time in seconds times 1,000. Now let's create that filter. Filter will be a function. Inside this function, we need to check if our users have the book club role ID or not.

[1:39] We'll return interaction.guild.members.cache.get(userid). Then check to see if they have the book club role ID. If they don't, that's when we want to return. Then we'll be able to continue on with our Collector.

[2:12] Now let's go ahead and turn our Collector on using Collect. Here, there are two different values we could be adding, it's Collector End. Collect will turn our Collector on, and End will finish our Collector. Turn this into a function. Inside here is where we're going to add the role to our users.

[2:37] First, let's get the ID of our user, and set it to a variable, interaction.guild.members.cache.get(userid). Then with that ID, we can add that book club role using the book club role ID. That's done, but we want to send a message to make sure our users know that they were added to the book club. We'll send a simple reply.

[3:22] Inside this, we'll do i.user.username so that user is printed out, has been added to the book club. Perfect. Now let's turn off our Collector, Collector End. In here, we'll go ahead and console.log.

[3:51] Now let's go and run our command again. Testing, enter, created the channel. You look at my user, you can see I don't have the role added. Thumbs up, Mentor here has been added to the book club. Let's check, I have it. Now let's check on our other user who's not an admin.

[4:18] You can see that the other user was added. We don't see the channel up here, but if we hit the thumbs up, Lucas Mentor has been added to the Book Club. Now I can see this, and I can type.

egghead
egghead
~ 36 seconds 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