How to make a Discord bot - Discord JS v13 [Ep. 1]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is alex from warrenoffkeys.com and welcome to episode one of my discord.js series using version 13. in this first video we're going to be simply just creating our bots and getting everything up and running and in future videos of the series we're going to be diving further into discord.js so you can learn how to make practically any bot you can think of so with that said let's get started you can go ahead and google discord developer portal or you can go to discord.com forward slash developers and after logging in you should be able to see a page similar to this one where you have your applications right here here's all the discord applications i've made most of these are just for testing and you can make your own by clicking on new application at the top right once you've clicked on it we have to give it a name i'm going to name mine worn off keys tutorial but of course you can name yours whatever you want we can then click on create and now we're inside of our application page and we want to go over to the bot section on the left we could then go to the right and click on the add bot button it'll then have a pop-up asking for sure we can go ahead and click on yes and now it says a wild bot has appeared and so our application now has a bot attached to it and inside this top section here we have some important things such as the profile picture and username so go ahead and modify those to whatever you want and the most important part is this token right here you never want to show anyone this token basically treat it as your bot's password we'll come back to this later on in the video so scrolling down a little further we see public bot right here and this will allow anyone to invite your bot to their server so if you don't want anyone else to use your bot you can go ahead and disable this i typically do this whenever i'm working on a new project or when i'm making a bot that i know should only be in my server so after you uncheck that if you want you can click on save changes now next up we're going to create our own private testing server that way we can invite this bot and we don't have to test it with other people seeing so let's head over to discord and do that but real quick if you'd ever need help with anything in my videos feel free to ask your questions in the warnoff keys discord server we have nearly ten thousand members in here and once you join you can read how to ask for help near the top of the server and it'll explain how you can gain access to all of the coding help categories where you can ask your questions and get help from the community so with that said let's go ahead and create our own test server by clicking on this plus right here we can click on create my own and then for me and my friends and the server name is going to be worn off keys testing but of course you can name it whatever you want i'm going to click on create so we're finally inside of our own test server let's go ahead and invite our bot i'm going to go back to the discord developer portal and if i go to oauth2 i can scroll down and we have to select what scopes we want for our bots for our use cases we only need bots and applications dot commands these are the only two we need the bot will obviously just make it an actual bot and then applications.commands is going to give us access to slash commands which is something we'll be covering in a future tutorial so if i scroll down further we see bot permissions and just for testing we can stick with the administrator permission which gives us access to practically everything we need when making an actual bot that other people will use you will only want to select the permissions that you need access to that way server owners aren't worried about what you can and can't do to their server but just for testing and throughout the series i'll be using the administrator permission so this url is your invite link and the different permissions that you access here will modify this url so the server owners who are inviting your bots will be able to see what permissions it requires so now i'm going to click on this and copy i'm going to paste it into a new tab here it's then going to ask me what server to add it to i'm going to scroll down to one off keys testing and click on continue this is where the server owners would see what permissions they need i can click on authorize i can prove i'm a human it then says authorized and if we go back into our server we now see that one off keys has joined and we see it right here on the right now it's offline for now because we haven't actually set up our own project and we haven't ran our own bot this is actually the next step so let's go back over to our web browser and if you go to nodejs.org you can actually download node.js which is what we need to run our bots for this series you will need at least version 16.6 we're also going to need a code editor where we can actually write our code and i prefer to use a visual studio code you can just simply go to code.visual code.visualstudio.com for slash download or google visual studio code download and you should find it both of these are free and have standard installers where most of the default options are fine and you can just click next a bunch of times and then you're good to go so after you have both of these installed you want to go ahead and open up vs code i've done so here and if you're new to vs code you might have this window right here with this untitled one we can go ahead and close out of this you now want to navigate into your windows explorer and make a new folder and place this convenient for you i've done so here and once you can see your folder here you can simply click on it and drag it into this left panel right here vs code will act like it refreshes and now this is synchronized with that folder so we would see all the files right here for any files within that folder but of course we just made this so we don't see anything yet before we start actually writing our code i want to mention that knowing javascript fundamentals is required to follow this series effectively if you don't know javascript you're probably going to run into a lot of problems and i do have a complete javascript course the first hour is on youtube for free so you can check that out in the description down below so with that said let's move forward and let's set up our project in vs code we can go to terminal and click on new terminal and here we have this console or this terminal and if you've never written anything inside of a console like this don't worry what we need to do is very simple to start off i'm going to use ctrl l or command l on a mac to go ahead and clear our console the next step is to actually create our node project so we can do this with npm init dash y and this will create a package.json file and now going back into the console i can use ctrl l to clear this and we now need to install the required dependencies in order for our bot to work so i can say npm install discord dot js space dot env so this will install the discord.js and the env packages into our project the next step is to install typescript which is basically a superset or an extension of javascript and if you're new to javascript or if you don't know what typescript is don't worry the amount of typescript we're going to be using in this series is very very minimal and is some of the most simple stuff within typescript but it does make our lives a little bit easier as developers so if you're not interested in using typescript that's fine you can use the youtube player to skip to the next section within the console here i can use npm install g which will install this as a global dependency for any project on our computer to use i can then say type script and then next is ts node now when installing this on mac you might be prompted for a password and when installing is on windows this might take a while and then eventually might give you an error that looks like this so if you get this error it's a pretty simple fix we first have to simply just close the vs code then we have to open it as an administrator so if you create a desktop icon orifice pin to your taskbar go ahead and right click on that right click on vs code itself and then click on run as administrator the reason for this is you need administrator permissions to install things globally so now back in the console i can use ctrl l to clear it to give us a little bit more space and i can use npm install dash g type and ts-node so everything's much faster this time and there's no errors next we can initialize our typescript project by doing tsc dash init this will create a typescript config file right here now we're looking to create two files within our project we can do so with this top left icon right here the first one is going to be index.ts and this is going to be where our main code lives the next is going to be dot env and this is going to be where we store our token so the env file should never be shared and should never be pushed to version control which is a concept that i'll explain in future videos if you don't know what that is so to actually put our token in here i can say token equals and then let's say that my token was abc123 it would look like this i'm now going to go back into my web browser and go to bot on the left scroll up and click on copy and keep in mind that if your token ever does get leaked you want to immediately regenerate it with this button you will have to update your bot to use the new token but that way other people will not have access to your token so going back i can now paste this in here and then i'm going to close that file now once i have our main file we're ready to actually start working on our project i'm first going to zoom in real quick just so you can see a little bit better and to start things off we have to import the third-party dependencies that we installed such as discord.js and env so i can say import discord js from discord.js and we have autocomplete right here so we can just simply press enter the next thing we have to import is dot env so import dot env from dot env now that we have access to these we can use them within our projects to make sure we have access to our actual env file and its contents we can use this import right here by doing dot env dot config we can add in parenthesis because this is a function call this will then give us access to the variables inside of the dot enb file as environment variables which we will use later on so let's start off by actually creating our own client and this basically will represent your bot so i can say const client equals new discord js dot client this is a function call and it takes in an object in here and new to discord version 13 we are now forced to provide what we intend to do with our bots these are known as intents we can pass in an array of these so we will add these in soon but i want to quickly explain what intents are so intense are a way to tell discord what your bot intends to use and what information it needs so if your bot doesn't care about reactions at all it won't send your information about reactions but if you do care about reactions then you can specifically say hey discord i want information about reactions and it'll send you that now some other tutorials out there mention that you should subscribe to all intents because it's easier and more convenient as a developer but this is a bad practice it'll in theory slow down your bots and eat up your bandwidth on your hosting servers and there's really no upside aside from saving around two seconds of typing out what you actually want so for example let's say that my bot intends to listen to messages i can first provide the guild's intent so i can say intense and then i can use control space to import this from discord.js i can then press enter and we now see that this has been added to our imports at the top this gives us access to autocomplete so i can say dot we then want to access the flags and inside we have access to all of the intents we want to start by saying that our bot intends to interact with guilds practically every guild intent requires this intent as well next we want dot flags dot guild underscore messages so now we're telling discord to send our bot information about guilds and about guild messages so now we've created our client and the next step is to actually listen for whenever the client starts up or whenever your bot goes online so i can say clients dot on i can then say ready and then we're going to have an anonymous function in here which will be invoked whenever the ready event is fired so this is basic event handling within node.js so in here you can do things such as connecting to databases and other things for now i'm just going to simply add in a console log saying that the bot is ready so console.log the bot is ready and afterwards we now want to log into the bot so i can say client dot login process dot inf dot token and the name here must match the exact variable name you gave your actual token earlier in the video in your env file so if you added in token equals something then you would add in token right here now everything should work so far but i want to listen for whenever someone sends a message that says ping and i want to reply with pong underneath our ready event we're going to listen for another event i can say client dot on message create and if you've worked with discord bots before you might have used the message event well this is now deprecated and you should now use message create we then have another callback here and we have an argument here which will be the actual message itself so now the next step is to see what the content of the message is basically what is the text of that message and then reply to that message if the text is ping so if message dot content which will give us access to the actual text of the message so the text is exactly equal to the string ping we then want to reply to the message so i can say message dot reply we can then add in curly braces here we can pass in the content and this is going to be a string of pong so now if i save this we can go into our console i can use ctrl l to clear it so we can see things a little easier we could then say ts dash node index dot ts which is the name of our file here if your name does something different just use that file name right here instead of index if i press enter it will now say the bot is ready and if i go back into discord we see the one off keys tutorial is now online if i say anything in the channel nothing happens immediately but if soon as i say ping in all lower case the bot will automatically reply with pawn so our bot is now up and running and everything seems to be working correctly thanks for watching the video if you want 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: 159,490
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: JMmUW4d3Noc
Channel Id: undefined
Length: 15min 25sec (925 seconds)
Published: Mon Aug 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.