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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I'm sorry but this tutorial is honestly really poor considering it was made incredibly recent.

It displays no reasonable explanation of what your should do but it does show all the things not todo.

Issues include:

1) using requests for a async system being heavily blocking.

2) massive if if if blocks as a command handler mess

3) COMPLETELY IGNORES THE INTENT SYSTEM WHICH IS NOW A MASSIVE THING THAT SHOULD BE POINTED OUT.

4) ignores the existence of the commands framework built into discord.py that could actually help beginner structure their code better.

5) direct message author to bot user comparison meaning it'll still respond to other bots.

6) uses a sync dB specifically for repl. It which basically locks this tutorial to only be able to be ran on repl. It a host that really shouldn't be used for bots.

Overall I can only discourage the use of this tutorial as it's even worse than tutorials written years ago and seems to be written by someone with not only very little knowledge of async and things that should be considered with it but also very little effort to study the documentation themselves ignoring things like intents, the bot framework, common mistakes and more.

If you want a tutorial to show you what not todo then I recommend this.

👍︎︎ 7 👤︎︎ u/ChillFish8 📅︎︎ Dec 16 2020 🗫︎ replies

Hey do you guys know any good book (pdf) for learning python? (Exercises + solution)

👍︎︎ 1 👤︎︎ u/[deleted] 📅︎︎ Dec 17 2020 🗫︎ replies
Captions
Hi, I'm Beau Carnes with Free Code Camp. In  this tutorial that you're watching right now,   I'm going to show you how to create a discord  bot with Python that runs completely in the   cloud. You don't need to install anything on your  computer, and you don't need to pay anything to   host your bot. We're gonna use a number of tools,  including the discord API, some Python libraries,   and a cloud computing platform called repple  it 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 web hooks with  a discord bot to do things like give real   time updates on a GitHub repository. 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. Okay, 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 encouragements 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 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 going to 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, we'll  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, 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 or   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  going to go to this URL Okay, so now we have   to choose the 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 Python  code that creates the bot and run the Python code.   And speaking of Python 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 replique. The website for replique   is our epl.it. And replique is an online ID  EA, 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 gonna make our button 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.   Okay, now that I'm logged in, and I  click this new repple in the corner here,   now we just have to create, choose the language  that we're going to use for this new repple. The   cool thing about repple it is that you can use  a bunch of different languages with it. And so   that makes it simple. If you're going to run some  of these languages on your computer, you probably   are going to have to install some extra software  or extra things on your computer to be able to run   these languages. But if you just go to repple, it,  you can just do it right in your browser. And you   don't have to install anything. But we're going  to be doing this in Python. So I'll click Python.   And it will have a default name here. But  I'm just going to be calling it encouraged.   Okay, and I'll just create the repple.   So let's go over the main part of Rep. What we  have here, which is right here is the code editor.   And you can see a default with a file open  called main.py. And we can type our Python   code right in here. And then here over here  are the files, and you can create new files,   we will be creating a few new files in the course  of creating this bot, you can also create new   folders. And then over here is the console. The  console is where the standard output will appear.   And you'll be prompted to enter input if  you're going to have input in your code.   And in Python, it's going to it's going to double  as an interactive prompt. So you can actually type   in Python code right here. And it will evaluate  the code interactively. So that's pretty cool. And   then over here, we have some things on the side  here. so files, version control, if you're going   to associate with git, and or we're not going  to do that in this tutorial, though. And then if   you're going to install packages, but there's also  another way to install patches that we'll see.   And then the main thing we're actually going to  be using is the replique database. So one thing   cool about replique is it includes a database  as part of it. And so you can easily store   things that will keep stored in there even if you  stop your program and run it again the everything   store in the database will will still be in there.  So we will be using that later in this tutorial.   And then you can also change some settings,  just like the theme of font size, all sorts   of stuff like that. But let's go back to the  files because we are going to start creating   our discord bot right now. So I mentioned that we  are going to be using the discord.py library. And   the first thing we're going to do is import that  library. So at the top has this do import Discord.   And the cool thing here is that in repple it it  will automatically install this dependency when   you press the Run button. So I mean my permanent  and do very much right now but if I click Run,   we'll see on the side here, that it's Installing  the discord despit dependency, and then it will   be able to use it in the program right away. So  there's nothing extra you have to do to install   dependencies besides just importing them right  in your program. So let me talk a little bit   about discord.pi. Before we continue with this bot  here, it revolves around the concept of events,   and event, it's something you listen to  and then respond to. For example, when a   message happens in discord, you will receive  an event about it that you can respond to it.   So let's make a bot that replies to a  specific message, we're going to start   with a very simple bot, and then we're going to  add to it and just add more features as we go.   So the simple bot that we're going to start  with the code is actually taken directly from   the discord.pi documentation. It's a really good  example of like the most basic bot you can make.   And like I said, then we'll continue to build on  that after we get this simple bot created. Okay,   so we got the discord library, imported. So  let's add some more lines here. So now we are   going to create an instance of a client. This  is the connection to Discord. So do a client   equals discord dot client. This is just part  of the discord.py library. Now, we are going   to use the client that event decorator to  register an event that looks like this.   So I remember I mentioned that this client  uses events to make it work. So this is how you   register an event. And this is an asynchronous  library discord, that pie. So things are done   with callbacks. a callback is a function  that is called when something else happens.   So in this code, where you're going to use  the unready event, so I'm going to put async.   def on ready. So this event is going to be called  when the bot is ready to start being used. And   when it's start, when it's ready, when the bot is  ready, we're going to do this print. And this is   going to print right to this console right here.  So when the bot is run and ready to be used,   it's going to print this to the console, it's  going to say we have logged in as and then we're   going to actually be able to get the username  that's logged in as, and this is how you do that.   So there's a few ways to do it.  But so this is taking the client,   this zero is being replaced with the client. So  it's doing the client that user So that's this   client, this zero gets replaced with this client  here as client, that user to get the username.   And to make sure I have this right next  to the quote goes here. And there we go.   Okay, let's create another event. So that's the  event that happens as soon as the bot is ready,   and it's working. So the next event is if it  receives a message, if the bot senses a message   right in the discord server. So it's going to  start off just the same to register the event.   And we're going to do another async  function. But this one is on message.   So these function names are specifically  from the discord.pi library, it's looking   for these function names, to know what  to do, when certain things happen.   This onmessage event triggers each time a message  is received. But we don't want to do anything if   the message is from ourselves, which is the  bot. If the message is from the bot, we want   to do nothing. And this event takes an argument  called message. Or you can call it anything you   want. But it's traditional to call a message  because it's the message that's being received.   So this event triggers each time a message is  received. But we don't want it to do anything.   If the message is from ourselves, we're gonna make  it check if the message is from ourselves. So if   message dot author equals client, that user  looks like I spelled something wrong there.   So if the author of the message is the user,  our bot user, all we're going to do is return.   Next thing we're going to do is we're going  to see if the message starts with a command   that's being sent to our discord bot. And we can  make up those commands to be anything we want.   So in this case, I'll show you we're going to  do if message dot content that starts starts   with and we're just going to make it up to be  dollar sign Hello. For our bot. We're going to say   all the commands are messages that start with the  dollar sign. So if the message starts With dollar   sign Hello, then the boss should know that  it's received a command so it should return   something. And to return a message  back to discord, we're going to await   message dot channel dot send. So this is going to  send a message to the channel. And the message is   going to be Hello. Okay, so now so whenever the  bot notices that someone says dollar sign Hello   within discord, it's going to respond Hello with  an exclamation point. So this is everything we   need for the bot. But we still have to run the  bot. So here's the line to run the bot client   dot run. And then right in the client that run  we need to put our token, we need to put our bots   token which is like the password for the bot.  Before we just go and copy the token again and   put it right in here, there's something I should  tell you about replica that you need to be careful   with. By default, all replicates are public,  they're visible to anyone. So anyone can just   access your replica. And they could potentially  see anything you put in your replica, including   any secrets or keys. And remember, I told you  that this key is like a password, you don't want   anybody else to find out about it. But there is a  way to keep things secret. And that is to use an   environment variable. So in repple, it let me show  you how you can use environment variables, we are   going to first of all, create a new file here. And  the new file is going to be called dot E and V.   So that anything we put in a dot E and V file will  not be made public, nobody will be able to see   anything in a dot E and V file in replique, pretty  much on a public account on a free tier account.   Everything is public always except just this one  file dot E and V, we're gonna put token equals,   and we're gonna set the token to equal our  token. Now we're gonna have to go back and   get our token. So let's go back to our development  portal page, the discord comm slash developers,   and then I'm gonna go to bot gonna copy the  token. And then we will go back over here. Okay,   there's my token. I'm going to disable  this bot later and change the token. So   don't bother trying to figure it out to use it  for your own code. But now that we have our token,   we have to make it so it gets into our main  file. So to do that, we are going to have to   do have another import statement here. So I'm  going to put import OS, and then down here,   we need to access that token from the dot EMB  file. So what we're going to put in our code here   is oh s dot get e and V. And then I'm going to  put token. Now the reason why it's token here   is because that's the name I put right here, you  can actually use any name for the token doesn't   have to be token, you just have to make sure that  when you're getting the environment variable,   you use the same name here. So now when you  run the code, it's just going to replace   this right here with your token. Now, if you're  doing this locally, or you're doing in such a   way where you know, nobody else is ever gonna see  this code, you can actually put your token right   in here. And you don't even have to import o 's  here. But since we're doing it in a public way,   we need to make sure that nobody else sees our  token, or else they could get access to our bot.   And that's actually it for our basic bot.  Like I said, we're going to add some more   additional features later. But let's run it now  and test this out. So click run here. Okay, look,   we have logged in as encouraged bot. Now let's go  to our server. You can see look before it showed   as logged out, but now it shows it as logged  in. So let me let's try this. I'll say hello.   Now you can see that's from from me, and it didn't  respond. Because remember, the special command is   actually dollar sign Hello. So let's try that.  And it works encouraged by just responded with   Hello. So our bot works if we do another command  like Hi, how are you or not not another command   another message? It's not going to respond at all.  But if we do the dollar sign Hello. Then it will   respond. So we just created a basic discord bot  that responds to the command dollar sign Hello.   And obviously you can make it any command. So if  you don't want to respond to dollar sign Hello,   you can actually use any command here. And then  you can change what it's going to respond with.   Now that we have our basic  bot working, we'll improve it.   I said it's called encouraged bot for a  reason, the bot will respond with a message   of encouragement. Whenever someone sends a  message containing a sad or depressing word,   we're gonna make it so anyone will be able to add  encouraging messages for the bot to use. And these   messages will be stored in a database. So they  will save even after you stop and start your bot.   We're also going to add another thing in the  database. That turns on whether that turns   on or off whether the bot is is responding to  messages. And an extra feature of encouragement   we're going to do is that the bot is going to  return a random inspirational quote from an API,   when someone types the message, dollar sign  inspire into the chat. So let's start with   that last thing I said, we're going to start  by adding the dollar sign spider feature.   I found this API called Zim quotes.io. That will  return inspirational quotes. Now, you can use any   API. So if you don't want to use in quotes.io,  you can just find a different one. If somehow   this isn't working, just try to find a different  API, but we're going to use is in quotes.io. And   a lot of these principles will apply to really  any API that you're using. So let's go back to   our bot. And we are going to import some new  things to work with this API. So what we're   going to do is we're going to import a couple more  Python modules, we're going to add a get quote   function. And we're going to update our bot code  to call the function. So let's import requests.   This request module allows your  code to make an HTTP request   to get data from the API. The API returns  JSON. So we're going to have to import   the JSON module. What because that's going to make  it easier to work with the data that's returned.   Okay, now we're going to add a  helper function. It's called get,   quote, this is a function we can call to return  a quote from the API. So first, we're going to   use the request module to request data from  the API URL. So that's going to go like this,   the response we're going to store the response  from the API in a variable called response.   And so we have to use the request module like   this request that get we're going to  make a get request to this URL. Now,   I just had to find this URL by looking at the  documentation of this in quotes API. So HTTP,   S, colon slash slash is in quotes.io. Slash API  slash random, it's going to return a random quote.   Okay, now we're going to convert that response  into JSON. So we're going to do JSON, data   equals JSON dot loads, response, that text. So  this response that text, I just had to look in   the documentation of the API, and found  out that the response is going to have   this response that text that I can, I can load  into JSON. So now we have to do something with   this JSON, we have to get the quote out of it.  Now, this was a lot of trial and error, I had to   print out the JSON data and try to figure out  how how to get the exact part of that JSON   that I needed. So so this is what what we  ended up with, quote, equals JSON data. Zero,   Q. Now, that's going to be the quote q stands for  a quote. And it's just something that's part of   this in quotes API, the result that's returned  from this API is going to have a key called q   when the value is going to be the quote.  So we're going to start with the quote,   but then we want to add after the quote,  the author of the quote who said it.   So I'm just going to add a plus we're going to  do some string concatenate Nathan here. And I'm   going to make it have a space after the quote,  and it's going to be a dash, the dash is going   to be right before the person's name. It's just a  way to kind of show who's who's doing the quote.   So we'll do another plus here. And let's go back  into the JSON data. And this time, instead of the   queue, we're going to do a for author. Okay, so we  got a quote in here. Now, I can return the quote.   Okay, now we have to update our bot. So it uses  this get, quote function to get a quote, and then   return the quote, as a message to Discord. Let  me scroll down a little bit here. So now I'm just   going to update these lines here. This was just an  example to rich to respond to the message Hello.   But that's not what our final bot is going to  do. So I'm going to change this to inspire.   And let's move this over a  little bit. So we can see more   of the code. So if the bot gets this message  inspire, we're going to send a quote. So   the way we're going to send a quote is first  adding another line. So I'm gonna put quote,   equals get, quote. So it's going to call the  get, quote, function and return the quote here.   And now I can just return the quote, or I can  send the quote to Discord. Okay, at this point, we   can run the code and try it out. If it's already  running, we got to stop it, and then run it again.   And then we have to wait till it says it's logged  as encouraged bot. That means we can go back to   our Discord server, and say, Hi, bot doesn't say  anything. Let's see if it will say, Hello, hello.   Okay, that command doesn't work anymore.  Because we change it. Let's do inspire. Okay,   we have an inspirational quote. And let's try  inspire a few more times to see if it's random.   There's another one. There's another  one. See, we're getting a random quote,   for every time we do it. So this is the quote part  by failing to prepare you are preparing to fail.   And then we add that the space and then the  dash, and then the author Benjamin Franklin.   Okay, well, that works. Let's go, go back to our  bytecode. Now we're going to implement the feature   where the bot responds with encouraging messages  when a user posts a message with a sad word.   So in this, this time, the user isn't putting a  certain command into discord to get a result. This   time, the bot is going to be checking every word  in every message to see if there's a sad word. And   if it's if it notices a sad word, it's going to  return an encouraging message. So the first thing   we're going to do is to create a Python list that  contains the sad words that the bot will respond   to. So right after the client variable is created,  I'm going to create another one called sad.   Words. And the CI word. This is just going to be  a list like I said, oops, there we go. depressed,   unhappy. Now I'm gonna have a list of  words here, you can add additional words.   Really, you can put any words you like on  this list here. I'll have a miserable. And   let's do one more. How about depressing? Okay, so  we are going to make it so whenever the bot sees   one of these words in the message, it's going to  respond. So what is the bot going to respond with?   Well, now we're going to add a list of encouraging  messages that the spot will respond with when it   sees one of those words. So I'm going to make a  new variable called starter and encouragements.   Now, I'll tell you why this is  called starter encouragements.   It's because these are the there's going  to be just a few encouragements that the   bat starts with, but users will be  able to add more encouragement later   that are stored in the database. So the start  encouragements I'm just gonna do a cheer up.   Hang in there.   And you are a great person, or maybe it's  just another bot. So we'll do person slash bot   Feel free to add any number of encourages you  like, and remember later, user will be able   to add more encouragements. Now we need to update  our bot to use the two lists that we just created.   So the first thing we're going to do is import the  random module, because the bot will be choosing   messages randomly. So import random. And we'll  be using this later the random module to make it   get the random message. So we're going to update  the on message response here, we're going to add   something on in this on message async function,  that's gonna check all the messages to see if   they contain a word from the sad words list. And  then if the sad word is found, the bot will send   a random message of encouragement. So here's the  code for that. After we check to see if it has   starts with inspire, then we're going to check  just if it has any other word from the sad word   list. So this is how we're gonna do if any, if any  weight, if any word in message for word and sad   words. So this just means it's going to go through  every word, and the sad words. And gonna see if   any of those words are in the message. If it sees  any of the word in the sad words in the message,   then it's going to Oh, wait. Message dot channel  dot send. This is just like this line above here.   And what is it going to send, it's going to send  random dot choice, it's going to choose a random   choice from starter encouragements from  that list that we created. Okay, we can   test the button Now, let's see if there's  something wrong here. undefined name message.   Okay, got it. I got ahead of myself  here. Actually, this message should be,   it's actually supposed to be message content  by switch to message, because since we're   going to be using message content a bunch  of times, what I want to do actually, is   create the message variable right here message  equals message content. And then anytime we   see message content, we can switch it with  message here. Okay, let's test the bot now.   Okay, let's go back over here. Let's  see if the Inspire still works.   Worked. Now let's check the sad word. I am sad.   You're a great person bet Brett person's  life spot. Well, now, I am depressed.   Hang in there. Oh, that's making me  that is making me feel much better.   Thanks. This encouraging by is very encouraging.  So it's working, it can now respond, whenever it   sees sees a stad method, it's going to try to  encourage the person that put that sad message.   So at this point, you basically  know enough to create your own bot.   But let's keep going because we're going to  add some more advanced features that I already   discussed. Now 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 a sad word. Like I mentioned   before, we're going to use repple. It's built  in database to store user submitted messages.   This database is a key value store that's built  into every rebel. So let's add that to our code.   Now. Now, I showed you this before this tab here,  this the only really the only point of this tab   is to just show you the commands the  most common commands for the database.   And you don't actually need to get in from here  if you already know him because this type of men,   but let's go to the top of the code and we're  going to Alka just insert this so I inserted from   replique import dB. This allows us to use the  replicate database. Before we add new commands   for the bot. Let's add two helper functions that  the first one will be able to add custom messages   to the database. And the second helper function  will be used to delete messages from the database.   So let's go under the get quote function.   This new function is going to  be called update encouragements.   So that's gonna it's going to update  the encouragements in the database.   And it's going to accept an  encouraging message as an argument.   So first, it's going to check if encouragement  is a key in the database. So if encouragements   do that, right, encourage myths is in dB  keys. So DB keys just returns a list of   the keys in the database. So if increment  is already in the keys, it's going to get   those all the key all the incursions are  already in the database. encouragements   equals the way to get a value from the  database that stored under a certain   key is like this DB encouragements.  Okay, so we've gotten the encouragement.   Now we just have to add the new encouragement to  this list. So encouragements dot append that's   going to add something new to this list. And we're  going to type in the encouraging message, which is   the argument that was passed into this function.  And then after we append the new encouraging   message to the old encouraging messages, we  have to save it again into the database. So DB   encouragements equals   encouragements. Else, if there's not  already encouraged us in the database,   then we're going to have to  create it. So dB, encouragements   equals, and we're going to put a bracket  here because this is going to be a list,   but it's only going to have one message. And  the one message is the encouraging message   that was passed into this function. Okay,  so now, let's see what we did wrong up here.   Okay, that's supposed to be if encouragements  in dB key not is I just excellent typed an   essence of it in. So that's supposed to be in.  Okay, so now, we finished our helper function,   let's create a new one, which is to delete an  encouraging message. So def delete, encouragement.   And it's going to take an index, as is an  argument, the index of the message to delete.   So first, we need to get a list of the  messages from the database. So encouragement   equals dB. encouragement. So we got the  list of encouragement from the database.   Now we have to check if the length of  encouragement is more than the index, because   the person could pass in an index that's,  that's actually not in that that list.   The person could pass in an index that's actually  not part of the list. So if the length of   encouragements is more than the index, then we're  going to delete DLS. Delete. We're going to delete   the encouragements at that index. And finally,  we are going to save it into the database again   equals encouragements. And this can  actually be in this if statement.   Okay. Done. We can now add an  encouragement and delete and encouragement.   Now let's update our bot code to use these  functions. So users and discord can update   encouragements and delete encouragements,  right from Discord. Let's go down here.   So the first thing we're going to do, if you  remember, currently, it returns a choice from   the starter encouragements if it sees a sad word.  But we want to also be able to return a random we   want the random encouragement to also be able to  come from the database. So let's add some code to   be able to use the starter encouragements with any  encouragement that has been added to the database.   So first of all, I'm going to create a new  variable options equals starter encouragements.   Okay, so now, if there are if there's any  incursions in the data in the in the database,   if encourage meant in dB keys,   then we can, we can add those to the options. So  options equals options plus DB encouragements.   Okay, so now instead of picking from starter  encouragements, we're going to pick from options   of encouragement to return. Now we're  going to add some code to add a new   user submitted message to the database.  So it's going to be looking for a new,   a new command. So if the message  from discord starts with the command,   knew, if it starts with the command new, that  means it's going to be a new message to add to   the database. So the command that the bot is going  to be looking for is the command dollar sign new,   but then in the same message that you that the  user puts dollar sign new, they're going to put   a space, and then they're going to put the new  encouraging message. So we need to break off   the encouraging message from the the new command  because we don't want to add the new command to   the database, we just want whatever text comes  after the dollar sign new, whatever text that   the user submits, after DOS, I knew we want  to add that to the database. So let's do let's   get the encouraging message. And the couraging  message is going is going to equal message dot   split, we're going to split off the message  from New. So we're gonna do dollar sign new.   Okay, so we are now going to get an array of  the message that splits at New. And so we're   getting the second element in the array. And  that second element of the array is going to be   the new message. Now you can see that we're not  just splitting at New, we're splitting up new   space. So there's a space after that, because  we don't want the user is going to put dollar   side new space and then the message and we  don't want to add the space to the database.   We just want the message after the database  or after New. So we got the new encouraging   message. And now we just need to update the  encouragement with that new encouraging message.   Then we want to send something back to the user  so they know that it worked correctly. So wait   channel or message dot channel, dot send. New  and courage couraging message added. Oh, yeah,   that that should be and again, I don't know  why keep doing that. If incursions and DB keys.   Okay, now we'll add the ability  for a user to delete a message. So   a new function if message that starts with if  it starts with Dell, or delete, that's what Dell   stands for. That starts with Dell, we're going  to get the list of messages. encouragements or   we're not going to get the list of messages, we're  just going to actually just create an empty list   called encouragements, because we're going to want  to return the new list of encouragements later.   So we're going to start it started this with an  empty list. And we're going to check if there's   any encouragement already. So if there's  any encouragement already, in dB keys,   then we're going to delete one of them. First,  we have to get the index from the command. So   So if matches that starts with dollar sign,   delete. So if it starts with Dell, Dell, that  means delete. Then we're going to first create an   empty list. incur, we're going to create an eight  encouragements variable as an empty list that we   will add to in a second, if encouragements, we're  checking if the incursions are already in the   database. Because if there's no encourage us in  the database, and we don't want to try to delete   any of them, so if it's already, whoops, we'll see  if increments are in dB keys, then let's get the   index from the message that the user submitted  through Discord. So we have to get the index,   or you have to split it from from  the here. So message that split.   And just like before, we're going  to, it's going to look very similar   Dell. And this time, adding the space there is  optional. What let me show you what I meant.   So before we add a space here, this time, we don't  necessarily have to have a space because we are   going to convert it to an integer. So whether  there's a space or not, it will still correctly   convert to the integer, whatever not there, we're  going to type in a number, the user is going to   have a number after the command del, and it's  going to be converted to an integer in Python.   Now, we just need to delete the encouragement   that's at that index. And then finally, we need  to get a list of all the encouragements that   get the liver current list after it's  been deleted. Because we're going to   return that to the users so they can see  the updated list after it's been deleted.   And now we are going to send that back  a weight message dot channel dot send   encouragements. Okay, so the reason why we made an  empty list here is because if there, if there's no   encouragement in the database already, it's just  going to return the empty list here. But if there   is encouragements in the database, it will get  those encouragements and then return return those.   Okay, let's test this bot. And then  we'll add some a few extra features.   So I'm running the bot again.   Okay, the bots running and we will test  it. So let's test the other features first.   We still get an inspiring message.  Now let's try to add a new message.   You are amazing.   Okay, hi, just realized I made an error  way up here. database has no attribute key.   So that's what's the key with an S at the end.  So let's stop this again. Let's run this again.   That's why it's good to test frequently  to catch these, these mistakes like this.   Let's go back over here. Okay, now let's try  typing this again. Again, new, you are amazing.   New encouraging message added. Let's add another  one new way to go. That may not make sense based   on the message that the person put in to get that  message but that's okay. New, you are the best   coder in the world. Okay, now in order  to receive get one of these messages,   we mean we have to have some sad words. So I am  sad. Hang in there. We've already seen that one.   Let's just type in sad. Again. Way  to go. Now, way to go. That's one   of our new messages. So this was one of the  original messages, the starter encouragements,   but now we've received randomly one of  our new messages. I'm not sad anymore.   You are a great person bought. I'm depressed.  Well, I guess you have to spell it right.   Cheer up trying to see another new one.   You are amazing. That's another  new one that we put in there.   Okay, now let's see if we can delete a message.  So we should be able to delete a message with   doe. And let's just delete the  first one. So I'll just spend zero.   Now it's going to return the messages that  are left. Now just way to go and you are the   best coder in the world. It doesn't have a  me So that one was deleted. So that worked.   Right now the only way you can see all the  messages that are all the user submitted messages   is to delete one, it would be nice to be able to  see the user submitted messages without having to   delete one. So let's add some additional  features. Let's go back over to the bot.   So after adding the ability to get  a list of user submitted messages,   from right from discord, we're going to add  the ability to turn off and on whether the   bot responds to sad words. So you will be able  to actually update that right from discord,   whether it's going to be responding or not.  So we're going to kind of do all that at once.   First of all, we are going to create  a new key value pair in the database.   So right after the starter encouragements, we're  going to check if responding is in the database   yet. So this is gonna be a new key in the database  responding. If it's not in the database, db keys,   then we're going to add it. And it's  going to default it's going to start off   by being true respond, thing equals true.  And we do need to put a colon right here.   And now we only want to respond if this is true.  So down here, where it responds to words. That's   this whole section right here is when it's  is when it's responding to the sad word. So   I'm going to press tab here, because this is  all going to be inside an if statement. If   dB, we're going to get the value of the  responding key in the database responding.   So if that's true, then it will respond to the sad  words. If not, it won't respond to the sad words.   In a minute, we will add the  ability to update this value.   So let's go back before we add  the ability to update that value,   we're going to add the ability to list encouraging  messages. So if message that starts with   me, maybe like guess what the user command  is gonna be used, it's gonna be a list.   So if the user, the user types in list into  discord, then first of all we're going to get   we're going to make a empty list encouragements  equals an empty list because there may not be any   encouragements in the list yet. So we're going to  check if encouragements is in dB keys with an S.   Then encouragements, we're  going to add them we're going to   set the encouragements in the database to  the incursions, variable DB encouragements   and then we just have to return the  encouragements. So Oh, wait that message   or send the message to the channel. That send and  encouragements. Okay, now, users should be able   to get a list of the encouragements. Now we're  going to have the feature of changing whether   the bot is going to respond to sad words  or not. So if message that starts with risk   responding, quotation mark responding,   then we're going to get the value. So the idea  is that the user is going to put responding true   or responding false, if the discord message  that the bot is going to receive as a command.   So if a nano value, we're gonna get the value that  the user typed in, so is it true or false split   we're going to do just like before responding.  And again, we're going to put responding space   because we don't want this space right after  that. So we want the first element in that array,   which is the second the second element of the  array and index one. So we got the value. So now   if value dot lower, no matter if they typed  in lowercase or uppercase, we're going to   convert it to lowercase. So if it equals True,  then we're going to make sure that DB responding   equals true. And then let me scroll down  a little bit here. We're going to send   the message message back to Discord. Oh, wait.  Message dot channel dot send. Responding is on.   Okay, we're gonna have an else, I'm just gonna  copy this whole thing. So if valued at lower does   not equal true. I'll just change this to else.  We're actually making it so the person doesn't   actually have to type in false anything  but true will be assumed to be false. So   encouraging, responding is going to be set to  false. And it's going to say responding is off.   The code for the bot is complete.  This is all we're adding to the bot.   We're not quite done, though. We're  not quite done. We're going to test it.   And then I'm going to tell you  about one more very important   step. So let's try this. Let's go to back to  our bot. Oh, we have to run the bot again.   Okay, the bot is running. Hey,  bot. I am sad. Okay, it added that   now let's get a list of our messages.  Remember sent is stored in the database.   There should still be messages. Yep. See,  these are the messages messages we added before   the last time we ran our bot. But since it  stored in the database, they get stored dependent   between times that you've run your your code. So  let's add one. Oh, put add. Nice. I'm just nice.   Oh, I did that wrong. It's actually supposed to  be new. Make sure you type in the commands, right.   And let's list them again. Show is  nice. Now let's delete something delete   index one that should be the middle one. And Yep,  it shows them that we can even list them again.   Now let's see if we can change turn off and on  whether the body's responding. Let's confirm   that the bot is responding. I'm sad. Nice, huh?  Maybe it's saying nice because the data said it's   hard to tell with this bot. But let's see if we  can turn off the messages. So responding false.   If I type in sad, and it doesn't say anything,  if I type in depressed, it doesn't say anything.   But if I do responding, true.  Okay, let's do say I am depressed.   Okay, the bot thinks that's pretty nice. Now,  since we created the responding variable in the   database, it will save weather between times even  if you shut down your bot and rerun it, it will   still save whether it's supposed to be responding  or not. Okay, so technically, our bot is done,   but you don't want to stop now. Because if I close  my tab, the bot will just stop running. And then   it's not gonna work. If I close my browser, the  bots gonna stop running, it's not gonna work.   There is a workaround repple it will continue  running a web server even after the tab is closed.   Now this is not a web server. But we can  create one to run it at the same time as this.   But even a web server will only run  for up to an hour without any use. Oh,   here's the rep docs about the web servers. See  it says once deployed, the server will continue   to run in the background even after you close the  browser tab. The server will stay awake and active   until an hour after its last request after  which it will enter a sleeping stage.   So even if you have a web server web server  running, it will eventually stop running after an   hour after its last request. So if nobody's  using the disk or bottles have stopped working.   However, there is a way to make the bot  continually get requests so it never shuts down.   So actually rip rebel has announced that in  the future They will offer a paid plan that   allows code run continuously, no matter what  it will just never enter the sleeping sage.   But they don't offer that quite yet. It's  coming very soon, maybe it'll be available   by the time you see this video. But until that  is implemented, there is another way to keep   your bot running longer than an hour. And this  method even works with the free tier of replicas.   So to keep this bot running continuously,  we're going to use another free service called   uptime robot, up and rollback can be set to ping  the bots web server on replica every five minutes   with constant pings, the bot will never enter the  sleeping stage, and will just keep running. So we   have two more things to do to make sure our bot  is working as we want to make sure it's working   running continuously. We're going to create a web  server and repple it and set up uptime robot to   continuously ping the web server. Okay, creating  the web server is simpler than you may think. To   do it, we're going to create a new file, so  go to files and click the Add File button.   And this is just going to be called keep alive.pi.  Because the whole purpose of the web server   is to keep keep everything alive. Okay, I'm just  gonna paste in the code from the web server here.   It's pretty simple. Like I said, and it's not,  it's not really specifically important for the   bot, it's just a thing that we do in replique, to  make it keep going. So I will have a link to the   text of this web server right in the description  here. So you can just copy and paste it also.   But you can see it's not that long. So in  this code, we're going to import flask,   and we're going to use flask as the web server.  And it's going to return Hello, I'm alive to   anyone who visits the server, the server will run  on a separate thread from our bot, the server is   going to run on a separate thread from robot,  so they can both be running at the same time.   But the rest of this is not really relevant to our  bot, we just need the bot to run this web server.   So let's go back into main.pi. And then at the  top, we're going to have to import it. So from   keep alive, import, keep alive. And if you just  go back over here, that that it's importing this,   which is this is what runs our web server here. So  from it's going to import the web server, and then   we're going to go all the way to the bottom. And  right before the client runs, we're going to call   keep alive. And this will run our web server.  So if I stopped the bot and rerun the bot,   it's something different is going to happen  because of the bot because of the web server.   Okay, you can see a new window open right up here.   And this is our web server running and see  how it says Hello, I'm alive. That's from   our web server code where it says return Hello,  I'm alive. So whenever you go to the server,   it says Hello, I'm alive. Now the important  thing here is this URL. So this URL was created.   And this is the URL that we are going to ping from  uptime robot. So I'm going to copy this URL right   here. And then we're going to use it in the next  section. So now we need to set up uptime robot.   So the first step is to create a  free account, and then get logged in.   Okay, once you're logged into uptime robot,  just click add new monitor for the monitor type,   https and the friendly name, you can  name this anything we're gonna just put,   bought. And then for the URL, this is where  you paste in the URL that you copied all   over from repel repple it and the monitoring  interval we'll just leave this the same as   every five minutes. And then create,  monitor and then create monitor again   monitor created so I can close this. And  we should be able to see it right here.   Here it is. This is now going to ping our  bot every five minutes. So at this point,   it's going to be continuing, continuing  continue to run even after we close our tab   and even after an hour. Even if there's no  activity in discord, this bot will just run   continuously. Let's just try closing the  tab really quick. I'm just gonna copy this   URL, close a tab. Let's see if we  can just open the tab back up on here   and you can see it's still running  still running in this right here.   It could mean it is going to say something  like this every time it's pinged.   I don't know if that's from the ping  or not, or if it's just from reloading.   But we are now done. Now the bot will run  continuously, so people can always interact with   it on replicat. It's time for a bonus section, you  already know how to set up a Python bot and run   it continuously from repple it. Now I'm going  to show you how to set up another discord bot   to work with web hooks. We are going to connect  the bot to GitHub so that the bot will post a   message to your server whenever a certain GitHub  repository is updated. And for this GitHub bot,   you don't even need to code anything, or hosted  anywhere. Let me show you how it works. Okay,   to add this GitHub bot, we'll go to our server.  And then we'll go to the server settings here.   And then we'll click integrations. And  then there's web hooks create web hook.   And instead of calling it Captain Hook, I'm  going to name it. I'll name it GitHub. Okay,   and we'll put it in the general channel.  And then I'll just copy the webhook URL.   Okay, I'll make sure to save.   Okay, now I'm gonna go over to the GitHub  repo that I want to connect with this bot.   So I'm going to use the free code camp.org  GitHub repo. And then I'll go to Settings.   And then on the side here, it says web hooks.  Okay, now I'm going to click Add web hook.   And then I'm just going to paste in the URL that I  copied from Discord. Now here's an important part,   you can't just use the URL, you also have to  add something, I'm gonna add slash GitHub at   the end of the URL. And then for the content  type, I'll change it to application slash JSON,   and they'll scroll down. And for what  event should trigger the web hook,   I'm gonna do send me everything. Depending on what  you're trying to do, you may want to do just the   push event or select individual events. But I'm  gonna do everything. So I'll add the web hook.   And now I can go back right over  to my discord channel. If I wait,   we should start to see some events happening  on the from the Free Code Camp GitHub repo.   Let me go and just try to make an event  happen. I'll just unstarred star the repo.   And it worked. We just got a message  from the GitHub bot, new star added.   So now we've connected our bot  to GitHub through web hooks.   And the GitHub bot will post a message  whenever anything happens on the GitHub repo.   You can create bots to work with a lot of other  external services with web hooks. Okay, you've   reached the end of this tutorial. Now you've  prepared to go out and code your own discord bot.
Info
Channel: freeCodeCamp.org
Views: 805,243
Rating: 4.9485188 out of 5
Keywords: replit
Id: SPTfmiYiuok
Channel Id: undefined
Length: 68min 27sec (4107 seconds)
Published: Tue Dec 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.