Code Your Own Discord Bot - Reaction Roles (2021)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to another video today we're going to be coding a reaction role discord bot using discord.js reaction roles allows your discord members to add or remove specific roles from simply reacting to a message having this on your discord server can be very beneficial as new members will have a clear view on which roles they will be assigned let's go ahead and get started we are going to be using our code from our first three videos of the playlist there will be a link in description the first one shows you how to set up your bots the second one shows you how to add a simple command handler and the third one shows you the advanced command handler we're going to be using the advanced command handler in this video open up your main.js there's actually a couple changes that we have to do on our cons client we're going to have to actually add some partials so go ahead and add the partial message channel and reaction do remember to update your discord.js to version 12 so you're able to use these partials head down to our command handler and add our new command we're going to name this reaction role and go ahead and also pass in a discord and client this is something that we're not doing in our other commands but we are going to need these variables in our command handler let's now create our javascript file we named our command reaction role so we will be naming our file reactionrole.js inside of our commands folder inside of the file create a module exports and add both a name and a description for the name simply add the command name and for the description add whatever you want next let's add our execute method the parameters of this method are message rx discord and client just as we named them in our command handler to begin we need a variable to hold a channel id for our get roles channel if you haven't created a get rolls channel go ahead and create one right click on your channel and select copy id paste this id inside of this variable i now want to explain how our worlds will work for the example of this video i created two roles in my server blue team and yellow team when a new member joins the discord they'll be prompted to choose between these two roles and choosing a role will allow them to communicate with their teammates go ahead and create the roles you want to let people assign to themselves after creating the roles head back into visual studio and now let's add those two roles into our code to do this we create two variables we're fetching these roles from our guild.roles and finding the corresponding role by name make sure that you have your naming correctly as even capitalization matters since this is a reaction role video we're gonna need our bots to react to a message so let's go ahead and choose the one emoji for each of our teams the best way to get the emojis is going to your discord server and finding the emoji that you desire once you click on the emoji don't send the message just yet before the emoji in your message type a backslash this will reveal the emojis id and we will need this id in our code discord integrated emojis will have the ability to be copied straight into code however custom emojis will have a special id that you will need we won't be using special emojis we're just going to be using the discord emojis that come with discord yellow team will be assigned the lemon emoji and the blue team will be assigned the grape emoji yeah i know grape it's not blue i just decided to go with the grape one and then just simply create two variables that hold your emojis now we need to create a message that our bots can't react to you can send a normal message but for this video we're going to be sending an embed i have a full tutorial on embeds so if you desire to learn more about embeds there will be a link in the description to that tutorial so i'm going to be using this boilerplate code that i coded before the video for embed if you guys want to get all of the code in this tutorial there'll be a link in the description just for that now instead of sending our embed like we usually do we actually want to hold the channel that's sent inside of a variable now the reason we're doing this is because we will have to turn everything that we're currently doing into an asynchronous function before the execute method add the async keyword this will then allow us to use the await keyword right next to our message send we do this because our bot is currently sending and reacting to a message at the same time we need our bot to wait for the message to be sent to then react to it now that we have asynchronous functionality we want to tell our bot to react to our embed with our emojis so let's run our bot and see how it's currently running our command is minus sign reaction rolls and as you can see the box sends the embed and then slowly adds the reactions now currently if we react to the embed we won't receive anything so let's go ahead and change that we are going to need to create a new event listener that we haven't talked about in this channel since we imported client on our execute method we can add the message reaction add listener to our command this event takes in two parameters the reaction and the user and we also have to make this event asynchronous so add the async function before the parameters okay we have our event but first we have to check some things so our code doesn't break we start off by using those partials that we added at the beginning of this video just add these two lines of code as we work with the partials to await responses let me go over how this listener works any member user entity that reacts with this embed will get passed down into this event we can then use the event to basically change their roles however the bot itself is a member slash user slash entity so we need to stop the bot from entering this event and our last check is checking that our reaction is indeed in our guild aka server moving on to the final checkpoint of this video we need to check that this reaction event is happening in a specific channel that we designated if we don't have this check our bots will look all over the server for all sorts of reactions and we don't want that this leads us to finally adding a role with each reaction we are checking this by making sure the emoji name of the reaction matches the emoji name of our teams so if the reaction emoji equals yellow team emoji then we add the yellow team role again we want to await for the previous task to be completed so to finally finish this off we're gonna get the user from the reaction parameter that we imported in our method after the user we just get the id and we simply just add the yellow team role we then need to create another if statement for our blue team this is pretty much the exact same thing we just changed the emoji name to the blue team emoji and the add method to add the blue team instead of the yellow team role there is something that we have to do as well and that is create an else statement to the if channel id equals our channel id to just return now if we want our bots and we react to the embed we will be assigned a role however we also want to add the ability to remove a role by unreacting from the embed it's pretty simple just copy and paste our message reaction add event listener then change the message reaction add to message reaction remove then head down to the rows that add methods and change them to rows dot remove this is actually all you need to create reaction rules let's go ahead and check out what we did in our get rolls channel we will first send the embed and then of course delete our own message we currently only have one role and that is the admin role so let's give ourselves the yellow team role and as you can see we are now part of the yellow team but let's say that we want to change teams we remove the role by unreacting and then react to the blue team boom everything works as intended well this is it for this video with this code you can pretty much customize your roles as much as you like thanks so much for watching let me know how you guys like this kind of format of the video i had a lot of fun creating this tutorial with that being said thanks again peace
Info
Channel: CodeLyon
Views: 72,327
Rating: undefined out of 5
Keywords: code your own discord bot, code your own discord bot - basics (2020), code your own discord bot 2020, how to code your own discord bot 2020, make your own discord bot, make your own discord bot 2020, make your own discord bot commands, discord bot react to get role, discord bot reaction roles, discord bot reaction roles code, discord bot tutorial, code discord bot, reaction role discord bot, discord js reaction role, discord reaction role, reaction roles discord
Id: wXjsCiUjUqo
Channel Id: undefined
Length: 8min 40sec (520 seconds)
Published: Tue Dec 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.