Code a Discord Bot with JavaScript - Host for Free in the Cloud

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi, I'm Beau Carnes with free code camp.org. I'm  going to show you how to code a discord bot with   JavaScript that runs completely in the cloud  for free. I already made a course about coding   a discord bot with Python. And you can get  the link to that up here in the corner or   down in the description. We're gonna use a few  different tools, including the discord API,   a few JavaScript libraries, and a cloud computing  platform called replique. You probably already   know what discord is, and what a discord  bot is, since you're watching this tutorial,   but just in case you don't let me give you a brief  overview. discord is an app used for real time   text chat, discord servers are set up by people  and generally are focused on a certain community.   Many groups and organizations have discord  servers for people to chat with each other.   A Discord server often has many channels, which  are different rooms that you can chat in. A   discord bot, is a user in your Discord server that  is controlled by a program instead of a person.   You can code a discord bot to make it do whatever  you want. There are many things discord bots   are often used for. They can be used to chat,  respond to messages, manage servers, play music,   and more. You can also use a web hooks with a  discord bot to do things like give real time   updates on a GitHub repository. The bow we will  create in this course is called encouraged bot, it   can tell people inspirational quotes that it gets  from an API. Also, when the user says a sad word,   the bot will respond with an encouraging phrase.  Users can also add or remove encouraging phrases   that the bot uses directly from Discord. The first  step for creating a discord bot is to make sure   you have a Discord server to add the bot to, you  may already have one. But let me just show you   how to set one up really quick in case you don't  already have one. So you're going to have to go   to discord comm and log into Discord. And then you  just click the plus button here to add a server.   And I'll say for me and my friends. And I'll just  call it robot family server. OK, let's create   that. And now we have our server. Okay, so before  we can code our bot using Python, and using the   discord API, we must first create a discord bot  account right within Discord. So you're going to   have to go to the discord developer portal. I'll  put the link in the description, but it's just   discord comm slash developers slash applications.  So once you're on the applications page, you're   going to click this new applications button. And  then we're going to create a name for it. This bot   is going to send friendly encouragement to people  from in Discord. So I'm going to call it encourage   bot. Okay, I'll create that. Okay, now I'm going  to go to the bot tab here. And then I am going to   click Add bot and say yes, do it. Okay, we'll keep  the default the default settings as a public bot,   and then we won't require the OAuth to grant the  code grant. So now the the bot has been created.   The next step is to copy the bot token. Okay,  so I'll just click Copy to copy the token,   we're going to need that later. Now  this token is your bots password.   So make sure not to share it with anybody. It  could allow someone to log into your bot and do   all sorts of bad things. And it could even allow  someone to mess with your server. And also,   you can obviously regenerate the token if  they accidentally get shared. So now we have   to invite the bot to our server, we have the  bot user, and we have to get into our server.   To do that, we're gonna have to create an invite  URL for it. So let's go to the off to tab here.   And now we're just gonna go to the scope section.  Since this is a bot, we'll just click Buy here.   Now we have to scroll down a little bit more and  go to the bot permissions section. So we'll choose   the permissions we want for the bot. Our bot  is going to be mainly used for text messages.   So we don't need a lot of permissions. So your  bot may do different things. But let's just   choose some of these here. Actually will do all  of them on this line except send TTS messages. Now   you do need to be careful with this administrator  permissions. If you give your bot administrative   permissions it has a it will be able to control  your server in ways that you may not actually   want. And then I guess the last one we need to  add is the view channels are bots going to have   to view the channels. So after selecting the  appropriate permissions, we'll click the Copy   button right here. And this will copy a URL  that can be used to add the bot to a server.   And I guess we'll also have to recopy the  back key, we copy the back key earlier,   but now it's been re copied with this URL. So  we'll have to come back later to get the back key.   But now let's just open up a new tab, and I'm  gonna go to this URL. Okay, so now we have to   choose a server, we want to add the bot to I'll  choose robot family server, the only option,   but you will probably have more than more options.  So we'll click Continue. And we want to approve   that we want to do all these things, we want  to authorize the bot for all these things.   And I'm not a robot and the bot is authorized.  That means the bot has joined the server. So   if we actually go over into our server, we can see  encouraged bot just slid into the server. But also   you see over here that the bot is offline, it's  not going to be online till we write our code that   creates the bot and run the code. And speaking of  code, it's time to start that. Now I said earlier   that you don't have to install anything, you don't  have to use any software except your web browser,   we are going to be creating the entire discord bot  right in the cloud on replicat. The website for   replique is our epl.it. And replica is an online  ID e that you can just use in your web browser.   So it makes it a lot easier to code things if  you don't have to install any special software   on your computer. And then you can log into  it from multiple browsers, multiple computers,   and then you can easily access your code and your  programs no matter what computer you're using.   So we're going to make our bet on this. And then  it will also be completely hosted in the cloud,   we can even run the bot on this website. So we  don't have to run it on our local computer. And   that will make it so we can keep the bot running.  Even if our computer shuts down, or we're gonna   where we close the browser tab or anything like  that, I'll show you how to make the bot so it'll   keep running in the background continuously.  And then you don't have to worry about it.   If you don't already have account, you're  going to sign up. But I'm going to log in here,   I'm going to show you how to use replique to  create the bot from scratch. But just in case you   click the link in the description to the completed  bot, you're going to want to click fork here.   And you don't really want to start editing, you  probably want to fork it and then create your   own copy, and then start editing instead of trying  to request edit my copy. Okay, this is replique.   So let me show you how to start a new project.  We're just going to click on new new repple here.   And then I'm going to be using node j s. So  I'll click on node j s. And then if we want,   we can name it or you can use the default  name, I'm going to call it encourage bot   js, and then create repple. We're going to be  using the discord.js node library to write the   code for the bot. This go js is an API wrapper for  discord that makes it easier to create a discord   bot, and no GS or JavaScript. Okay, we're in our  project, you can see that we have our files here   we have our code that we're going to write here.  And then we have a console over on the side. And   then there's some different tabs, we are only  going to be mainly using the Files tab here. But   we'll also be using the database tab later.  But for now, let's get started. So first,   we're going to add the discord.js library. So  I'll add const discord equals require discord.js.   Now repple, it will automatically install  this dependency when you press the button the   Run button. So that's one cool thing about  repple is that dependencies are installed   automatically. And if you ever have a problem,  you can always go into the shell. And you can   install anything manually in the shell. But  let's go back over to the console. This core.js   revolves around the concept of events. An event  is something you listen to and then respond to.   For example, when a message happened, you will  receive an event about it that you can respond to,   we're going to start by making a very simple bot.  This is a bot that's going to respond to messages.   This is taken directly from the discord.js doc  documentation. So we'll start by making a very   simple one. And then we'll continue to add to  and make it more complex and make it work for our   purposes. So after this first line, we need to get  a client const client And equals new discord dot   client. So this is our discord client that  we'll be working with to connect to our Discord   server. This is the client for the discord.js  library. Okay, I talked about different events.   So our client is going to respond to different  types of events. One event is the ready event,   a ready event means that the bot has successfully  connected to our Discord server and is ready to do   more things. So let's add code now to respond  to the ready event. So you start with client   that on so on what on what event?  Well, the event is going to be ready.   So on the ready event, once it's ready to start  working, once it's connected to our server,   it's going to call this callback function.  So let's make this callback function here.   And we're just going to console  dot log something console dot   log, blog and as. And then when I get the  client name, or the username, the name of   the bot client, that user dot tag. Okay, now we're  going to respond to a different event, the event   is a message. So whenever the bot sees a message  in the discord server, it will get the message   event and it will do something whenever there's  a message, so client that on message. And then   it will call this callback function, we'll get  access to the message through this parameter here.   And we're going to check to see what the message  actually is. message content is the text of the   message. So we're going to check for this  example, if the message is the word Ping.   And if so, then we'll do something. So we're going  to reply message reply. Pong. So it will the our   bot will actually reply with another message  in the chat, and the message will be Pong.   Okay, now that we have our, our event set  up the ready event in the message event,   we need to have our client login to the server. So  to log into the server, we'll do client dot login.   Now here, we need to put our token. But we're  not going to actually you could put your token   as a string here. Remember, the token is a thing  when we created the bot on the developer page,   we copied the token. And if you don't have that  anymore, you can go back to the developer page and   copy the token. Again, you can put the token as a  string right here. But we're not going to do that.   And the reason is, because anything on repple,  it can be accessed by anyone on the internet.   So we don't want anybody to be able to have access  to our token that's like a password for our bot.   And then anybody could actually use our bot to do  anything that they wanted. We don't want that. So   we need to have a figured out we need a way to  keep our token a secret. And the way we're going   to do that is when it within an environment  variable repple, it has a special way of doing   that, we are going to create our environment  variable in just a second. Let's access it right   now. And then we'll create it. So to access  a environment variable, where we do process   dot E and V dot and then the name of the  environment variable, which in this case is going   to be token. So now we have to find a place to put  this. So this is how it works in redplum, we're   gonna create a new file, and I'm gonna call it dot  E and V. Anything in a dot E and V file is hidden   from the rest of the world. And you can see we  already have it here. So we have the word token,   you're gonna have to put the word token and then  an equal sign. And then you paste in your token   that you got for your bot. So in this case, this  is my token. And now that we have our token stored   in our environmental variable, it can get the  token and the dot E and V file. Like I said,   it cannot be accessed by it, nobody can see except  you in the other file. Everyone can see it but   the dot EMB only you can see it and that's how  we can get that token that nobody else can see.   I guess all of you just saw because I just  saw it on my screen, but more usually,   you know Not to be making a video so nobody will  actually see the actual environment variable.   So it's time I'm going to run this program.  Okay, it says we're logged in. So that worked.   Maybe you may have had a problem if somehow it  didn't automatically install discord dot j s. In   that case, like I said, you can install it in  this shell. But everything worked for me so and   I am going to go over to my Discord server  right now and see what it looks like. Okay,   show is that encouraged by is logged in. So  I'm going to do a chat, I'm gonna type in   ping pong. And see since it's a reply, it says  My name because encouraged by replying to me   and saying Pong. So it worked. We have a bot, and  it works. Now that we have a basic bot working,   we'll improve it. It's called encouraged bot for  a reason. The bot will respond with a message   of encouragement whenever someone sent a message  containing a sad or depressing word. Anyone will   be able to add encouraging messages for the bot  to use, and the user submitted messages will be   stored in the replique database. The bot will also  return a random inspirational quote from an API.   When someone types a message in spire into the  chat. We'll start with adding the Inspire feature,   we're gonna get inspirational quotes  from an API called Zen quotes.io.   Now this, it could actually work with any API,  you can use any API and just any API that's   going to return JSON or any sort of string, you  can use. And then you can get that information.   And then you can actually return the string as a  message with your bot. But I just found this is in   quotes that I owe is a easy way to get a quote  and encouraging or motivating quote, that we can   return with our bond. So since we are going  to be calling this API, we're going to need   to import the node fetch module. So up here,  I am going to do const. Fetch equals require   node fat. So we will be able to use that to call  the API. And now let's make a function called get   quote, that will be able to be called to get  a quote from the API. So function, get quote,   inside this function, we are going to return the  quote. And inside this function, we are going   to fetch the quote, and actually, we're going to  return it so return, and then I will do my fetch   command, fetch. And then here is where we put  the URL, HTTPS, colon slash less than quotes.io   slash API. Let's make more room here. So  you can see everything that I'm typing in   x n is going to be slash random. Okay, after  we fetch the information, do dot then this   is an asynchronous call. So first, it gets the  information, it may not get it right away, so but   it's going to wait till it gets information from  this URL, then it's going to do something with   that data. So let's put the callback function,  they'll call that will call after get that data.   We are going to return the result.  But we're going to convert it to JSON.   And then we are going to do something else with  that data that JSON that data that's returned.   We're going to return we're going to format it  in the exact way we want it to be formatted.   So we have the data. And let me just type in this.   Okay, to figure out exactly what I want  to return here and how to format this,   I had to use trial and error. So I  got the information from this URL,   and I converted JSON. And then I figured out  that the Q is quote, and the A is the author. So   this is going to put the quote here. And then it's  going to be there'll be a dash and there'll be the   author, you'll see exactly what it looks like when  we test this out. But so this is going to this is   going to return a random inspirational quote. And  to get the information into the format I wanted.   It's going to look like that. And so it's going to  return the quote when someone calls this function.   So let's update our message. Here, so it will  return a quote at the right time, we're going   to add something where, first we're gonna add a  new thing, we're gonna check to see if the message   is from the bot. If the bot ever says anything we  don't want the bot to, to respond to itself. So if   message dot author dot bot, that means if the  bot is author of the message, just return.   So we're not going to do anything in that  case. But if message content equals, well,   it's not gonna be equal, and ping anymore, we're  looking for the word inspire. So if it senses   that there's a message that's, that's inspire,  then we're gonna do the get quote, function,   get quote, that then, so you see where, where  respond, we're returning the risk, the what's   the result of this fetch. And so this is also a  promise that we can then this is an async call. So   once we get the result, then we're going to do  something, then we are going to take the quote   and we'll do a message dot channel, dot send.  So this is going to send a message to the whole   channel, and the message is going to be the  quote. Okay, let's try this out. First of all,   stop it because it was already running before  but we just started again for the new code.   And I'll wait till it shows it's logged in here.  Oh, see is installing the node fetch module now.   Okay, we're logged in. Okay, now  I will type dollar sign inspire.   They say love is the best investment The  more you give, the more you get in return,   Audrey Hepburn. Wow, I feel very inspired now.   And just so you know, it doesn't respond  like that to anything. If I say hello,   it doesn't have response, the only response if it  sees dollar sign and spider. Now we will implement   the feature where the bot respond with encouraging  messages when a user posts a message with a sad   word. So first, we need to create an array that  contains the sad words that the bot will respond   to. So I'm gonna go up here, and I'm going to  create an array, it's going to be called sad   words. And then I'll just  have a list of sad words.   Now you can use any list you want, and  any sad words about the sad, depressed,   unhappy, angry. And we'll do those. But  you can add more to this list if you   want. Now we'll add an array of encouraging  messages that the bot will respond where. So   let's add another array called  encouragements, const encouragements   Cheer up. Hang in there. You  are a great person, or bot.   Unlike before, feel free to add any phrases you  want, or just change them up. I'm just using these   three items for now, because later we'll add the  ability for users to add more encouraging phrases   for the bots to for the bot to use. So now we need  to update our bot to use the two lists we created.   So let's go down to the message function, we're  going to add another section here. And we're   going to add a section that's going to check to  see if the message contains a word from the sad   word list. And then if a sad word is found, the  bot will send a random message of encouragement.   So let's add that. And this time, I just paste  it in some code, and we're going to go over it.   So this is how we're going to check if the  message contains a word and the sad words list.   So we start with sad words, that sum, this is an  array method. Let me show you what it's gonna do.   We're going to go through each item in the array,  or in this case, a word. And then we're going to   check if message content dot includes if the  message includes the word, and we're going to   do that for every word in the list. And this sad  words that Psalm is going to return true if one or   more of these things are true. And so if at least  one of those words in the list is in the middle   It's going to be true. So then it's going to run  the code here. So first we're going to create a   single encouragement. So the encouragement  list, we have the encouragement list,   and then we want a random element. So this is how  we get a random element. Mat that floor rounds   down math dot random time, it's encouragements  that link. So this is just a way to get a random   index number for the encouragements array. And  then we're going to get that random encouragement,   and then message reply, encouragement. So  it's going to return that encouragement.   Okay, let's stop this. And then we're going  to restart it. Okay, we can test it. I am sad.   says Beau Carnes. Hang in there. It's very  encouraging. Thank you encouraged bought,   I am happy now. And it doesn't respond  at all. Because now that I'm happy,   it doesn't need to respond. But now I'm  feeling good. I'm feeling depressed.   Cheer up. Oh, that makes me feel a lot better.  And you can see us getting random phrases,   hang in there, cheer up, and include my name here.  Because it's a reply. If we go back to our code,   you can see that it's a message dot reply. Anytime  it's a reply, it's going to include the name,   the person who said the message, and then  the new message, which is the encouragement.   Okay, you basically know enough to  make your own bot. And you can go to   the discord.js documentation to find out even  more features you can do with your discord bot.   But now we're going to learn right now how to  implement more advanced features with this bot,   and store data using the rep database. So  let's make it possible to update the bot   right from Discord. A user should be able to  add more encouraging messages for the bot to use   when it detects the sad word, we're going to be  using replicates built in database to store user   submitted messages. The database is a key value  store that's built into every replica. So if you   click right here, it says database, it's going to  show you all these database commands you can use.   So you're always going to have to import  the database at the top, you can create a   new database, we're actually going to put some  of these commands right now. So if we just go   up to the top here, and then right under my other  require, I'm going to click insert here. And it's   just going to put this code right in here, current  database equals require, and then it's going to   get this database library. Okay, now we have to  create a new database. So we'll separate these   require statements from these other new statements  here. So I'm going to create a new database.   And when we run the code, this  will automatically install.   And if it doesn't, or if you're doing it on your  local machine, you can just go over to the shell   and do npm install, and then use this for the  name here. So what we're going to do down here   after the encouragements, we're  going to initialize the database   with these three encouragements. We're  gonna just add those to the to the database   if they're not already in the database. So  this is how we're gonna do that. dB dot get   encouragements. So we're going to get the  encouragement that are already in the database.   Well, if this is the first time this program  runs, there won't be any in there. But if the   program has run previously in our there's already  encouraged us in the database, then we'll get the   list of encouragements that are in the database.  So this is the key, enter this key key value   pairs. So this is the key and then we're going to  get the value which to start off with would be no,   but after we get the encouragement, then  we are going to take those encouragements.   And then we're going to do this.  I'm going to put this function here.   If we're going to check if there are no  encouragements, if there are no encouragements or   if the encouragements that link  is less than one. So if there's   zero encouragements, then we're going to add the  encouragements. So there will always be at least   something to encourage people with. So and by  the way, the reason why we have both of these   is because the first time this runs encouragements  will be no but later encourages could be zero.   So an either case, we want to do DB dot set.  We're going to set the encouragements key. We're   going to set this to be the encouragements. Well,  we want to set to be these encouragements, but   we're already using this variable here. And we're  going to use encouragements later. So this is what   let's just change this to be called starter  encouragements. So this is now this starter   encouragements, and we're going to set these  encouragements to be the starter encouragements.   users will be able to add custom  encouraging messages for the bot to use   directly from the discord chat. Before we add  new commands to the bot, let's add to let's   create two helper functions that will add custom  messages to the database and delete the messages.   So right after this, we're going to  create a helper function function.   Function update encouragements and it's going  to take one argument and encouraging message.   And so first, we're gonna get the list of  current encouraging messages, db dot get   encouragement. And then after  we get those encouragements,   then we're going to take those encouragements.   And in this function, we're gonna do something  with them. So we're gonna do encouragements   dot push, we're gonna push the new message on to  this list onto this array. encouraging message.   Now, we're going to do DB dot set,  we're going to add this to the database   with the new the new encouraging message on it. So   encouragements that's the key, and the new value  for that key is going to be encouragements. Okay,   now we have to make another function to delete  the encouragements. So function, delete, incur   encouragement. And this is going to take an index,  the index in the array that needs to be deleted.   So first, we're going to  get it's actually gonna be   somewhat similar. So I'm going to  get just copy this and paste this in.   But we are not going to be pushing any new any  new thing here. So what we're going to do instead   is if encouragement, that length is greater than  the index, we're only only good delete something   if the index is is actually an appropriate index.  So if the length is greater than the index,   then it's part of the index will actually apply  to this array. What we're going to do is incur   encouragements dot splice, this is how you remove  an item from an array index one. So at the index,   we're going to remove one item, and then we're  going to just move this up. I'm going to cut this   and put it here. So now we're going to set the  encouragement again to be this new encouragements   array that has the encouragements  remove that we want to remove.   Okay, let's update this section now. So now we  have to make sure that we are using our list from   the database when we return a message when there's  a sad word detected. So let's add something here.   We are going to have to get the the  messages from the database. So DB   dot get encouragement. That then encouragement.   And now all this code, I'm just gonna  cut that and then put it in here.   And this is actually should be encouraged minutes.   Okay, now let's make it so our bot will respond  to two new commands. So, if message dot content   dot starts starts with. So if the content starts  with dollar sign new, then we are going to do   something. So first we have to figure out what the  new the new messages. So the person will be typing   dollar sign new space, and then they'll put their  new message that should be should be added to the   list of encouragements. So the encouraging message  is going to equal message dot content dot split,   new one. So this is the method. And  that split is a JavaScript string   method. And we're going to split at this  and make sure there's a space at the end,   because the person's going to type in dollar  sign new space. And then we want anything after   that to be the new message. So index one in the  array, because this is going to create an array   index one will be anything after this. So  we'll get the encouraging message. And then   update encouragements with  the encouraging message.   And then after we do that, we can send a message  to the whole channel, message dot channel, dot   send. So our bot is about to send this  message new, encourage Jane and her urging,   there we go. Message added. Oh, see right here.  Encouraging. There we go. New encouraging message   added. Okay, now we have to do the same thing with  delete. So I'm just going to copy this whole thing   because it will be kind of similar, and then paste  it here. Now we're looking to see if the message   the user types in starts with D l for delete. And  we're splitting it D L. And this is now going to   be the index, the person is going to type in  dollar sign t l space and they're going to put   a number and the number is going to be the  index of the array that should be deleted,   sends it to number we should do parse int to  convert this to from a string to an int. So   parse int, and then I'm going to put this  in parentheses. So parse int, and instead of   update encouragements, we're going to be doing  the leet encouragement. And then I can put the   index here, and then message that child  that send new encouraging message deleted.   Okay, we can test this out here. So I'll  run the program. And we're logged in.   I am sad. Hang in there. That's really  encouraging. Well, let's become even   more encouraging, because I'm going to add a new  message. You are a great person, and also a great   singer. Hmm, that would make me feel encouraged.  Okay, how about nice avatar? And how about   that was a wonderful message. Okay, now I'm  going to see if it will use one of those new   messages we added. So I am sad. That was  a wonderful message. I am depressed. I   guess that was a wonderful message. Is that  encouraging if I'm saying that I'm sad, but   you are a great person and also a great singer.  Now let's see if we can delete one of these.   So let's delete. I'll do Dell and I'll put index  zero. And let me just delete a few of them.   Now we should have deleted all three of  the initial messages. So if what's sad   let's see if we see any of the initial messages.   We're not seeing any we're only seeing the new  ones because all the initial ones were deleted.   Okay, now we're going to add the ability to get  a list of user submitted messages right from   Discord. And we'll we will add the ability To turn  off and on whether the bot responds to sad words.   So that change whether it responds or not, we're  going to add a new variable or a new new key   value pair to the database. So let me scroll  up a little bit. And right before this where   we initialize the encouragements key value pair,  let's do the same thing with another one DB that   get responding. So this is a variable  called responding or the key value pair.   So as we're trying to figure out, if the  bot should be responding or not, then   value will get whatever the the initial value is.   And let's check if it exists yet.  If If value equals equals null,   that means that this is the first time the  program has run, so it's not in the database yet.   Then let's set it to something set, set it to  responding is going to equal or be set to true.   Now, this will only happen if the value  goes no. So if you run the program,   and you've set this to false, the next time  the program runs, it won't then be set to true,   it will still be saved in the database,  even if you stop and restart the program.   Okay, so the whole point of that was to change  whether it responds to the sad words or not.   So let's add something else  here. dB that get responding.   And we're trying to see if the, if the bots  be responding or not, then respond responding.   Now we're going to take this whole thing here.  And it's going to be inside this other section,   and let me indent this. So now we're  gonna check if responding is true. And   this is true. Alright, so be responding. There we  go. So now we're checking with for two things to   be true before it sends the encouraging message.  Okay, let's go down and we're going to add two   command to more commands, one of them will be to  list all the encouraging messages. And the other   command will be to change the responding  status to either true or false on or off.   So if message dot content dot starts  with, if it starts with less list,   then we're going to list all the messages. So  first, we have to get the messages DB dot get   encouragements dot then and encouragement.   And then actually this week  called encouragements methods dot   channel dot send encouragements. So send all  the encouragement to the channel as a message.   Okay, so for the next one, you  just copy this first line here.   If message content that starts with and  this time it's going to be responding.   So that means we're chaining the command  is to change whether the bot is going to   respond or not. So let's get the value first  value equals message dot content dot split.   responding with the space at the  end, one, this is just like before,   and the value is either going to be  true or false. So if value.to lowercase   equals equals equals True then DB that  sec responding to true. Also message dot   channel dot send. Chris respond. risk.  Oh, wait a second, Reese bonding is on   else. So if it's anything but true, we'll assume  it's false. So let me just copy this here,   else set responding to false, and the  message will be responding is off.   The code for the bot is complete.   We're gonna test it out one more time. And  then I will show you how to make the bot,   how to set the bot to run continuously,  or run the program and it's logged in.   Okay, let's just try to do a list. Let's  see if I can add something to the list.   And I'll do the list again. And adds the item I  added the item. Yes, and now it's on the list. Now   it's even easier to delete things. So I'm going  to delete that was a wonderful message because   we don't want to respond with that if someone  says a sad message. So I will do tell that's   the index two. And then I will do list   and oh, you know this is wrong new encouraging  message deleted. That doesn't make sense.   This is just be encouraging message deleted.  I don't know why I didn't notice that before.   So let's update that to encouraging.  That's going to be new. There we go. Look   looks like I have a few spelling errors here.  Okay, responding. And then down here. We have   responding. I don't know why I can't spell  this. Okay, let's play that again. Hopefully   we don't have any errors. or run that again. Keep  saying play because it looks like a play button.   And now I will list   and it's gone. It doesn't say that was a wonderful  message anymore. Sad. Oh, I didn't mean it. But I   shouldn't put the dollar sign but it should  still respond because it has the word sad.   Oh, but it's not there's no  space before it's so let's just   Oh, it is responding. Nice avatar said  you're a great person and also a great   singer. Now let's see if we can turn it  off and on. So responding, false or false.   Responding is off. So if I type in sad, it doesn't  respond. Now I can turn it back on responding.   respond, thing true. Responding  is on. So if I type in depressed,   you are a great person and also a great singer.   It works. We've created the bot, we've tested  it, everything works. Okay, so our bot is done.   But there's one more thing I want to show you,  which is how to make the bot run continuously   as it is if you ever Close your eyes. So if you  run this bot right now if I run this bot and   ever close my tab or close my browser, it will  stop running. But we want to make it so it'll   just run in the background. So you don't even  have to have your computer on for the discord   bot to be running. So there's two ways to do  it. One way is just to click right up here.   And you can see it says this repple can be kept  always on and there normally would be just a an on   button here to keep an eye on so just keep running  in the background. Even if you close things   however you do have to upgrade. So you do have  to upgrade to the paid plan to have this built   in way to keep the repple always on. So here's the  pricing for the hacker plan that repple offers.   You get five rebels that are always on if you  if you get it you also get a bunch of storage,   extra memory more speed, and soon you'll even  get offline rebels and $7 a month is pretty   reasonable. It's just like you would pay for  any hosting replique made this course possible   through a grant. And they're also offering three  months free of this hacker plan to the first   1000 people that clink that click the link in the  description with the coupon code. But like I said,   there's also a way to get your rep to be always  on for free without even signing up for this.   And I'm going to show you that right now. If you  run your bot and repple it and then close the tab,   it's running in your bot was stopped running.  However, there is a workaround. So the workaround   without having to pay anything is that replica  will continue running a web server even after the   tab is closed. But even a web server will only run  for up to an hour without any use. So to keep the   bot running continuously, we'll use another free  service called uptime robot. That's the uptime   robot.com, it can be set up to ping the bar to  web server on rappeler every five minutes. And   then with constant pings, the bot will never enter  the sleeping stage and will just keep running. So   we have to do two things, we have to create the  web server and replicate and set up uptime robot   to continually ping the web server. So let me  show you how we're going to create the webserver.   First of all, I'm going to create a new file,  and just click Add File and this is going to be   called server dot j s. Okay, and now we're going  to be using express to create the web server.   Okay, so we'll do const.  Express, equals require, Express.   And then we to create the server  so const server equals Express.   Now that we have the server, we need  to create a route. And the route is the   thing that is that's going to be pinged from  uptime robot. So here we go, server, dot all.   So this creates a new route at just slash  like just at your root domain. And it's   going to respond to all HTTP requests.  That's what this all is. So like get   put, post, and it's just going to say it's going  to send back bot is running. So now that we have   the server ready, we have to create a function  that starts the server. So function, and we're   just going to be calling it keep alive because  it's going to keep alive our replique instance.   Server dot, listen. We'll listen at Port 3000.  And then we're just going to log something.   Whoops, to be equal sign.  Console dot log server is ready.   So when this keepalive function is run,  it's going to the server is gonna start   listening at Port 303,000. And it's going  to log servers ready. And now we just have   to export this so we can import it and run  it from our bot file. So module dot exports.   Equals keep alive. Okay, now let's make sure that  when our bot runs, automatically runs this server.   So let me go into our index dot j, s. And  then at the very top here const keep alive   and require. And then we're just going to  require the server that we just created   server. And then down here,  right before our client logs in,   keep alive. So we'll start our server  will start that server function.   Okay, now let's run this and  see if the server starts.   Oh, actually, we need to make keep alive,  not keep alive ever. So let's try that again.   And we can tell that the server started  because it opened up a new window here and   it says bot is running. So I'm going to copy  there's a link right here which I'm going to   select and copy. This is our server link that  we're going to have to put into uptime robot.   So let's go set this up on uptime robot to ping  our server every five minutes. Okay, I uptime   robot.com. And you're either gonna have to  register or login. And once you get logged in,   you'll see this dashboard and there's this  add new monitor here. So I'm gonna click that.   And monitor type, this is going to be an HTTPS  monitor. And I can just call this encouraged bot.   And then I'll just paste in the  URL that I copied from our applet.   And then it's on every five minutes.   And then, if I want I can select myself to notify  if you want to be notified when something goes   wrong, but you know, I'm not going to notify  myself so I'm just going to do create monitor   and it's created. Now, our bot will continue  running after a close the tab or close the window.   Even if my computer is turned off. The  discord bot will still just keep running.   We're done we learn how to create a discord bot.  We learn how to make it continually run. And   now you know enough to start customizing this  and creating your own bot. I would recommend   checking out the discord.js documentation  to learn even more commands you can add   Okay, you reach the end, go and create your  own bots. But remember, use your code for good
Info
Channel: freeCodeCamp.org
Views: 179,686
Rating: undefined out of 5
Keywords:
Id: 7rU_KyudGBY
Channel Id: undefined
Length: 56min 30sec (3390 seconds)
Published: Mon Mar 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.