How to make a Discord Bot for Beginners (Discord.js v14)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone how's it going I hope you're all doing well in this video I'm going to teach you how to set up your very own Discord bot from scratch now before we started this video it is important that you know at least the basics of JavaScript now speaking of JavaScript we have to run it somewhere so we are going to need node.js so go to nodejs.org the long term support version is what I'd recommend you download if you're watching this video at a later date where the long-term support version doesn't work you can always download the latest version now the next thing that we need is a code editor so head over to code.visualstudio.com and download visual studio code you can either use this or your favorite code editor so to get started let's actually create our bot account so I shouldn't have closed my browser but to create our bot account go to Discord whoops discord.com developers slash applications once you're here you know usually you'd have no applications to create an application just click on new application up here now an application is not your Bot it's basically just like a container for your bots so what you want to do is you want to give it a name you want to agree to to the terms of service and create your application once you create your application go to the bot section of the left over here and where it says build a bot click on ADD bot and click on yes do it now as you guys can see your Bot account was created and it was given the name of your application and if you gave your application an icon then it would automatically inherit that as well so before we start we have to change a few settings actually uh first the public bot let's take it private because this is a development bot after all and we don't really want other people to be able to add this to their server the next thing is these privileged Gateway intents now if you don't know what these Gateway intents mean don't worry just remember the word and I will come back to this in a bit so just enable all three of these options and click on Save changes now your Bot is ready to be invited to your server and the way you do that is of course you'd be given a link so to generate that link go to oauth 2 and click on URL generator and over here there are a few Scopes you can choose from most of these we don't really care about but the ones that we care about is bought and application dot commands the application.command scope will allow us to use stuff like slash commands and a bunch of other interactions now down here you have your Bot permissions of course you can just give your Bot whatever permissions you want but I highly recommend choosing administrator to start with just so you're not confused when some stuff doesn't work after that you will be given a URL just copy that and you can either you can either paste it in a new tab or what I like to do is go to Discord and paste your link in chat click on it click the server to add to and click continue all and authorize it and you will notice your Bot is in the server and as you guys can see it's offline no worries of course we're going to take it online in this video and we're also going to be doing a little bit of message interaction basically we send a message and the bot replies but it will only reply to the message if it meets a specific requirements what we're going to do now is we are going to go ahead and open up our Visual Studio code the code editor that we just downloaded I actually already have a little bit of a project over here but we are going to ignore that come up here click on file open folder and what you want to do is you want to create a folder anywhere you like I am going to create it in my desktop and I'm going to name this Discord bought and open up your folder once you open up your folder of course you have no files initially but we're going to work on that so the first thing that we want to do is we want to initialize this folder as what we call an npm project so the way you can do that is click on Terminal and new terminal and over here what we want to do is we want to type out a command which is npm init which is initialize and we're going to add a flag of Dash y the dash Y flag will basically say yes to all the default questions that npm asks us so just click on enter and it generated a package.json file which is our project information now we're going to change one thing which is the main field and what we're going to do is we're going to add a source folder before it and do Source slash index.js because we're going to be storing our index.js file in a folder called source which let's create that right now in your file tree you will have this little plus folder icon click on that and type out source and what you want to do is you want to create a new file now the file is again going to be called index.js or you can actually call it whatever you guys like but it's best practice to just call your main entry point index.js come back to your terminal which you can by the way click on Terminal and click on new terminal or there's even a shortcut so basically come back to your terminal and type out the following command npm install or you can even use I which is short for install and you can type out the package name which is Discord dot JS so just hit enter and inside our index file we can start working on our bots so the first thing that we want to do is we want to import a few things from our discord.js library we are going to destructure so do const we're going to open up an object and do require Discord dot JS now this is called destructuring basically you're just importing a set amount of things from something which in our case is a package so the things that we're going to import is clients and intense bit field I'm going to explain what intenses remember when I said Remember the word intent this is basically what that is and I am going to explain to you what that is a client on the other hand is going to be our bot so we need to initialize a new client because as you guys can see this is a class so let's go ahead and create a new client you can give your client whatever variable name you want so I'm just going to say const client in all lowercase equals to new client with a Capital C and this class basically takes in an object and if I do control spacebar you will see all the things that this client takes in but it only it only requires one the rest are all optional so we're just going to add intents for now and this is going to be an array so just open that up like that now what we can do is we can add our intense bit field you can do that dot flags and the first intent that we're going to add is called guilds now if you don't understand what intent is let me explain that to you real quick intents are basically a set a set of permissions that your Bot can use in order to get access to a set of events so what does that mean now actually uh I feel I remember there was actually a really good set of dogs from Discord themselves so I'm gonna search up Discord intent and I believe it was this one and yes uh in the Discord documentation if you come to the topic of Gateway and you'll notice uh the list of intents basically over here you'll see that we have a bunch of intents we have the guild intent uh The Guild members intent The Guild bans intent and yeah there's a list of intents and each intent will have a set of events now these events will only be accessible to the bot if our bot has uh explicitly defined this intent so for the guild intent that we have defined over here our bot will have access to all of these events which means our bot will be able to know when a guild is created a guild is updated a guild is deleted a role inside a guild is updated created or deleted by the way a guild in Discord development is actually a server so remember that now once you have a basic idea of what intents are we can basically add a few more intents to give our bot even more permissions to work with so we're just going to duplicate that and in the next intent I believe is going to be Guild members so we need to have access to the members who are inside servers now the next one is going to be Guild messages now this is important for what we're going to do in today's video which is listen to messages so this is required for today's project however we will not be able to read the messages for that we need another intent which is called message content again you can find all of these in the official Discord documentation and I will actually have a link to this down below anyway this is all the intents that we need for now um and now what we can do is we can actually log into our bot as you guys remember the client is our bot instance so we can call our client and say we can chain a login method and inside the login we have to pass in something called token which is a string now what is a token basically the token is our bot's password and to get that you have to go back to your Discord developer portal go to bot and scroll back up and under token you will see there is a reset token button now in previous versions of this Discord developer portal when you created a bot you will be able to see your token whenever you want but right now you have to reset your token and you have to like reset it every time you wanna you want to see your token so as you guys can see we have to click on reset token click on yes do it and it will show a long string right here we can copy that and we can paste it inside our login method now as I said before this is your bot's password so you want to keep this as safe as possible after the after I'm done recording this video I am going to reset my token which again if you have compromised compromise your token you can always come here and click on reset token and it will completely stop any instances running on the spot so anyway now once we have the login setup it's pretty much ready to go we can take our bot online let's save the file and by the way I have an auto code formatter which is called prettier so let me show you real quick this is the code formatter that I'm using and feel free to install it now back to our index.js file we have to run this file now now one way we can do that is by coming to our terminal again and typing out node Source slash index dot JS now that is a good way to run totally fine however whenever we make any changes we are going to have to restart that thing over and over again manually and that is not what we want in in development we want the bot to restart automatically whenever we have made any changes so to do that we're going to use a library called nodemon and you can use npm to install it however we are not going to install it in this project right here we are going to install it globally so you can type npm install Dash G node mod the dash G flag means Global and it will install it in your computer if you're on Windows this command should work just fine however if you're on Mac or Linux you are going to have to add sudo in front and press enter now as I'm on Mac I'm going to have to put in my password and now it's going to start installing node mod I already had node mod installed so I don't really think it's going to make any changes so yeah I think there was an update or something but yeah it was already installed now what we can do is we can type in our terminal node Mon and this is enough because in our package.json file we have already declared where our index file is so node mod doesn't even need need to need the path to our index file however if you did not change it over here you can always manually Define where you want to start the node mon operation so we can actually just type in node mod and press enter and as you guys can see it says nodemon starting node Source index.js however nothing else is coming in the console and don't worry about that because we haven't actually said uh to log anything to the console however if you go back to Discord now you will notice our bot is actually online it doesn't really do anything it doesn't really have any commands however it is online that is a good first step and now what we can do is actually we can log to the console whenever we whenever our bot starts or whenever our bot logs in or is ready to be used so what we can do now is actually before the client.login we can add a new event listener so we can say client dot on and the on is a method which basically has access to a list of events now there are many events like Channel create channel delete uh errors you know message create this is something we'll use in today's video and the one that we are looking for right now is called ready so let's type out ready and basically what this does is it listens when our bot is ready now we can actually write a callback function actually this is meant to be c as that is more descriptive because C is for client however we already have client uh defined over here so we cannot redefine by calling this client so we're just going to call this C now once our bot is ready we of course went to console log that the bot is ready so let's do that real quick so let's type console.log and we can give it a generic message like the bot is ready and that would work just fine so if I save the file you will notice down in the terminal it will say the bot is ready and that works just fine however we want a more personalized message a message that will actually write our bot's name down here so how we can do that is by using this client instance that we get returned so let's use a template literal which we'll use I believe these are called tilde uh I mean backticks but the key is called tilde which is above your tab key and what you can do is you can type a dollar sign and open up your curly brackets and in here let's use this client instance so C dot user dot tag now the tag is basically your user or your bot's username plus its tag however if you just want the username to come you can do a DOT username or if you want an ID for some reason you can also do that dot ID and that will log your Bots ID down here however in our case we just want the tag so C dot user.tag is online and I'll actually use an emoji over here and I will give it this emoji and let's save it now let's check our console and it says not under control is online which is our bot's name if we check Discord yes indeed it is online however it doesn't really do anything if we send a message like hey it doesn't reply so let's configure that under our ready event listener we can have a new event listener that we talked about earlier so let's do client dot on and we can listen to a new event which is message create whoops message create and this event listener will basically be triggered whenever a new message is sent that the bot can see now if we actually remove these two intents our bot will not be able to listen for the message create event and this event will not be triggered so this is why I'm telling you that intents is super important and make sure you provide the valid ones because I have actually encountered a lot of problems not providing the valid intents and I've been debugging for like hours on ends however uh in our case now we can return a callback function up here we can actually return a message you can call this whatever you like some people like to call it MSG short for message however I like to call it message because that is more descriptive and what we can do is we can actually console log the message let's do that and let's see what it is like so I will open up my terminal and you will see the bot is online now and what I can do is I can send a message saying hello once I send it I can come back to my terminal and you will see the message has been logged now the type is message now I did say I wasn't going to show you the documentation but I feel like this is going to be helpful for today's video we can go to discord.js.org and we can come to the documentation and I want you to search the the thing that has returned basically which in our case is message so type out message up here and go to the first one and as you see there are basically a bunch of things that you can access there are properties so a message can have stuff like the author the channel ID that it was sent in uh editable basically checks if the message can be edited uh The Guild which is the server that the message was sent in the member which the person who sent uh the met the message and we have a whole set of things and this is super helpful this is why you should read the documentation it's actually super helpful same with methods and these are like all the methods that you can chain onto your message so we can have stuff like message.edit dot delete this is considering the message belongs to the bot but yeah there are a whole lot of things that you can do with a message so anyway what we are interested in is in the content of the message which in our case you guys can see right here the message content says hello so let's access that so we can say console.log message dot content and let's save that and let's run our bot again I will make my terminal a little smaller and we can go back to Discord and I can say ping this time and as you can see it logs ping that is true so let's start and uh let's actually go ahead and deal with this message let's go ahead and start interacting with the user because that is ultimately what we want to do what we can do now is actually we can check what the message content is so we can say stuff like if message the dot content equals uh hello now we can actually do something with this message in our case we can reply to that message by saying message dot reply and we can actually type out whatever we want to reply to the user with this is pretty straightforward and I hope you can understand this and let's just say something like hey let's save it and uh our bot is now online let's say hello and the bot replies with hey so it is working if I send any other message it will not reply uh however I do want to show you one thing let's say we send hello and the bot replies with whoops and the bot replies with hello let's save that let's wait for the bot to restart okay now let's say we say hello now you'll see it says hello and it starts replying to itself because the bot said hello itself the bot doesn't really know how to distinguish between a human and a bot so how we can uh you know distinguish how we can tell the Bots to distinguish between them is by checking the user who's sending the message and checking basically if that user is a bot one way we can do that is before we run any validations before we run any like message reply what we can do is we can say if message dot author the one the property that I showed you earlier is has the bought property so the bot property can either be true or false what we can do is we can say return because we don't want to do anything else if the author is a bot so now if I restart the bot by saying node mon by the way I closed down my bot because it would have constantly kept replying to itself so anyway now that it's online we can say hello and it will reply to us and it will stop right there it won't reply to itself like it did up here because we gave it this condition right here and it just returned out of this function so anyway what we what can we do next um I actually want to secure the token that we have over here this is not really a very safe way of having your token so what we can do is we can open up our terminal by clicking terminal new terminal or I just like to use the shortcut and let's close our bot by doing Ctrl C and what we want to do is we want to install a new package so let's type in npm install and the package is called dot EnV and this will allow us to set environment variables you're going to see what that is in a bit so let's install it now the reason why I'm saying this is not safe is because you may want to share your main file it could be to anybody else it could be on GitHub you know you don't want your token just lying around here so what you can do now is with our package installed you can see in our package.json we have dot EnV so what what we can do with this is we can come to the main directory outside the source folder where our project is initialized we can create a new file called dot EnV and inside over here we can store private information like our Bots token so let's do token equals and go to our index.js and cut this token from here and I will actually clean this up and inside our DOT EnV we can paste our token and what you can do now is you can share your file successfully without any like worries of your token getting leaked and if you are wondering well want the EnV file get pushed to places like GitHub well you can add a new file called git ignore and over here you can add the file dot EnV and this will basically ignore the dot EnV and it won't be pushed to GitHub anyway now you can close out your EnV file and what we can do is we can first require our DOT EnV we don't really have to store it in any sort of variable because we just have to call a method called config and that is it that literally gives us access to this dot EnV file from anywhere in our code base so any file that you create you can pretty much access it uh just because of this that's considering you um that considering you relate those files together it's kind of hard to explain right now but yeah just close the dot EnV and now what you can do is you can come down here to your login and type out process dot EnV sorry dot EnV dot your variable name which in our case is token in all uppercase so dot token and let's save that now we can start our Bots by typing node mon and as you can see it still works just fine if we go back to Discord we type hello that works so yeah this is just a much safer way of storing your token it's uh it's really important actually because the more you grow your project uh the more there's a risk of your token getting leaked because you may have not secured it properly so I think this is it for today's video in the next video I'm going to teach you how to register slash commands like like Dyno over here it has a bunch of Slash commands that you can use and yeah in the next video we're going to be registering slash commands and hopefully even responding to those slash commands but yeah I will see you in the next video If this video helped you out please make sure to like it and uh if you guys enjoy my content make sure to subscribe thank you and I'll see you in the next one
Info
Channel: Under Ctrl
Views: 224,547
Rating: undefined out of 5
Keywords: discord bot, discord bots, discord.js, discord.js v14, how to make a discord bot, make a discord bot 2022
Id: KZ3tIGHU314
Channel Id: undefined
Length: 27min 28sec (1648 seconds)
Published: Mon Dec 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.