Discord.JS v13 - Kick and Ban Commands [Ep. 11]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is alex from warnoffkeys.com and in this video i'm going to show you how you can create your own kick and band commands using discord.js version 13. in this video i'll be using the warnoff keys commands command handler if you have a different command handler you would like to use that's fine and if you're new to one of keys commands an introduction video as well as the full documentation can be found in the description down below now real quick i want to mention that knowing javascript is required to follow this video and this entire series if you don't know javascript don't worry you can scroll down to the description of this video and at the top you'll see javascript course there is an hour long crash course here on youtube which you can right click and open this in a new tab so you can watch that after you're done with this video so some important notes is that i've already specified where my commands folder is and i have an empty folder here which we'll be adding some files to in a moment i've also specified what test service i have here and because i'm looking to use slash commands i have to specify a mongodb connection here if you don't have a server you won't be able to use slash commands using one of keys commands now if you've used one off keys commands before maybe one following this series make sure you have the latest version installed by doing npm install one off keys commands at latest and this should install the latest version there are frequently improvements and changes added to one of keys commands so you want to make sure you stay up to date so an important note here is that as of the latest version you need to have a connection in order to use slash commands without this you will only be able to use regular legacy commands and so far throughout this project i've simply just specified my commands folder as well as what test servers i'm going to be using so inside my commands folder i can go ahead and make a new file called kick.ts and if you're using javascript you'd want to use js instead within here we need to export an empty object within javascript that would be module module.exports equals an empty object like this within typescript that would be export default object and also within typescript we can specify what type of object this is going to be by saying as i command here and i command is an interface that warnoff keys commands has this allows us to use autocomplete here so we can see all possible options for this object now two things that we need for each command will be a category and because we're kicking someone i'm going to call this a moderation category we also need a description which i will just simply say kicks a user and you can add in whatever text you want for these two properties but they are required when using one off keys commands next we should specify what type of permissions we need for this command and one of key's commands provides two options for that the first of which will be specifying built-in discord permissions such as administrator and this will make it so only people who are admins on the servers who are using your bot are able to actually kick people now in a lot of cases server owners want to have moderator roles have the ability to use some of these commands so instead of this we could specify require roles is true built into ward off keys commands is a required role command which will allow server owners to specify what commands need what roles to use this required role's property being set to true will force this command to have a role attached to it so not everyone can use it it'll also provide information to the server owner of how to do that we'll take a look at this in action later on once we're done writing the code next i'm looking to make this both a legacy command with a prefix such as kick as well as a slash command such as forward slash kick so to do this we can specify slash as the string both and also specify test only as true if you're not using slash commands or you do not have a db server connected to one of these commands you can ignore these two lines here now because we're kicking a user from a guild we want to specify guild only is true behind the scenes this will make sure that this cannot be ran within direct messages now we need to specify what type of arguments we're going to have on this command so i can say minimum args is two i can say expected args is a string and here we're gonna have the user first and then the reason and if you're using slash commands you can now specify what type of arguments these are going to be by passing in expected args types right here this will be an array of strings that will match each individual type to the corresponding index of the expected argument so the first element here will be the type for the user the second element with the type for the string right here and if you do not provide this it will assume every single one of these will be a string so in this case we want this user to be taggable within our slash command so we can pass in our user here and then we can pass in our string here now again if both of these were strings where we didn't have to tag anyone we would not need this because one of key's commands will assume strings for all the possible arguments so i'm going to undo that and here we can now finally add in our callback function which will be ran every single time someone runs this command this is passed in an object which we can destructure some properties from such as a message the interaction as well as an arguments array now the message is only provided if this is ran as a legacy command for example forward slash kick an interaction is only provided if this is ran as a slash command and arguments will be a string array no matter if it's a legacy or slash command of each individual word passed in from the arguments here so we could start off by getting the tagged user by either fetching it from the message or the interaction and we can use a ternary operator to achieve this like i say const target equals message ternary operator and here we can write the logic for if message does exist so basically if this is ran as a legacy command i can say message dot mentions dot members dot first and this will give us the first mentioned member now we can use a colon here to specify otherwise and in this case this will be what happens if a slash command is used so i can say interaction dot options dot get member and then we can pass in the exact string that we specified right here which is user now if i hover over get member we see that this is going to return a guild member or an api interaction data resolved guild member or null and the only thing we want is guild member so we're going to specify this as guild member and this code right here should only be used if you're using typescript if you're using javascript you won't need to do this but i'm using typescript so i have to add this in here now we need to make sure that target actually exists so if not target we can then return i could say please tag someone to kick then after this we now know that target does exist and if i go over to the documentation of guild member i can scroll down to the kickable property and this has a boolean whether this member is kickable by the client user so we want to first make sure that the user is actually kickable before we attempt to kick them so i could say if not target dot kickable i can return cannot kick that user now afterwards we now know that we have access to a member as well as we can kick them so we want to create a string of the reason to do so this was actually specified right here but whether this is a legacy command or a slash command the arguments array will include all passed in arguments including the mentioned user right here now that's the first element of what this array would be so we can remove the first element by just doing args dot shift now afterwards all remaining elements of the string array will be each individual word so i could say const to reason equals args dot join with a space so this will take all remaining elements and combine them together and put a space in between them basically creating a reason string then i can say target dot kick as a method and pass in our reason and finally i'm going to return a template literal saying you kicked and then i'm going to tag it the mentioned user which is less than at symbol greater than and in between the at symbol and the greater than sign i can insert in target dot id now one thing i want to quickly mention before we test this is that when using slash commands you have the ability to set ephemeral to true and this way only people who ran the command are able to see the response now because this is a moderation command you might want that functionality and it's annoying to have to manually check to see if interaction exists and then respond so one of keys commands has a built-in way to do this very easily for example right here instead of returning a string i can return an object with custom is true now anything else inside this object is what will be passed into interaction.reply behind the scenes so for example we could set content as cannot kick that user and ephemeral is true i could do the same concept at the bottom here i'm going to cut out this code i'm going to say custom is true this will tell one of keys commands to pass in this object to the reply method we can say content is this and then ephemeral is true now in this top case here this is fine to do because the only way target is null or undefined is if we passed in a string instead of a mention on a legacy command that's because specifying user here as the type will force the user running the slash command to actually mention someone and so target should always exist when using a slash command so with that said we can still stick to simply just returning a string so i'm going to go ahead and save this and i'm going to open up a new terminal by going to terminal new terminal now you should be already navigated into your normal folder but for me i have to navigate into episode 11 and now i can run my bots with npm run dev now if you get an error or a warning about not having a connection and trying to create a slash command you can simply change slash to false or just remove this altogether and now it says our bot is running and now we can go into discord and now we can test this command with exclamation point kick it's then going to say you must specify what roles can use this command with the required rules command that's because we specified right here required roles is true and because we have not configured any roles to be required for this command such as a moderator role it's going to not work but instead going to tell the user who ran the command how to use it now only administrators can run this command so i'm going to go ahead and do that like i say required roles i could specify the command name in this case kik and then the role id so if i go over to my server settings and under roles i can right click on moderator and copy the id i can go back and paste this in here it will now say out of that role to the command kick and so i can then run it here and it says i do not have the moderator role so i can give myself that role and now if i try it again it's going to say incorrect usage please use kick user and reason so i'm first going to try kicking something that isn't a user so i could say kick test and normally we would mention a user here i can say test again it'll say please tag someone to kick so i could try kicking myself for test it then says cannot kick that user and now i'm going to try testing this account here so i can say kick at testing account for the reason of testing we now see that they left the server here and that you kicked testing account so the kick commands now works and the band command is very similar so i'm going to minimize my console and i'm essentially going to copy this entire thing here and i'm going to make a new command as a new file called ban.ts i can then paste this in here and we have a couple things to change such as the description instead of kicks we could say bands the user and the reason is fine scrolling down we can say please tag someone to ban instead of kik instead of target.kickable we have banneable which is the same concept but only for banning users now the content here we could say cannot ban that user and scrolling down instead of target.kik we can specify target.ban now we're getting an error here because we're not meant to pass in just a string instead we have an object and if i use control space we see a reason right here we can pass that in we also have days which is between 0 and 7 and this is how many days of messages to delete the user this is not how long the ban should be to do that they'll have to be separate functionality built in and by default this is just going to be a permanent ban so here we have contents instead of you kicked we can say you banned and everything should be working here so i'm going to save this and we see my bot is automatically restarting it now says create a guild command ban and if we go back into discord i can now try the band command here it's going to say we have to specify what roles this way not anyone can just ban someone immediately so i'm going to go and copy the role again i can copy id there i can now say required roles ban and then the id i can now run the band command again and it gives me proper usage so i could try banning myself or testing it'll say cannot ban that user and i also want to test if ephemeral true actually worked so i could say forward slash ban i could specify myself and the testing and then say cannot ban that user and this is only sent to me so i can then dismiss this message and that is because we passed in ephemeral true right here so if you want to pass in embeds or buttons or other things like that you can just add it to this message just as if you're going to do interaction.reply or message.reply so i've now reinvited the testing account we can go ahead and try banning them so let's try the slash command version which should work the same exact way so forward slash ban i can then tag testing account and the reason will be testing the band command if i run this it'll say uband testing account and only we can see this and we see that they are no longer there if i go to my server settings and i go to bands we now to test the account right here the reason is testing the band command thanks for watching the video if you want access to video source code as well as early access to future tutorials consider clicking on the join button down below the video to support the channel
Info
Channel: Worn Off Keys
Views: 5,630
Rating: undefined out of 5
Keywords: discord bot tutorial, discord.js tutorial, discord.js, how to make a discord bot, how to code a discord bot, code your own discord bot, how to program a discord bot, creating a discord bot, discord.js v13, discord bot tutorial 2021, discord bot tutorial for beginners, discord bot tutorial javascript, discord bot tutorial node js, worn off keys
Id: cmhWVyjt0uw
Channel Id: undefined
Length: 14min 31sec (871 seconds)
Published: Wed Sep 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.