Code Your Own Discord Bot - Permissions & Roles (2021)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back to another discord.js tutorial today's video we're going to be focused on creating different permissions for different commands permissions can be a very complicated topic when we're talking about creating your own discord bots through discord.js but in this video we're going to be doing only two specific things that we're going to be implementing using you know this permissions method the first thing we're going to be doing is be able to check if a member whoever set the message has a specific permission to be able to perform that command if they do we're gonna do whatever the command needs to do if they don't we're just gonna send a message saying they don't have sufficient permissions to be able to use the command and the second thing we're going to be doing is basically just adding and removing discord permissions slash roles um through a discord.js command and that's pretty simple i'm actually going to have a link in description to another website that kind of talks about the actual hard advance in detailed information about permissions because this can get very complicated about overriding if you want to check if it has a specific discord implemented permission then we have to go ahead and do some advanced stuff so i'll leave that to the little article that i'll link in the description below so if you're completely new here make sure to watch the first and second video and those videos that basically tell you how to set up your disk or bots as well as your coding environments and then the other video i teach you how to set up commands in this video we're going to be using the command handler that we created last video so if you want to go ahead and watch that you can do that if you don't then you don't necessarily have to have this command handler as we're using here so let's go ahead and get started first thing we actually have to do is go into our discord server and basically create a world that we're going to be interacting in this video so go up here and go into server settings and i actually already created a role i just named it mod and made the color red and basically has very very basic permissions i actually didn't change anything i do want to tell you guys you have to come up here and allow anyone to mention this role turn that on and the reason we're going to turn this on is because this is the only way to get the role id which is something that we're going to be using in our code so for now make sure to allow anyone to mention this role you can definitely turn this off later so people don't start messaging the world you know by tagging them which is something that is not very popular on discord servers so once you turn this on the first thing we're gonna need is probably get the role id each role on discord has its id that you can use through the code we're also going to talk about another method that you don't necessarily need the id but getting the id is actually something that is a safer method of doing this so to get the id of the role all you're going to do is do backslash at and then the role name so we name the mod and that's actually going to give us this uh kind of code here and this right here is our role id once again you can only do this if you have all the way here if you have this turned on you can turn it off like that and then no one else were able to tag it and you already have the id right here let's actually go into our code so we created a command last episode where it basically just sends the youtube link when they do the youtube command very basic but we're going to be changing that so only people with the mod permission are going to be able to basically interact with all of our code here so the first thing we do is we are going to check if they have the role so we're going to do if message that member member that rolls that cache sorry cache dot has and then here open the string and then insert the role id that we got and then we're going to copy this code like this and that's basically saying that if the member has this role you're going to be able to send this message and here you can basically code all of your commands methods and functions in here else we are just going to simply i'm just going to copy this right here we're going to simply just send them a message saying that you don't have you know sufficient uh permission so you uh can't you know uh send this command because uh i really can't type tonight because you don't have the right permissions right something like that you can basically customize it as much as you want so let's go ahead and open up our terminal and we're going to run our code here let me actually save this as well just like that and oh that's not exactly what i meant to do i'm going to do node space dots and that's going to get our bot running now if we go back into discord and we do our command and you're gonna see it's not gonna work to do the youtube command it's gonna send the message of you can't send this command because you don't have the right permissions and that's because my myself don't have the mod permission but if we add that role into you know myself and we try doing that one more time you'll see that will actually go ahead and send um the youtube link so that's pretty much how it works is very basic so that's pretty much the code here all you're doing is checking if it has this role if it does go ahead and execute this code if they don't have the uh permission or the role go ahead and send this message very basic so let's change some things right now we're doing is we're sending this message if they don't have the role but what if we want to implement some kind of method that if they don't have the role we're just going to simply add them to the role so the next time they try to do the command we're going to be able to send them this message up here and this is actually very basic all you do is message that member um that roles and then dot add and then we're just going to add this roll up id right here so just going to copy this and we're going to paste it like that so what's going to happen is basically it's going to send a message and we're actually just going to basically change this to i see you don't have the correct permissions let me change that or something like that right and a little smiley face like that right so what's gonna happen is if they have the role aka the permissions to be able to do this it's going to send this message if they don't the bot is going to send them a message saying oh looks like you don't have the correct permissions let me change that and then it's going to add them into the role so let's go ahead and run our code here go back into discord and we're actually going to remove ourselves from the mod roll and in here we're going to do youtube and it's going to say i see you don't have to correct permissions let me change that and look what happens we got the mod roll added to the member right here um so now next time we do youtube uh well if i can actually type youtube it's just gonna send the youtube link just like that so that is how you can add a role um in a command and you can do many things you know do whatever you want with this method you can add um to delete rows it's very easy all you do is you do remove and it's just going to remove um basically that role we can actually test that right now so if you clear that you can know space dots go back into our discord make sure we have the model we do and then we do youtube um well first of all we should probably do this up here so let's actually copy this right here so it's going to send it and then it's going to remove us from the role because we already have the role so it's going to do this basically this and not this so let's actually do that let's run that and go back into here and if we do youtube we're going to get the youtube link and we got removed from the world just like that so very simple if you want to be very safe i would actually recommend you to um so let's do this change that to add to catch because there could be some errors um so if you really want to be a good programmer you can say and catch some errors make sure you just do catch and then console that error and if some error kind of happens it'll catch it and it will log it into the console here which is something that you might want to do um because you know there might be some problems with the role id or something else so make sure to do that so what if you want to get the basically the role id without having to do the method that i showed earlier in the video which is having to mention the role on discords and getting the role id that way what if you just want to basically check if a member has a role with a specific role name so let's say we want to check if a member has the role of name mod then we can execute it uh that's what i'll basically be showing you here i don't necessarily recommend you to do this method um because it's basically prone to many errors let's say later on the role mod gets changed to moderator then that's going to have some problems here in the code because we're looking for the specific role named mod not moderator so usually you want to do this method and i recommend you to do this method which is the role id this is the id specifically for the world um but let's say some some reason you need to check by the world name this is how you do it so basically we're going to delete all of this all the way to has and instead of has we're going to do sub and then here we're going to do is do our we do an arrow function and then do r dot name and then we're going to do three equal signs one two three and we're going to type in the name for role which is basically mod as you have it right here mod so let's go ahead and do mod mod and that's basically the same thing as getting the roll id so let's actually remove this and let's go ahead and run our basically our bot right here and then go into discord and we do have the role mod so let's go ahead and do youtube and it's gonna send the code lion youtube link because we're able to check it by checking the mod name and you can pretty much do the same thing up here if you want to add a row so if you don't have it instead of doing dot add um sorry not that ad we can actually just create a basically variable so we can do roll equals message and we actually just copy this to be honest so let's actually just copy this like that um so we're basically writing a variable of exactly what we have in here um so that's role we can just actually add role here we can also just add this whole entire code in here but i think that's a little bit easier so we created a variable role that has all of this okay so if we clear and run it again go into discord and then do youtube we're gonna have the role so it's gonna send the youtube link and if we remove the role it would do it again let's say i see you don't have the correct permissions let me change that and it looks like it didn't work so let me go ahead and check this error message this is why i tell you to do catch because it's actually going to send um some some sort of error to your terminal so there was some very nubish um kind of code that it did first of all i just copied this which makes no sense because here i'm checking and i'm kind of receiving a true or false kind of method here so if it's true send that if it's false than that so we actually have to change some things if we want to create this through a variable up here first things first is change this into guild and change this into find right so we're checking the guild roles and we're finding the role mod uh by the name mod and then we're storing that in the role variable so make sure you copy this not that stupid code that i wrote which is kind of something that i kind of improvised um so just don't copy this i actually have this code and then if we basically pass in this role variable through the add function here um we're going to see that it's going to work so if we do notice based on our bot we do first of all let's check we have no role mod we don't youtube it's gonna say i see you don't have the quick commissions let me change that and it added the mod roll through that way so that works like that too if you want to add it in a variable so you can use the role in all of your code so for the final thing in this video we're going to be able to check if a member has a specific permission so not a role but a permission and usually they get these permissions through roles so let's go ahead and go back into discord and if we go up here into server settings and then rolls we're gonna see that we currently have the permission of cake members so the moderator role sorry moderator has the let's actually change that so let's go back here and turn this off because i did this wrong the moderator role is going to have the permission to kick and ban members and we want to check that whoever has the moderate role that they have access basically to that permission and you can do many things with this maybe you don't want a specific person to have a kick or a ban permission even though they're a moderator possibly so you just want to check if a member has permission through the roles so let's go ahead and save this let's make sure we're a moderator we are and all we're going to do in here is i kind of blank this out and we're gonna check if it has the permission of kick member if it does we're gonna send a message saying you have the permission to get a member so to do this all we're going to do is do if and we're going to message that member that permissions dot has and in here we're going to pass in a discord.js uh actually technically a discord flag there's specific flags that you have to pass in and these flags are all of these permissions up here so these are the permissions that discord gives you which is all of this right here and to pass that in there's actually a website uh it's basically discord.js.net docs basically all the information for discord.js is right here and they give you the flags the static flags for all the permissions so if you want to check for the kick we can see that is right here kick members so we're going to copy this and then we're going to paste it in here make sure it is a string and if they do have the permission we're going to do a message oh that is not message message dot channel dot send i'm sorry guys i've been typing terribly this video i kind of got a new uh keyboard and i have to get used to it so uh you have the permission to kick members right so let's go ahead and test our code here let's go ahead and run our bot and if you go back in here and we're still in the youtube command it's going to send you have the permission to kick members right because we have the role of well the role of mod that has the permission to kick a member right if we come in here and let's say we don't have the permission to let's make sure we're in mod we don't have the permission to banned members let's turn that off so we currently don't have the permission right and if you come in here uh let's go back into our code we come in here and actually let me go ahead and open that website up again right here right so for band members it's this flag right here so you can again check this out i'll have a link in the description to all these flags as well um right here we do band member so if they do we're gonna send the you have the permission to ban and if you don't let's do it else we're just gonna send oh that's not exactly what i wanted to do we're gonna send you don't have the permission to ban members right so if we clear and run our bot once again come back in here and we currently don't have the permission to ban members and if we do the command it's going to say you you have the british debate i saved that correctly uh roles mods i did save that correctly so maybe i did something wrong with my code band members if asked permission band members let me test that one more time maybe it's a fleek go ahead and check that once again if not i'll go ahead and check my code to see if something's wrong you have permission to bad members i'm guessing maybe because i'm the owner but i don't have a specific role for it right yeah that wouldn't make sense let me go ahead and check if there's something wrong with my code so i think it does have to do with basically the accounts right since i created the server i basically have permissions to do everything right so what i did is added my secondary accounts right here and i tried doing um you know basically the command it's going to say you don't have permissions to ban members right since i am basically the owner of the server i kind of have all the permissions uh so it does work it's just that i couldn't do it my other command and actually if we go ahead and go into our code we can actually basically turn this on and turn this off and if that account runs the command it's just going to add them to the role so let me go ahead and pull that up again by first having to exit and open our bot again come back in here and now if you do youtube and say oh i don't think it's saved correctly let me actually make sure i save the file before doing anything so let's go ahead and okay well i'm kind of typing randomly now um why is it pasting that okay that's weird so let's bring on our bot one more time and this will be the last thing we do this episode just to show off that it currently works uh the code that we did this video it's gonna say you don't seem to have the permissions let me change that and i got the permission of mod and this again this new account that i was able to add to this server so just like that that is kind of how you mess around with permissions i'm going to leave in the description a link to this website right and this website has a lot more details you can say about how to overwrite stuff um so adding overwrites here you can check um if everyone has the view channel it's going to give you a true or false sorry hit my mic there it's also going to give you some objects that you can use um replacing overwrites all these crazy advanced stuff i'll make sure to leave that in the description below so you guys can check it out and go ahead and mess around with more advanced stuff through permissions but for this episode that'll be it we learned how to check add um permissions roles all of that make sure to subscribe for more discord.js tutorials i'll be trying to upload more often on this channel so make sure to basically subscribe hit the like button comment below any suggestions for upcoming videos i think i'm going to be doing um kick and man next so you can kick a member uh band a member uh because apparently my code from a year ago isn't working properly uh as things change usually in the coding world so make sure to subscribe and i'll see you guys later on my next episode peace
Info
Channel: CodeLyon
Views: 149,046
Rating: undefined out of 5
Keywords: code your own discord bot, create and code your own discord bot, how to code your own discord bot 2020, make your own discord bot, make your own discord bot 2020, make your own discord bot basics (2020), make your own discord bot commands, make your own discord bot free, make your own discord bot online, program your own discord bot, discord bot tutorial, code discord bot, how to make a discord bot, personal discord bot, discord js permissions, discord.js bot permissions
Id: 5BArCspxauI
Channel Id: undefined
Length: 20min 12sec (1212 seconds)
Published: Tue Jul 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.