Create a Discord Bot With Node.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on guys my name is anson and today i'm going to be teaching you guys how to create a very simple discord bot with no json javascript now before i even get started i want to thank brad traversi for this amazing opportunity to be on his channel to teach his viewers you guys on how to create a discord bot his videos have helped many programmers including myself and i can't thank him enough for all the work he's done for the programming community with all that being said i also do have my own youtube channel where i post on a regular basis i post videos ranging from server side development to front end development my most recent videos have been react as well as creating full stack applications so if that interests any of you guys the link will be in the description and yeah so that's pretty much it let's go ahead and get started on setting up our project so i'm assuming that all of you guys already have the prerequisite knowledge of javascript if you guys don't i would highly recommend you first learn javascript before attempting this tutorial because it's going to be very hard to keep up with it second i'm assuming that you guys also have nodejs installed if you don't you can go over to nodejs.org and you can install node.js discord bots are server sided applications so we need to make sure we're using a server sided environment to actually run the application okay but assuming you guys have javascript knowledge as well as nodejs installed what we're going to do first is we're actually going to go ahead and let me open up my windows terminal and i'm in my documents folder right now and i'm going to create a new directory you can actually create this with the file explorer if you don't want to use the windows terminal i'm going to type mikter and i'm going to call this discord.js tutorial bots okay so that's discord.js hyphen tutorial hyphen bot and you can see that it was just created successfully and i'm going to go ahead and just cd into discord.js tutorial bot so this is going to basically change the directory so we're currently inside documents folder and we've just created a new folder called discord.js tutorialbot but i want to basically change the current directory to that new folder i just created so i'm going to type cd which stands for change directory so enter you can see that now our current directory we're inside discord.js tutorial bot which is inside the documents folder now i'm going to open up my text editor now you can use whatever text editor you want i'm going to be using visual studio code so i'm just going to type code dot you can actually just open up this folder manually by going to file open folder and searching your file explorer in my case i would go to the documents folder and then search for the discord.js tutorialbot folder and then i would open it up okay so obviously you can see right over here we have a blank uh area over here we have no files so let's go ahead and fix that so i'm going to go ahead and just type npm init hyphen y this is going to create a patch.json file which is going to be very important because let's say if we want to deploy our bot we're going to make sure we have all of our dependencies listed inside this patch.json file and we also want to make sure we have our scripts as well the next thing we want to do is we want to install discord.js this is a third-party module it's not part of the node.js core library so we have to install it okay this library is we're going to be using to interact with the discord api to create our discord bot now i will mention right off the bat that this library is very powerful and it can also be a little bit complicated depending on your knowledge of programming it's object oriented based so if you don't have much of an object-oriented background it might be a little bit tricky so i would highly recommend you guys familiarize yourself with object oriented concepts but i'm going to try my best to explain as much as i can at a very simple level so you guys aren't going to be so so confused with all these you know jargon all these terminologies i'm going to be explaining it very very thoroughly so don't worry okay but what we're going to do is we're going to just install discord.js we're going to type npm in discord.js okay and that's going to install discord.js should take a couple seconds there we go okay and you can see right over here that we now have this object over here this dependencies object which has discord js and the version okay we're going to also install one more dependency called the dot end this is going to allow us to use environment variables we're going to be using environment variables to store our bot token so the first thing we're going to do is we're going to create a source folder so typically what i like to do is i like to create new folder and i like to call it source src and inside the source folder i'm going to create my bot.js file this is going to be the main entry point to our bots okay this is where we're going to keep all of our code and what i want to do next is i want to set up a dot env file so we're going to create a new file and notice how i'm creating this file outside of the source folder okay so dot env is outside of the source folder and i'm going to create an environment variable by just typing the name of the environment variable i want to use so i'm just going to call this discord.js bot token you can name it whatever you want but you want to basically give it a name and then the equal sign and you want to assign a value now to actually get our bot token we need to do a couple things we first need to actually go over to the developer portal so you can go over to discord.com slash developers slash applications and you're going to be brought to this dashboard over here and you're going to click on new application and i'm just going to name this uh djs tutorial bot or i'm actually just call this discord.js tutorialbot and i'll create it and we're going to be brought to the screen over here so we have the client id with the client secret we're not going to worry about the client secret at all we're going to click on bots and i'm going to click on add bot and click on yes do it and there we go we have created a bot for our application there are many things you can do with your discord application and one of the things that you can do is create a bot with it so now that we've created our bot we're going to go ahead and click on copy right over here where it says token click to reveal token we're going to click on copy okay and i'm going to copy and paste that right over here now it's okay that i show you guys my token because this is just for tutorial purposes it's going to be reset after this video so don't worry but you want to make sure you keep your bot token kept private you never want to share your bot token with anyone because if someone has your bot token they can do a lot of awful things such as deleting every single channel on your server depending on what permissions has obviously you can kick every single user from the server be sure that you don't share this token with anyone and be sure you take precaution whenever you are securing your bot token it's very important keep private now if someone does compromise your bot you can actually just click on regenerate and this will update the token it will basically reset everything so the previous token will not work okay so if i click token if i click regenerate you can see that a new one was generated i can just click on copy and this one that'll be pasted will not work so let's close this out and let's go ahead and actually import dot n so we're gonna do require dot n and i'm gonna call the config function so this is going to basically load up all of the environment variables that are inside our env file now one thing that i will mention is that if you actually have environment variables set on your operating system these environment variables will not override them so let's say if on my operating system i have an environment variable called discord js underscore bot underscore token and i have it set to some kind of value this bot token or i'm sorry this environment variable will not be interpreted with this value it's going to interpret the value set on the operating system so it's very important to understand that because sometimes some people do run into the issue very often so that's why i'm naming it something that i don't have already on my operating system okay so the next thing that i want to show you guys is how this basically works so now i'm going to basically reference the environment variable let me console.log it so to reference the environment variable we're going to reference the global process object and this object has a property called dot end or end env which is an object and we're going to reference the environment variable so discord.js bot token now i'm going to go ahead and run the bot now so we'll run the script so we're going to type node source bot.js oh whoops what's going on here node.sourcebot.js there we go and you can see that the script worked successfully and you can see that our token was logged to the console okay so that's just a pretty simple example of how this whole thing works now the next problem is that we actually don't have our bot on our server okay so right now i have my test server you can actually just create a server by doing add server and click on create a server and you just name it whatever you want it's very easy and when you have your server uh what we want to do is we want to add our bot to the server so what we're going to do is we're going to go over to oauth2 well first let's get the client id so we're going to click on general information and we're going to copy the client id and then what i'm going to do is i'm going to visit this url so i'll leave the url either in the video or they'll probably i don't know if it'll be in the description but i'll put it in the video basically you want to go to discord.com api slash oauth2 authorize and we're going to pass in a query string so question mark client underscore id that's the query parameter and this is going to have the value of our bots client id which is what we got from right over here okay you go to general information client id and you copy it okay just paste it over there we also need to make sure we have a scope because if i just enter this right now it's going to take us to here but it says no scopes were provided so we need to add another query parameter with ampersand and then the name of scope and then set this equal to bot so now because we're authorizing our application as a bot now we're going to go ahead and select the server that we want to add the bot to now you can only add bots to servers where you have the manage server permission so that doesn't necessarily mean that you have to be the owner because there are different permissions when it comes to the discord server but if you're the owner then you definitely have the manager permission so what i'm going to do is i'm just going to select the server that i own so arizona server i'm just going to click authorize and we're going to do that and that should be it so say authorized okay let's close this window let's close this as well now if i go over to discord you can see that it says discord.js tutorial bot uh has joined okay it says today at 6 27 am and our bot is over here now our buzz in the server but it's not online so let's actually get it online so to do that we need to actually use code so that's going to be the next part of this tutorial so actually logging the bot in so one thing that i will mention right off the bat is that if you guys need a reference to the discord.js library you can actually go to discord.js.org and you can click on documentation they also do have a guide as well that you can go through if you prefer a text based guide or text based tutorial it's actually very informative the devs put a lot of effort into this as well as documenting their entire library so i'd highly recommend you visit this website these are additional resources aside from this video that you can reference okay but we're going to go over documentation and we're going to be referencing the documentation a lot so what we're going to do first is we're going to import a class and directly from the discord.js library so i'm using the require function and i'm basically importing discord.js and in between these curly braces this is actually called object destructuring and it also works with importing uh certain exports from the module so the module itself basically just an object that has a bunch of different uh exports so these those exports you can directly import them from it so if i want to import client i can import client if i want import message i can do that as well but the only thing that we're going to import is client okay the client itself is a class that allows us to interact with the discord api and it actually uses we actually need to create an instance of that class so we're going to do const and i'm going to call this client with a lowercase c it might be confusing but if it is confusing you can name it bots i'm just going to name a client and we're going to use the new keyword to create a new instance of the client class so now we have this client object which is an instance of the client class okay and this is where the object oriented terminology is very important when you have a class you can create many different instances of it but we only need one okay and now that we've created an instance of this class we can start referencing all the properties as well as the methods so one of the methods that we're going to be using is the login method this is going to take in a parameter which is the bot token that we need to pass in and this is going to basically log or bought in it's going to create the connection to the discord gateway the discrete api so to actually log or bought in we're going to pass a token now this is the reason why i have the environment variable is because i don't want to just paste my token in like this let's copy that and let's paste it in there we go i don't want to just paste it like that and you never want to do this with any type of credential because credentials are very sensitive so you want to make sure you keep it private you want to keep it inside an environment variable but we're going to reference the environment variable like we did earlier so discord js bots token just like that okay you want to make sure it matches and now let's actually run our bot so you can see right now it is currently offline but i'm going to go ahead and just type node dot source bot.js and you can see our botches went online which is pretty cool okay now i'm actually kind of tired of just typing node.source.js so what i'm actually going to do is i'm going to set up two scripts i'm going to set up a start script and this start script is actually going to be very important if we want to deploy our bot so let's say heroku for example the start script is basically just going to be an alias for node.slash sourcebot.js now instead of typing that in all the time i can just type npm run start and you can see it's just an alias for node dot slash source bot.js okay it's going to run the application i'm also going to set up a dev script this is personally what i like to do and it basically uses nodemon and what this is going to do is every single time i make a change to any of my file in the directory it's going to restart the process now you need to make sure you have nodemon installed if you want to use this so if you don't have nodemon installed you can ignore this part but i would highly recommend you install node modding just type npm i hyphen g node mod and that should install it globally and you can just type nodemon and then source.js and you can see that every single time if i save it's going to say restarting due to changes okay it works like a wonder so this is going to basically prevent you from needing to restart your application manually over and over again instead it will do it automatically whenever you make a change to your application okay so for the rest of tutorial we're just going to be using nodemon but like i said if you don't have normal if you have issues with nodemon ignore it don't worry about it just use the node command or the start script that we created okay so end camera dev and there we go so one more thing that i want to mention is that if you actually have an incorrect token it will actually throw this error it will say invalid tokens provided so please make sure that you have your bot token uh set up correctly okay if that error happens it means that you pass in an incorrect token or the token uh you probably reset the token and it said that okay so always double check all right so our bot is currently logged in but what if i actually wanted to log a message to the console whenever the bot first logs in okay so what we're gonna do is we're going to listen to an event okay events are very important to understand so let me get let me give you guys a brief overview of how events work so everything that occurs on the discord server or on the discord client itself can be thought of as an event so let's say for example in javascript in the browser we have simple events such as the click event so if i click on let's say this site over here i can register a click event on this sidebar and if there is a register click event then we can execute some kind of function right we can pop pop up an alert for example well let's say for example if i type hello and if i type enter and if i send that message okay that message is going to be sent to the channel okay the discord api is going to emit an event back to the client and the client which is going to be our library over here it's going to handle that event for us okay and it's going to basically emit what's called a message event when the bot first logs in it's going to emit a ready event if let's say for example if i react to this message over here with this emoji that's going to trigger a message reaction ad event okay so i hope you guys get the idea of these events because basically anything that we do if i delete the message as well that's going to trigger an event and there are so many different events that we can use with the discord api which is pretty cool if you actually want to get a whole list of events you can actually go over to the discord.js documentation and if you click on client and if you scroll down over here on the side you can see these are all of the events okay the one that we're going to be using now is the ready event which is triggered whenever the client becomes ready so whenever the bot has successfully logged in so to actually register an event we're going to need to call the on method directly on the client object okay so one thing that i will also mention is the client class itself extends event emitter it's a subclass of event emitter and what i mean subclass is that basically it inherits all of the methods and properties of the super class the parent class so if you actually go over to documentation and if you go over to client it says extends base client and base client extends event emitter so if you click on that it will take you to the node.js documentation and if you're familiar with event emitters you'll know how they work whenever one thing happens we can manually emit an event and then any uh and then any file that uh listens to those events will be able to receive those events okay so it's a good way to uh do things asynchronously as well okay so what we're going to do is we're going to reference clients okay remember it's a subclass of event emitter so we can reference the on method and we're going to pass and ready that's the name of the event so it's always going to be client.on the name of the event so ready and then it's going to be a callback function as a second parameter the callback function is how we handle the events okay and for the ready events this callback function takes zero parameters so all we're going to do is i'm just going to say console.log the bot has logged in now if i save and if i go over to my terminal it's going to say the bot is logged in now if i want a more descriptive log i can also let's say if i wanted to log the bot's username and the bots discriminator so i can actually just use template string so that's backtick so instead of using quotes i can use backticks and we're going to use dollar sign curly braces so this is called string interpolation and i'm going to reference client and the client object itself has a property called user okay and the user object itself is a client user type and the client user type has a bunch of other properties that we can reference so you can see over here that we can reference let's say for example if i want to get the username i can get that as well so client.user.username and if i save that it can say it'll say in the consolediscord.js tutorialbot if let's say if i wanted both the username and the discriminator i can do client the user.tag and it'll log that to the console give it a second or two or three let's see there we go okay so i'm just gonna say uh has a log in and if you go over to the documentation i'm going to keep referencing this because it's very important okay it's very well documented so if we click on client and if i click on properties you can see that the client itself has a bunch of properties and one of the properties is user and the type is a client user if i click on that it will tell me all the properties of client user so i know that client.user this user object is a client user type and the client user type has all these properties such as verified username tag uh system presence all these types of properties and they also have methods too so i would highly suggest you guys to play around with them and see what you can do with it there's a lot of different information that you can get from every single object so i'd highly encourage you guys to play around with it okay but we'll leave it like this for now so you can see that if i restart the bot every single time it logs in successfully it's going to trigger this ready event but we're going to move on and next thing that we want to do is we want to basically set up some basic responses from the bot so to do that we need to listen to another event called the message event remember like i said everything that occurs within our discord client so if i delete a message that's going to trigger a message delete event if i send a message it's going to trigger a message event and to show you guys that we're going to reference the client object once again and we're going to call the odd method and this time and if you have this intellisense setup it will show you all of the different uh events so we're going to use the message event okay now this time the message event takes in a parameter for the callback function okay and that parameter is a message object it's a message type and i'm going to go to the documentation once again because i want to encourage you guys to use documentation if i go over to events so remember that's classes click on client click on events or scroll over down over here and if you go over here and if you click on message that's the name of the event and it says admitted whenever a message is created which basically means whenever a message is sent to a channel and it says over here parameter type description so the parameter over here these are basically the parameters for the callback function okay so if you see uh let's say for example for the message reaction add if you see more than one parameter that means that you would need two parameters for the callback function okay but let's focus on message for now so you can see that this is a message parameter the type is a message so i click on message you can see that i have a bunch of different properties and have a bunch of different methods okay and this is the beauty of object oriented programming because we're basically encapsulating a bunch of different methods and properties and we're basically putting them all together because they all relate in such a way and with all these properties we can reference the activity we can reference the application attachments so let's say for example if the message has any files that were sent with it we can get those files by referencing attachments if i want to get the author that sent the message i can reference message.author okay if i want to get the channel that the message was sent in i can do message.channel and etc so let's just do a basic thing so let's say if i want to get the actual message that the user sent we can actually reference message.content okay so if i go over here it says the content of the message so we're just going to go ahead and do console.log message.content i just want to show you guys how this works so our bot is logged in and let's go over here now let's send a message let's say hello and see what it says hello so we basically sent a message okay this event was emitted and when this event was emitted if we want to handle it we need the callback function the callback function takes one parameter which is a message type and the message object itself has a bunch of properties and we're referencing content okay now let's say if i want to get the user that sent the message so let's log something more descriptive so let's do something like the user or let's do uh let's do this message so again i'm using a template strings and string interpolation so that's two back ticks and then we're using the dollar sign and then a pair of curly braces and inside the curly brace you want to pass in the variable value that you want to interpolate or evaluate rather so message dots and i want to get the author that sent the message so like i said we can reference the property author on the message object and if you look over here type of the author property is a user type so if i open that up i can actually see all the properties of the user type as well as methods so let's just do this message.author and let's just get the tag so that's the username the pound symbol as well as the discriminator and i'll just do this that user sent well let's just do this let's create a more advanced this kind of like our own custom logger and then we'll just log the message so measure the content just like that so let's do hello and you can see oh wait our bot is not uh logged in yet just give it a couple of seconds okay so say hi all right so you can see i have my discriminator and the message that i had sent so let me just do one more let's just say hello world and there we have hello world okay so that's pretty straightforward hopefully that makes sense but let's do something more advanced okay let's for example let's just say if i wanted to send the message back to the user if they say hello so what we're going to do is we're going to use an if condition and we're basically going to check to see if message.content remember message.content is a string it's the actual string the actual value that the user had sent the actual message and we're gonna basically check to see if they sent the message hello so we're gonna compare it to the string hello a hard-coded string and if this is true okay if this is a condition is true we're going to go ahead and do message now if i want to send the message back to the user and tag them i can just do message.reply and i can just say hello there okay and let's save and let's see what happens when i type hello so hello so see how the bot responds it tags me it says hello there let's say if i want to send a message that doesn't tag the user so if i want to send the message and not type the user what you can do is you can reference the channel property well first of all let's talk about what the channel property is if you go over back to the documentation and if you click on message class okay and if we go over to channel channel is the channel that the message was sent in and this can either be a text channel which is any channel that is inside the actual discord server that is text based or a dm channel which is the dm between the the bot itself and the user so in case you didn't know you can actually dm the bot so if i click on text channel you're gonna see we have a bunch of different properties and we have a bunch of different methods okay we're not gonna go over every single property method but if i want to send a message back to the channel the method that i would need to call is dot send and you can see over here it says sends a message to this channel and what's nice about this documentation like i said earlier is it's well documented it has a bunch of different examples so you can reference it it shows you how to send uh just simple text messages you can send files you can send embed messages a whole bunch of things okay but we're just going to send a simple message so let's call dot send and let's just do hello now one thing's going to happen and i'm going to show you guys what's going to happen and i'm going to show you guys how to solve it and this is a common practice that everyone typically does in the discord bot development community so right now we are checking to see if message.content is equal to hello and if it is we'll send back hello so we're basically sending the same message back watch this hello and if you actually see it's going to say hello over and over again it's basically uh sending a message a multiple different times the reason why there's this delay is because there's a rate limit and i think that's handled by the library itself to prevent you from being rate limited so that's why it's only sending five messages at a time but you can see right over here that it's happening but why exactly is happening so let me actually just restart the bot so it stops spamming the channel so the reason why this is happening is because when we send this message hello it's going to basically uh evaluate this to true right because mesh.content equals equals equals hello that's true and then it's going to send this message back to the channel which is also hello so when this message was sent by the bot okay this message was sent by the bot it said hello and when the bot sent that message it also triggered the message event so since that also triggered the message event our callback function was also called as well okay hence why we also have the logs over here because by default this does not ignore bot messages okay i just want to let you guys know that so since it doesn't ignore bot messages it executed this logic and it said okay look message.content is also equal to hello this is true so it's going to send this message so basically infinitely we'll send that message over and over and over again of course it didn't happen before because the message that we said was hello world right we replied to the user or hello there rather and that is different than the actual message.content itself so to fix this what we want to do is at the very top level over here we're going to go ahead and use another if condition and we basically just want to check to see if the author of the message so message the author and if we go back to the documentation remember what author is author is a user type okay and if you actually look at the properties there's a property called bot now this is a boolean and it basically tells you whether or not the user is a bot so if message.author.bot is true then that means the author of the message is a bond now obviously we don't have to compare it to true we can just do if message the author of bot and then if this is true we can just return and that basically will stop anything from executing after so this will pretty much ignore bot messages of course if you want to handle bob messages you can handle it manually you just want to make sure that nothing else happens after so you just want to return so now let's go ahead and go back up here or go inside this uh if condition over here and let's go ahead and send hello again and let's see what happens so see how this time the bot sends the message perfectly fine but it doesn't send it over and over again because we are checking to see if the message was sent by bot and if it is we are returning so it's not going to execute anything after this anything after this line over here so hopefully that part makes sense so that's a very simple uh response from the bot all right so the next thing that i want to do is i want to show you guys how to get started with setting up some simple commands for our discord text channels so if you guys aren't familiar with what text commands are basically with most chat applications they have their own commands and they basically enhance the usage of the chat if you actually look on discord if you type forward slash there are actually some built-in commands that discord provides for you but these commands aren't enough because sometimes we might we might want certain commands to do other things such as fetching data from an api or getting data from the database et cetera so we're going to go ahead and create our own commands so one thing that i will need to mention is that commands basically start with a prefix so in our case we're going to use the dollar sign for our prefix and then after prefix is going to be our command name so for example uh dollar sign and then kick that would be the kick command and commands typically do not have spaces and you shouldn't have spaces with your commands it's going to make it a lot harder to really set it up anyways but after the space everything else after that is implied to be a command argument so for example if i want to set up a simple kick command i want to kick someone from the server i would do dollar sign kick and then i would pass in the user id so the user id could be one two three and that would kick a user if they were found in the guild or in the server okay so hopefully that part makes sense so let's go ahead and just get started with this so inside my bot.js file up top over here i'm just going to declare a variable as constant called prefix and i'm going to assign it the value of a dollar sign which is basically just gonna be a string okay this can be whatever you want i would recommend you not use the at symbol because that would try to add users uh you shouldn't also use the forward slash but i'm just gonna use dollar sign for now so what i'm also gonna do next is inside the message event because remember when we send the command it's also a message which is going to omit the message event what i'm going to do is i'm first going to check to see if the message itself if the message.content okay if it starts with prefix so this starts with method is a method on the string prototype because we know content is a string so we can call dot starts with to check to see if it starts with a substring which in this case we're checking to see if the message starts with prefix so if it does then that means they're trying to issue a command and that command could either be a valid or invalid command which will handle ourselves later so the next thing we're going to do is we want to get the actual command name so we know for a fact that everything after the prefix is expected to be the command name so for example if i do dollar sign uh let's say google search okay our command name is going to be google search so what we're going to do is we're going to get that command name after the prefix so to do that well first let's declare a variable called command name and i'm going to go ahead and reference message.content once again and i'm going to call the substring method because we want to extract a substring from an entire string so whenever you want to get a string like a a certain part of a text from an entire string you would want to use the substring method so the substring method can take in one or two parameters so you can see over here it says return to substring at the specified location now we want to return the substring at the position after the command prefix okay in our case with strings the command prefix itself is at position number zero so we want to return everything that's after the prefix which is going to start from position one all the way up to the end over here okay so now to do that what i'm going to do is i'm going to first pass in prefix dot length okay so now watch this so if i console log command name i want to show you guys what the actual output is so let's go over here let's just wait for our bot to log in should in just a couple minutes like okay there we go so let's do kick and you're going to see it gives us kick so once again if i do kick that's going to give us kick but if i actually do kick one two three that's actually going to give us the entire string after the prefix so if we want just the command name we can't just do this because we'll have not only the command name but the arguments after okay remember what i said earlier that you should never have spaces in your commands okay so what we're actually going to do is we're going to assume that every single uh argument is going to be split up by spaces going to let's just call dot trim so this will trim all the white space before and after okay well actually you know what let me do that right over here let me do that before substring so that will trim all the white space before and after okay and then what i'm going to do is let me actually format this a little bit nicer so it looks so we don't have a long line of code so after the dot substring i'm going to split it up so i'm going to split it by using a simple space over here now this is going to return an array so it doesn't make sense to call this a command name anymore so instead what i'm going to do is i'm going to do something called array destructuring okay and what i'm doing first is the first element in the array is going to be structured is going to be destructured into this command name variable and then every other element after that array is going to be stored inside this rx variable which is going to be an array so with this spreader operator what it's basically doing it's basically just pack unpacking all of those elements in the array into this rx variable so let me show you how that looks like so if i do command name i might do args and let's try this again so let's do kick one two three you're gonna see we have the command name and we have one two three if i do kick one space two space three it's going to give me an array of all the arguments okay so i know it's a little bit weird but watch what happens if i just did args okay if i just did args one two three it would basically give me only one but i want everything so i'm going to use the spreader operator okay that's what this is called the spreader operator so hopefully that part makes sense now one more thing that i should also address too is let's say for example if i did kick one two three space space space space and then four five six watch what happens it's actually going to give us a bunch of different spaces so to actually solve that we can actually use what's called a regular expression which allows us to basically match for a pattern in the string so instead of just having a hard-coded single white space we can do a regular expression so i'm gonna basically use a simple uh regular expression like this so this is basically gonna match all uh white spaces okay so let's copy this again and let's see what happens oh we gotta wait for a bot to log in first there we go and there we go you can see that we only have two arguments so we had although we had a lot of different spaces it only treats them as two arguments okay so hopefully this whole concept of commands makes sense okay the anatomy of a command that is so we have the prefix we have the command name and we have the arguments okay so now that we've parsed our uh commands correctly we can go ahead and actually check to see what the command the user executed so if i want to check to see if the user let's say if they did a simple command such as kick so we're going to do if command name is equal to kick okay we're going to just do something like message.channel we're just going to pretend like we kick the user for now and then we're actually going to kick the user so kick the user let's just make sure it works you always want to make sure you're testing your code just like kick one two three kick the user so we know that this works okay let's do another command so let's just say for example i can also do an elsif condition after and let's say if i want to do ban and let's just pretend like we banned the user and banned the user and let's do that again so if i do kick 123 band one two three and these are perfect so if i just kick itself it would still execute that so now let's actually implement our command so what i'm gonna do is i'm just gonna do the kick command for now so when it comes to kicking users there's a couple things we have to do first first we need to actually make sure that the user that we're trying to kick is actually in the guild because if they aren't in the guild then we can't kick them however when it comes to banning you can actually ban people that are not in the guild so what we're going to do is we're first going to get the member object first okay so i'm going to declare a variable called member and i want to get the member object from the guild in order to do that we need to actually get the guild object so remember that if we actually refer to the documentation okay right now we currently only have the message object itself so when it comes to using this library itself you basically need to get all of your information from either the client object or the object that you have that's inside of the scope of the function in our case we have the message object and we can use the message object to get all types of data that we want hopefully that makes sense so for example if i wanted to get the channel i can do message.channel if i want to get the guild the guild that the message is sent in i can do message.guild if i want to get the member object of the user that sent the message i can do message.member and etc okay but we're going to get the guild object so we're going to reference member or message.guild and you can see if i click on the documentation i have a bunch of properties and methods on guild we'll take a look at this in just a sec but first we'll do message dot guild and what i'm going to do after is i'm going to reference the members property now if you want to look at it you can see on the documentation the members property itself is a guild member manager okay and the guild number manager basically manages all of the members that belong to guild and there are other properties and methods so you can do things such as bang the user you can fetch the user you can prune the user or yeah you can prune the user you can resolve the user you can unban the user etc now notice how there's no kick method so we actually need to get the member from the cache okay so we're gonna go ahead and reference message.guild.members remember that's the guild number manager and the guild number manager has a property called cash okay now cash is actually a collection okay now if you actually want to know what a collection is you can actually click on the documentation over here and it'll tell you what it is but collections are basically just like maps okay so this was a an additional class that was built on top of maps it extends maps okay and it has additional utility methods it's actually very very easy to use it's nothing crazy okay the base map itself doesn't have a method such as filter or uh reduce or sort but this collection itself does okay so when you think about maps you can think about key value pairs and when it comes to discord everything has an id users have an id i have an id so for example if you want to get my id i can right click copy that's my id if i want to get the id of a message i can right click the message and i can click on copy id that's the message of the i that's the id of the message if i want to get the id of the channel i can right click it and i can paste it over there okay so it makes sense for the library devs to store everything inside collections because everything is mapped by an id and when you reference things in a map it's actually much faster than looping through an array for example okay we're not going to go super in depth and time complexity but i just wanted to mention that so if i want to get the actual member object from the cache which is a collection slash map okay if collections seem confusing to you just think of the cache as a map and you can see over here that this map itself okay it maps the id of the guild number to the actual guild number object itself and if i actually click on guild number it'll give me the documentation for it and it'll give me all the properties and all the methods okay so let's take this one step at a time first let's call dot get remember this is the same exact method that's part of the map and we're gonna pass in the actual user id okay so for this kick method or for this kick command i'm actually expecting uh args which is an array okay to be non-empty so i'm gonna do one check i'm going to go ahead and check to see if args dot length is equal to zero then i'm just going to return a message and i'm going to say message dot reply please uh provide an id so that way after i send this message i don't have to it won't execute anything else after okay but if they actually did provide an id then this would be false so i'm just going to go ahead and reference args subscript 0 just like that because that's going to be the id because we're only going to kick one user okay so now watch this if i go ahead and console log member let's go ahead and go into the console first let's do this let's do kick one two three you're gonna see that it says undefined if i do kick it says anson please provide id if i do kick space it's gonna do the same thing okay now if i want to kick a user let's go ahead and kick uh this bot over here let's try to i'm going to paste the id and notice how we don't get the error message back and if i look in the console we have all of the data of the guild number see it's a guild number object it has the guild property it has a bunch of other properties as well okay we're not going to reference every single one of them but we just need to make sure we have this guild number object okay and if you look at the documentation you have all the properties and all the methods and if you click on them it will tell you what each property represents and if you look at the methods it'll tell you what each method does okay so what we're going to do is we're going to check to see if member is truthy because if this member is not found in the cash then that means the member is likely not actually in the guild itself okay so we're going to check to see if members truthy so if number so if that's the case then we're going to go ahead and kick the user so i can actually just do member dot kick okay and i can provide a reason if i want to but i'm just i'm not gonna do that for now and if the member is not found then we'll just say message and we'll send this message to the channel and not reply to the user so we'll just say uh that member was not found okay so watch this let's first do kick one two three that number was not found okay now watch this if i try to kick this bot you're going to see that an error is going to happen and it's saying missing permissions because our bot does not have permissions so we need to go ahead and give our bot permissions so typically what you should do is you should create a separate role for your bot so for example i'm going to create you know i'm just going to create a special role discord bots okay and you want to basically uh let me give it a color just to uh so i can stand out a little bit okay you also want to make sure that the bot itself has permissions above other users so for example uh for example this discord bot is above everyone okay but it won't be able to actually do anything to a verified bot moderator admin so if you're kind of new to discord and if you don't really understand the whole role hierarchy basically for example if my bot only has this discord bot role and it's trying to kick someone with the verified rule it won't be able to do that because uh the role is higher okay so i'll show you an example but you want to make sure that for this role you want to give its kick members okay and let's go ahead and do this so first let's give this guy the discord bots roll so it will be able to kick brainy bot because it doesn't have roles but first let me give it the verified role real quick uh hold on let me do this let's just make it so they could do that okay there we go so let me go ahead and make it so that it can kick this ball over here so now if i do kick you're gonna see that nothing happens even though we do have the roll it does have the permission okay it has the actual tick members permission but it still isn't able to kick it because it is underneath the verified role because that's just pretty much a high level explanation of the whole role hierarchy so if you actually wanted to be able to kick the verified role or any user with the verified role then you would need to put it on top of it so i save and i'm going to get the id again if i kick notice how the bot is gone and there's no error this time these were from the previous ones but notice how the ball is gone because we just kicked it okay now if i also uh take away the verified rule from this guy over here so let's do kick and let's do copy id okay notice how it still works so any role that is underneath the discord bots role will be able to be kicked or banned okay if we give the ban permissions so hopefully that part makes sense okay and obviously if i try to kick you know them again it's going to say the member was not found so it's very important that you understand the whole permission as hierarchy as the whole roll hierarchy okay but that's pretty much the simple gist of the kick command okay it's that simple all right so let me go ahead and just get my user back on the server okay so let's just say for example if the bot doesn't have permissions so let's just give it the verified role again well let's just give it let's give it a different let's give it a the admin role okay so let's say if the bot did not have permissions we should technically send some kind of message to the console or the uh the channel letting the user know that they don't have permissions or the bot design permissions so notice how in the console earlier we had this uh unhandled promise rejection warning so one thing that i should mention and i think i mentioned this earlier is that whenever you call methods like dot kick okay they'll actually return a promise okay so we should technically handle the promise and we shall also handle our errors in case it happens because you never know what could happen so when we resolve the promise this will actually return the actual guild number that was kicked so if i go here it will say kicks this member from the guild and returns promise guild member so let me go ahead and do this so let's hand the promise by calling.then this is going to give us the member that was kicked so i'm just going to remember and what i'll do is i'll actually just send a message to the channel okay and i'm just going to say oh let's do this remember was kicked okay so the reason why i'm using string interpolation is because i want to actually mention the member that was kicked so to do that you can actually just uh pass in the member parameter directly inside in between the curly braces and it will actually uh at the user which is pretty cool now if they don't have permissions we're just going to send the message we're just going to say message.channel let's send i do not have permissions okay now keep in mind that uh the bot permissions are completely different than actual user permissions so you should always make sure you're checking to see who's executing certain certain commands so in our case this kit command is technically a moderation command so you want to make sure that you're checking to see if moderators are using this command okay so for example let's go ahead and do this let's try to kick yeti again so it's going to say i do not have permission so it technically does but it can't kick it or i don't have a permissions to kick that user well let's just do this i cannot kick that user okay and it could be due to either permissions or the person's higher okay and uh let's just get rid of the uh rule again and let's kick them again and now cs as the id was kicked and if i click on it it'll display the actual user card okay so that's pretty pretty straightforward now if we wanted to actually check to see if uh the person that was executing the command was either a moderator or admin or an owner we could do that we can actually do that right up top over here so let's say for example i can do something like if message dot member so this time i'm referencing message.member instead of message the author because there's a difference between the actual user type and the guild number type when we're talking about permissions roles nicknames we want to reference the member if you're trying to get the user's discriminator or the user's username you want to get that from the actual user type itself but i'm going to go ahead and just check to see if the member has permission so this is another method and i can actually just pass in certain permissions so for example i want to make sure that the user has uh kik members okay so if the user has kik members then we'll let them use it if they don't then we'll just return a message we'll just say you do not have permissions to use this command so if you don't have the permission uh we'll just do message.reply you do not have permissions to use that command and that should be a pretty straightforward so let's try this again and this time i'm gonna actually have to do this uh from my alt account so let me go ahead and just invite my user again so let me just go another account okay so i am in the channel and i'm just gonna type kick and notice how it says you do not have permission to use that command okay but if i give it let's say if i give it the uh discord bots role which does have the permission if you click again notice how it's saying a different message it's saying please provide an id which basically said that this part was uh false so it didn't return and then it goes over here if i try to do let's do one more thing let's do kick one two three it just says that was not found okay so our other account does have permission to use the command if i get rid of it again you do not have permission to use that command okay so hopefully that's pretty straightforward now let's do one more command let's do a band command so if i want to do a band command it's very similar to the kick command okay but actually the nice thing about the band command is that we don't actually need to check to see if the user's in the guild because we can ban users that are not in the guild okay so we're going to do the same thing we're going to check to see if the command name is equal to banned this time and i'm going to copy and paste this code over here but this time instead of check and see if the permission is kick members we're going to see if the person has band members permission okay so if they have this permission then we're going to allow them to use that command if they don't then we will just send that message back and i'll also copy uh this as well and you can see we have a lot of reusable code which you can start to see that it's actually you know becoming a little bit messy but of course like i said the whole point of the tutorial is to make something that's very simple i don't want to you know create some kind of crazy opinionated structure but if you guys are interested in that i do have a lot of different tutorials on my channel that cover so many different things with discord bots so feel free to check it out but for now we'll just stick with this okay so the next thing that we're gonna do is we're going to reference the guild object again so we're going to do message dot guild and we're actually going to reference members and we're going to call the band method and the band method takes in what's called a user resolvable so user resolvable can either be the actual user object it can be the member object it can be the id of the person you're trying to kick or ban or i'm sorry it can be the id of the person that you're trying to ban and claim ban okay so if you click on user resolvable it'll tell you what it is so it can either be the user object it could be the id which is known as a snowflake it can be the message object so that will just pretty much resolve to the author of the message okay or it could be the guild number object itself so in our case we're just going to pass in the id directly so we don't have to actually fetch the member from the cache we can just pass an args subscript zero and now just directly ban the user so for example uh let's go ahead and try this so one thing that i also want to do is i want to actually handle the errors of course so i'm just going to go ahead and just catch the error i'm not going to resolve the promise with a dot then i'll just log the error for now just to show you guys what the errors look like okay so let's go ahead and let's try to uh let's try to do band 123 and let's see what happens so you can see that it actually says unknown user okay so unknown user basically means that the user is not found so let me actually get the id of a different uh bot that i have it's not in the guild but if i do that it's gonna say uh missing permissions okay so unknown user so this this user is not found but this user was okay and this is basically one of my other bots so right now it says we're missing permissions our bot is missing permissions so we're gonna have to give our bot permissions now so let's go ahead and do that so let's go back to the role and let's just enable band members and let's try to do it again and if you look over here uh we don't have the error anymore it actually banned the user if i actually want to see all the users that are banned i can go to server settings and you can see that i have uh cinderease which is my other bot that's the username band so if you actually pass in an invalid user then it actually won't work so the user actually needs to exist but the user doesn't have to be in the same guild okay so let's actually handle our errors like i said we always want to handle errors to provide any uh messages back to the user in case something doesn't work but this time you know what i'm going to do instead of using uh then catch i want to switch it up a little bit i'm going to use async away so if you guys are not familiar with async await i highly recommend you learn and familiarize yourself with that because with discord.js a lot of these methods that you're going to call are going to return promises and there are going to be some times where you're going to actually need the data that is returned okay and you might want to use it for later on so what we're going to do is we're going to add the async keyword all the way in front of our callback function and then right over here the band method will return a promise okay it's actually going to return the member that was banned or it's going to return the uh the user or member that was banned okay and what i'm going to do is instead of using denim.catch i'm actually going to use try catch this time i know it seems pretty messy like our code seems pretty messy but the whole point is i want to show you guys the different uh techniques that you can go about it because with discord.js it's a more advanced library so i want to try i want to do what i can to show you guys different techniques when it comes to you know writing your code using the scorches library okay so in order to actually use async await successfully we need to first actually await this method call so i'm going to do const and since this returns uh the actual user or member option itself i'm just going to call this uh const user okay and we're going to await the promise so this is going to wait until this promise has been fulfilled and it's going to return the user object now if i console log the user it's going to give us the actual user data and if i console log the error it will tell us the error so let's try this again so let's go ahead and do band 123 okay and let's type that and let's it's going to say unknown user okay so that's pretty cool um and let's also do this uh let me do one more thing let's do uh ban and let me get the id of my other bots so this bot is on the server like i said and if i ban the user you're going to see that it returns the id now notice how it actually doesn't return the user object and if you actually look over here or let me actually go to the documentation to show you guys this so it makes more sense if you go over here it returns a promise and the promise resolves either the guild number the user or the snowflake the snowflake is the id the reason why it's returning to snowflake is because this user is actually nowhere to be found in the actual client's cache okay so because our bot is actually because our because the user that we're banning is actually not in any mutual guild that our bot is in it's just going to return the id okay but if the bot was actually in the same uh guild that it was in as our other bot then they would actually return the actual object itself okay so what we're going to do is we're going to go ahead and just simply we're just going to display message we'll just do message.channel the send user was banned successfully okay and if an error happens we should always send the error to channel uh we can say something like an error occurred either i do not have permissions or the user was not found okay so now let's go ahead and let's uh ban well let's unban the other user so let's unban my other my other bot account and when i say bot account what i mean is like the actual like an actual discord application like self botting is actually against terms of service so i just want to clarify i'm not self-botting the account that i'm using is an actual other discord bot account that i created through the portal okay it's just in a different guild and i don't have another account to use okay i can just ban this one but i'll do that later but the whole reason why i'm showcasing this is because i want to show you guys how to ban users that are not in the same guild so if i do ban the user was banned successfully perfect and let me ban this guy there we go use was banned successfully pretty straightforward so that is pretty much it when it comes to kicking and banning okay so the one thing i'm going to show you guys how to do next is to set up a simple roll reaction system okay so this is going to be a very basic one we're not going to be using a database for this so what we want to do is whenever we react to a certain emoji when we react to a message with a certain emoji it's going to give us a specific role i'm sure you guys have seen something like this before where if you go join the discrete server and then you click on let's say the roles channel there are a bunch of messages and then it tells you which emoji will give you which role so i have a couple of rules set up on my server ready but you just want to go over to roles and just click on this plus sign and just create whatever role you want so i can just call this role node.js for example you also want to make sure that your bot has the manage rules permission if it doesn't it won't be able to add rules and one thing that i need to mention is that your bot will only be able to add roles to any role that's below the actual rule the bot has so what what it means is this bot over here it has the discord box rule so that means it's only going to add users to any role underneath it so if it tries to add the user to the bot moderator admin role it's not going to work so let's go ahead and set up our message so we're gonna have a javascript and i guess we can use whatever emoji we want i'm going to use apple we'll do python so for python we'll do do we have a banana emoji for c sharp we will do grapes and then what was the last one java we shall do peach so basically we're going to send this message whoops made a mistake over here and ideally what we want to do is we want to just add one of these reactions and it's going to give us the role so if i add the apple emoji it's going to give us the javascript role if i add the banana emoji it's going to give us the python role if i add the grape emoji it's going to give a c sharp and if i add the peach mode it's going to give us the java rule so let's go ahead and implement this so there's actually a couple things we need to do one we need to listen to a new event okay so we're no longer inside the message event anymore we're actually going to listen to a new event and it's called message reaction ad whoops there we go okay so this is another event and the callback function takes in two parameters it's going to take in message reaction object so we're gonna name it reaction and it's gonna take in a user object okay so the reaction object itself has properties such as the message it was reacted to the emoji that it was reacted with and the user object represents the user that reacted to the message so if we want to get the id of the user that reacted to the message then we can do that as well okay now the very next thing that we want to do is we want to check to see which message is receiving these reactions so what i'm going to do is want to check to see if reaction.message.id so remember reaction has a property called message and message has a property called id everything has an id or not everything but most things have an id so we're going to first check to see if the id of the message matches this because that's the idea of the message i right clicked it and i clicked on copy id and paste it in just like that now the next thing we're going to do is we're going to check to see if any of these emojis were added we're using unicode emojis okay so with unicode emojis they actually don't have ids we're going to check the name instead so we're going to get the name by simply we're going to destructure this from the reaction dot emoji property so remember this is the message reaction object and it has the emoji property and the emoji property has the name okay so now i can actually check to see which mode they react with so we're going to use a switch case and then the first case i'm going to have is i want to check to see if they react with the apple emoji now how do we actually get the name of it is it actually named apple well not quite i can't do something like case apple or i can't do something like if name is equal to apple instead since these are unicode emojis we need to actually get the unicode itself so to do that on the discord client you can actually do backward slash colon and the name of the emoji so apple and you can actually copy it and you can actually just paste it directly in there we're going to do the same thing with banana gray oh grapes and then peach okay so we can just directly copy that so just break right over here we're just gonna set up our switch case real quick and then we will copy that as well so now if they react with the apple emoji we're going to go ahead and give them the javascript role so now we actually need to give the role to the user so we're going to go ahead and copy the id of the javascript rule and then what we're going to do is whenever they react with the apple emoji we are going to just give the role to the member okay so when i say member we need to actually get the member object itself so what i'm going to do is right outside over here all in the top i'm actually going to go ahead and get the member from the guild so to do that from here now we need to reference the guild property or the guild object somehow well we can do that by simply doing this reaction dot message okay remember the reaction has a property called message and like we did before the message has a property called guild and guild has a property called members okay this is a guild number manager and this is a collection of all of the guild numbers or i'm sorry not a collection this is the manager to get the actual collection we would reference cash which is the collection so now we can reference this cache remember the cache is just a collection also known as a map okay the collection is a map so we can call the dot get method and then we can pass in the id of the user to get the actual uh member let me name this member i didn't mean to name it guild sorry so we're going to pass a user.id okay remember there's a huge difference between users and members to add a role to a particular person on the server you need to get the member object okay to update permissions into the member object etc so now if that's true we're going to reference member dot roles and then we can just pass in the id of the role and then we're going to do the same thing for uh banana so banana was python so let's get the python roll number dot rules to add python and just make sure there we go c sharp was i think uh grape let's remember it's member dot rules grape and let's do one more uh i think python was peach right or java was peach sorry java okay so remember the rules and add all right so let's just see how this works okay first let's actually so i'm actually going to show you one thing real quick okay so watch this so our bot is going to restart and i want to show you guys something so let me open up the logs over here the console i'm going to go ahead and react with the apple emoji and you're actually going to notice that nothing is happening in the logs i actually did not receive the roll nothing happened and you're probably wondering well is our code broken well not necessarily okay so let me show you one more thing let me actually send a message and let me react with the apple emoji again and you're going to see right over here that it does say hello this time so the question is why did it not work for this message but why did it work for this message okay so in discord.js the library itself actually caches your messages what that means is whenever a message is sent to any channel it's stored in a collection okay now the message reaction at event is only emitted for cached messages only so if the message is in memory if it's saved in the collection it's going to emit this event if it's not it will not emit this event by default so in order to work around this we need to actually opt into something called partials so partials are a discourages concept that allow you to handle uncached data so data that is not stored in the cache but it will still allow you to use the event itself okay so luckily we actually just need to write one line of code so inside the constructor so new client all the way on line four we're going to pass in an object inside the parentheses and we're going to specify partials and we're going to enable the user or not user we're going to enable the message partial and then the reaction partial because we're using message reaction add so we need both of these so now this should work without any doubt so if i go back to the console and i'm going to react with a different emoji this time so just give a sec okay so let's give it the banana emoji and you're gonna see this is hello and did it give us the role it did it gave us the python rule let me react with the grapes emoji it gave us the c-sharp role and let's react with the peach emoji it's going to give us the java rule so notice how now it's working before it wasn't the reason why is because we opted into partials okay when our bot restarted this message was nowhere to be found cache now if you actually fetch the message from the discord api it will be cached but once your bot goes offline and it comes back online this message is nowhere to be found in the collection of messages that are cached so in order to get it to work if it's not cached you need to make sure you opt into partials so let's react with orange and notice how nothing's gonna happen okay it'll still trigger the event but nothing's gonna happen okay and likewise even if i send a new message and i'll react with uh let's say green apple it's still gonna work okay so hopefully that makes sense and hopefully that gives you an understanding on why you need to use the partials now if you want to remove the role you can just do the same thing so we're going to copy that code and we're going to paste it and this time we're going to use the message reaction remove event okay and instead of doing roles.add we're going to do roles.remove now let's save and like i said because we are opted into the partials this will work so let's try it again so i have all the roles well not all of them i need to uh give myself the java javascript rule so let's give us that role so i have all the rules so now let's go ahead and unreact you can see i got it got rid of the javascript rule it got rid of the java rule okay get rid of the c sharp roll and then it got rid of the python rule if i react again it's going to give me the javascript role so that's pretty much how it works so hopefully this was a very simple system that shows you guys how to set up reaction roles and just some advice make sure you do not uh allow users to add themselves to the admin role okay or any moderated role that might be underneath the discord bots role okay so i'm going to show you guys how to use web hooks now so if you guys are not familiar discord actually has web hook integration so you can actually use for example web hooks with github so whenever someone triggers some kind of event on your calendar repository such as a star a fork a pull request it will send that message to a specific channel okay so we're gonna go ahead and first set up a web hook so you actually need to go into a channel that you want the web hook to send messages to so if i go over to general if i click on edit channel and if i click on integrations you're going to see that i actually do have one web hook okay so i have a web hook right over here let me uh go back there and i can create a bunch of web hooks that do different things so for example i'll create this web hook and i'll just call this uh announcements okay and you can select the channel that you want the webhook to be in so we'll just leave it as general and now you actually want to get the information of the webhook so we're going to copy this web hook url so if we actually paste this url into our browser you can see we have basically json object returned so let's go ahead and first let's get the id so the id is right over here this uh the webhook id is after this web hooks route okay so we're going to copy that part and then the token is everything after that okay so i'm going to save these things inside an environment variable so let me go ahead and open up this and we're going to call this webhook id webhook token so first let's get the id so there we go now let's get the token there we go just like just like that okay so now the next thing we're going to do is we want to instantiate the webhook client class so discord.js comes with the webhook client class that makes it very easy to use webhooks so we're going to go ahead and import that class from the discord.js module and now we're going to go ahead and declare a new variable we're going to call the webhook client and we're going to use a new keyword to instantiate webhook client and it takes in three parameters the last one is option the last one is the options but we're going to take in the id so that's the webhook id so we're going to reference that through the environment variables that's webhook id and then webhook token okay there we go perfect so now we can actually write a simple command to send webhook messages so now we're going to go down all the way to inside our message event after our band command we're going to write an elsif statement that's going to check to see if the command name is equal to announcements okay so this is basically going to be a symbol announcements command with web hooks obviously you can use regular messages but we'll use web hooks because you can obviously use web books for a bunch of different things aside from discord you can use it with slack messages i think i think slack has web hooks you can also use with github you can use google like which services have web hooks integration and you can just play around with that so now we want to basically take in as an argument the actual message that the user wants to send so for example our prefix dollar sign so announce so actually changes to announce okay and i want to say something like hello world and i want to at every one the problem is right now we're splitting all of our arguments with a regular expression that eats up all the white space so what we're going to do is we're going to do the reverse we're going to join everything with a space instead okay so we're going to join all of our arguments so const message and that's going to just we're going to be taking args which is our array of arguments and we're going to join it with a space the reason why is because like i said rx is an array that is split up by white space so in order to get the actual original message we need to reverse the operation and join it back and i'll console log too so you guys can see what it looks like now let's actually send the message with the web client so webhook client.send and we're going to pass in message and now give it some time and now tell the world so see how it says hello world at everyone which is pretty cool it also can mention rules which is uh very useful okay i can do announce hello perfect if i look at the console you can see that this right over here this array is the value of args and then this string over here is the value of message this is what happens after we join it okay the only downside to this is if you have a message that has a bunch of white space it will not work it will it will still work it will just join it back again that's probably the only uh caveat to this and that is pretty much it for this whole entire tutorial once again i want to thank brad for this amazing opportunity to be on his channel to teach you guys how to create a discord bot i had a lot of fun making this whole video if you guys have any questions feel free to leave a comment down below and any follow-up questions i'll be happy to help you guys out and that is pretty much it for this whole video peace
Info
Channel: Traversy Media
Views: 175,363
Rating: undefined out of 5
Keywords: node.js, node, discord, node.js discord bot, discord bot, Anson the developer, discord javascript, bot, javascript
Id: BmKXBVdEV0g
Channel Id: undefined
Length: 77min 32sec (4652 seconds)
Published: Mon Aug 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.