Code a Discord Bot with Python - Host without charge within the Cloud
hello, I'm Beau Carnes with free Code Camp. In this educational that you're watching at present, . i will enable you create a discord bot with Python that runs totally in the . cloud. You don't want to install anything on your computing device, and you do not need to pay whatever to . host your bot. we're gonna use quite a few tools, adding the discord API, some Python libraries, . and a cloud computing platform referred to as repple it you probably already know what discord is, . and what a discord bot is, since you are watching this tutorial , . yet simply if you do not permit me provide you with a brief assessment. discord is an app used for real time . textual content chat, discord servers are hooked up by means of people and generally are concentrated on a certain community. . Many organizations and organisations have discord servers for people to talk with one another..
how to create a bot in discord and say definite, 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 supply. So now the bot has been created. . the next move is to copy the bot token. okay, so i'll just click on Reproduction to repeat the token, . we will want that later. Now this token is your bots password. So make sure not to . share it with anyone. It may enable someone to log into your bot and do all sorts of bad things. . And iit could even enable somebody to mess with your server. And also, you could definitely regenerate . the token in the event that they by accident get shared. So we now have to ask the bot to our server, . we've the bot consumer, and we have to get into our server. to try this, . we will ought to create an invitation URL for it. So let's go to the off to tab here..
And now we're just gonna go to the scope part. considering the fact that it is a bot, we are going to simply click on Buy here. . now we have to scroll down slightly extra and go to the bot permissions part. So we are going to choose . the permissions we'd like for the bot. Our bot is going to be mainly used for textual content messages. . So we don't desire a great number of permissions. So your bot may do different things. Yet let's just . choose these kind of right here. In fact, we'll do them all in this line, apart from send TTS . messages. Now you do should be cautious with this administrator permissions. If you give . your bot administrative permissions it has, it will have the ability to manage your server in ways that . you will not correctly favor. 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 proper permissions, we will click on.
encouraged bot just slid into the server. But also you spot over here that the bot is offline, it's . not going to be on line until we write our Python code that creates the bot and run the Python code. . And speaking of Python code, it is time to start that. Now I pronounced earlier that you don't have . to put in anything, you don't have to use any application except your internet browser, we're going . to be developing the entire discord bot, right in the cloud on replique. The website for replique . is our epl.it. And replique is a web ID EA, that you'll just use in your web browser. . So it makes it a lot simpler to code things if you do not have to install any different software . on your desktop. after which you can log into it from diverse browsers, numerous computer systems, . and then you can effortlessly access your code and your courses no matter what computing device you're utilizing..
So we're gonna make our button this and then it will also be totally hosted in the cloud, . we may even run the bot on this web site, so we do not have to run it on our nearby machine. . And that will make it on the way to preserve the bot jogging, even if our computer shuts down, or we're . gonna where we close the browser tab or anything like that, i am going to make it easier to make the bot. So . it will retain walking in the history continuously. And then you do not have to stress about it. . If you do not have already got account, you're going to enroll. but I'm going to log in here. . Ok, now that i'm logged in, and I click this new repple within the nook here, . now we simply need to create, choose the language that we're going to use for this new repple. The . cool aspect approximately repple it is that you can use a gaggle of different languages with it. And so.
That makes it simple. If you are going to run some of these languages on your laptop, you probably . are going to have to install a few additional software or extraa things on your computer that allows you to run . those languages. but when you only visit repple, it, you can just do it right on your browser. And you . do not have to install whatever. but we're going to be doing this in Python. So i am going to click Python. . and it will have a default call right here. But I'm simply going to be calling it advocated. . okay, and I'll simply create the repple. . So let's go over the most portion of Rep. What we have right here, which is right here is the code editor. . And you can find a default with a dossier open called leading.py. And we are able to kind our Python . code right in here. after which here over here are the documents, and you can create new documents,.
We will be creating a few new documents in the course of creating this bot, you also can create new . folders. And then over this is the console. The console is wherein the standard output will show up. . And you'll be caused to enter input if you're going to have enter on your code. . And in Python, it is going to it's going to double as an interactive immediate. So one can type . in Python code here. and it will evaluate the code interactively. So that's surprisingly cool. And . then over right here, we've some things at the side right here. so files, version control, if you're going . to go together with git, and or we are not going to do this in this educational, though. after which if . you're going to install programs, but there's also another way to put in patches that we are going to see. . And then the most element we're correctly going to be utilizing is the replique database. So some thing.
Cool approximately replique is it encompasses a database as part of it. And so you may effortlessly store . matters which will keep stored in there no matter if you discontinue your program and run it again the everything . shop in the database will will still be in there. So we would be using that later during this tutorial . . after which you also can change some settings, like the subject of font length, all sorts . of stuff like that. but let's go back to the documents because we'll start creating . our discord bot at present. So i discussed that we are going to be utilizing the discord.py library. And . first thing we will do is import that library. So at the tip has this do import Discord. . And the cool factor here's that during repple it it will immediately install this dependency when . you press the Run button. So I suggest my permanent and do greatly at the moment but if I click on Run,.
we are going to see at the part right here, that it is Installing the discord despit dependency, and then it will . be able to use it in this system straight away. So there's nothing additional you need to do to install . dependencies anyway simply importing them right in your software. So allow me communicate a little bit . approximately discord.pi. Before we retain with this bot right here, it revolves round the concept of hobbies, . and event, it's whatever you listen to after which respond to. for instance, when a . message occurs in discord, you will receive an event approximately it that you'll be able to respond to it. . So let's make a bot that replies to a particular message, we will start . with an easy bot, after which we're going to add to it and just add extra features as we move. . So the simple bot that we're going to start with the code is truly taken directly from.
The discord.pi documentation. It's a really good example of like the most basic bot you can make. . And prefer I pronounced, then we are going to maintain to build on that when we get this simple bot created. okay, . so we got the discord library, imported. So let's add a few extra traces here. So now we are . going to create an instance of a consumer. 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're going . to use the customer that event decorator to check in an event that looks like this. . So I keep in mind i discussed that this client makes use of activities to make it paintings. So that 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 anything else occurs..
So during this code, wherein you're going to use the unready event, so i'll put async. . def on all set. So this event goes to be called when the bot is able to begin being used. And . when it's start, when it's prepared, while the bot is ready, we'll do that print. And this is . going to print right to this console here. So when the bot is administered and ready to be used, . it will print this to the console, it's going to assert we've logged in as after which we're . going to actually be capable to get the username that's logged in as, and that's how you do this. . So there's a few methods to do it. but so it is taking the customer, . this zero is being changed with the client. So it is doing the client that consumer So that's this . client, this 0 receives replaced with this client here as purchaser, that person to get the username..
And to make certain i've thiis right next to the quote goes right here. And there we cross. . okay, let's create one more event. So that's the occasion that happens once the bot is in a position, . and it's working. So the subsequent occasion is that if it gets a message, if the bot senses a message . correct within the discord server. So it's going to start off just a similar to register the event. . And we're going to do a further async function. but this one is on message. . So those function names are specifically from the discord.pi library, it is looking . for those operate names, to grasp what to do, whilst sure matters occur. . This onmessage event triggers every time a message is acquired. Yet we don't want to do whatever if . the message is from ourselves, which is the bot. If the message is from the bot, we want.
To do not anything. And this occasion takes an argument known as message. Or you may call it anything you . want. But it's classic to call a message because it's the message that's being obtained. . So this event triggers every time a message is received. Yet we don't want it to do anything. . If the message is from ourselves, we're gonna make it determine if the message is from ourselves. So if . message dot writer equals purchaser, that user seems like I spelled something incorrect there. . So if the author of the message is the user, our bot consumer, all we'll do is go back. . subsequent thing we'll do is we are going to see if the message starts offevolved with a command . that's being sent to our discord bot. And we can make up those commands to be anything we'd like. . So subsequently, i am going to show you we're going to do if message dot content material that starts offevolved starts.
With and we're simply going to make it as much as be dollar sign whats up. For our bot. We're going to say . all of the commands are messages that begin with the dollar sign. So if the message starts offevolved With dollar . signal whats up, then the boss ought to recognize that it is obtained a command so it may return . anything. And to return a message back to discord, we will await . message dot channel dot send. So this goes to send a message to the channel. And the message is . going to be howdy. okay, so now so anytime the bot notices that somebody says greenback signal Hello . inside discord, it's going to respond whats up with an exclamation factor. So that's every thing we . want for the bot. but we still have got to run the bot. So here's the line to run the bot client . dot run. after which correct in the client that run we have to put our token, we need to placed our bots.
Token that's like the password for the bot. Before we just pass and duplicate the token returned and . placed it right in right here, there is something I should let you know about reproduction it is advisable be careful . with. by default, all replicates are public, they're noticeable to anybody. So anyone can just . access your reproduction . And they may potentially see whatever you install your replica, including . any secrets or keys. And keep in mind, I informed you that thiis key is sort of a password, you don't want . anyone else to study it. but there is a manner to keep matters mystery. And that's to apply an . surroundings variable. So in repple, it allow me show you how you can use environment variables, we are . going to first of all, create a new dossier here. And the hot dossier goes to be known as dot E and V. . So that whatever we installed a dot E and V dossier will now not be made public, no person will have the ability to see.
anything in a dot E and V dossier in replique, pretty much on a public account on a loose tier account. . Everything is public necessarily apart from just this one dossier dot E and V, we are gonna put token equals, . and we're gonna set the token to equivalent our token. Now we are gonna have to return and . get our token. So let's return to our development portal web page, the discord comm decrease developers, . and then i am gonna go to bot gonna copy the token. And then we will return over right here. Ok, . there's my token. i'll disable this bot later and alter the token. So . do not bother attempting to determine it out to apply it in your own code. Yet now that we have our token, . we need to make it so it receives into our main file. So to try this, we will have to . do have a different import declaration right here. So I'm going to put import OS, and then down right here,.
we have to access that token from the dot EMB dossier. So what we're going to installed our code here . is oh s dot get e and V. after which I'm going to positioned token. Now the explanation why it's token here . is because that is the name I positioned right here, you can actually use any call for the token doesn't . should be token, you just have to make sure that when you're getting our surroundings variable, . you utilize an identical call right here. So now when you run the code, it is simply going to replace . this here along with your token. Now, if you're doing this locally, or you are doing in such a . manner in which you recognize, no one else is ever gonna see this code, you can placed your token right . in right here. And you do not actually have to import o 's right here. but for the reason that we are doing it in a public manner, . we want to make sure that nobody else sees our token, in any other case they could get entry to our bot..
And that's actually it for our hassle-free bot. Like I pronounced, we will add some more . extra features later. but let's run it now and experiment this out. So click on run here. Ok, look, . we've logged in as endorsed bot. Now let's go to our server. you'll find appear before it showed . as logged out, but now it shows it as logged in. So let me let's do that. I will say hi there. . Now you will see that is from from me, and it didn't respond. Because remember, the distinct command is . actually greenback signal howdy. So let's attempt that. And iit works recommended by using simply replied with . good day. So our bot works if we do one other command like Hi, how are you or no longer now not one other command . yet another message? it is not likely to respond in any respect. But if we do the greenback signal hi there. Then it will . reply. So we simply created a ordinary discord bot that responds to the command dollar sign hi there..
and clearly you can make it any command. So if you do not need to answer dollar signal Hello, . you may use any command here. And then you could difference what it will respond with. . Now that we have got our basic bot working, we'll improve it. . I reported it is known as advocated bot for a reason, the bot will respond with a message . of encouragement. Whenever somebody sends a message containing a sad or depressing notice, . we are gonna make it so anyone will be able to add encouraging messages for the bot to apply. And these . messages would be saved in a database. So they will keep even once you discontinue and start your bot. . We are also going to add another thing in the database. that activates whether that turns . on or off no matter if the bot is is responding to messages. And yet another characteristic of encouragement.
we'll do is that the bot is going to return a random inspirational quote from an API, . when someone versions the message, dollar sign encourage into the chat. So let's start with . that very last thing I said, we'll start by means of adding the dollar sign spider function. . I found this API referred to as Zim charges.io. That will go back inspirational charges. Now, you need to use any . API. So if you don't want to use in rates.io, you may just find aa different one. If somehow . this isn't working, just attempt to find a different API, but we're going to use is in quotes.io. And . some of these ideas will apply to really any API that you are using. So let's go back to . our bot. And we are going to import a few new matters to work with this API. So what we're . going to do is we're going to import a pair more Python modules, we're going to upload a get quote.
Operate. And we will 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'll need to import . the JSON module. What because that is going to make it easier to paintings with the data that's lower back. . Ok, now we'll upload a helper function. It's referred to as get, . quote, this can be a operate we will name to return a quote from the API. So first, we're going to . use the request module to request information from the API URL. So that's going to move like this, . the reaction we will store the response from the API in a variable called reaction. . 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 locate this URL by using watching the documentation of this in prices API. So HTTP, . S, colon lower diminish is in costs.io. diminish API cut down random, it will go back a random quote. . Ok, now we're going to convert that response into JSON. So we're going to do JSON, data . equals JSON dot plenty, reaction, that textual content. So this response that text, I simply had to look in . the documentation of the API, and found out that the reaction goes to have . this reaction that text that I'm able to, i will load into JSON. So we have to do something with . this JSON, we have to get the quote out of it. Now, this changed into plenty of trial and blunder, I had to . print out the JSON information and check out to figure out how a way to get the exact portion of that JSON . that i wanted. So so it really is what what we ended up with, quote, equals JSON information. zero,.
Q. Now, that is going to be the quote q stands for a quote. And it's just something that's part of . this in quotes API, the outcome that is returned from this API goes to have a key called q . while the price goes to be the quote. So we'll start with the quote, . but then we wish to upload after the quote, the writer of the quote who mentioned it. . So i am just going to feature a plus we're going to perform a little string concatenate Nathan right here. And I'm . going to make it have an area after the quote, and iit's going to be a touch, the dash is going . to be right before the person's name. it is just a thanks to variety of show who's who is doing the quote. . So we'll do an additional plus right here. And let's go back into the JSON information. And this time, instead of the . queue, we will do a for writer. okay, so we bought a quote in here. Now, i can return the quote..
okay, now we have to update our bot. So it uses this get, quote operate to get a quote, and then . go back the quote, as a message to Discord. Let me scroll down a bit of here. So now i am just . going to update these traces here. This became just an instance to wealthy to respond to the message hi there. . Yet that is not what our final bot is going to do. So I'm going to change this to motivate. . And let's pass this over a little bit. that will see more . of the code. So if the bot gets this message inspire, we will send a quote. So . the style we'll send a quote is first adding a further line. So I'm gonna put quote, . equals get, quote. So it is going to call the get, quote, function and return the quote here. . And now i can simply return the quote, or I can send the quote to Discord. Ok, at this factor, we.
Can run the code and check it out. If it's already strolling, we got to stop it, and then run it returned. . after which we need to wait until it says it's logged as advocated bot. that means we can go back to . our Discord server, and say, Hi, bot would not say anything. let's have a look at if it's going to say, Hello, hiya. . okay, that command would not work anymore. due to the fact we modify it. Let's do inspire. okay, . we've an inspirational quote. And let's try inspire a few extra times to see if it's random. . There's another one. There's another one. See, we are getting a random quote, . for each time we do it. So it really is the quote part by using failing to prepare you're making ready to fail. . And then we add that the gap and then the dash, after which the author Benjamin Franklin. . Ok, good, that works. Let's move, go back to our bytecode. Now we will enforce the feature.
Wherein the bot responds with encouraging messages whilst a person posts a message with a sad note. . So during this, this time, the user is not putting a sure command into discord to get a influence. This . time, the bot goes to be checking each word in each message to work out if there's a sad notice. And . if it is if it notices a tragic be aware, 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 phrases that the bot will respond . to. So correct after the client variable is created, i will create a different one called unhappy . . phrases. And the CI notice. that's simply going to be a list like I mentioned, oops, there we move. depressed, . sad. Now i am gonna have a listing of phrases right here, you could upload extra phrases. . Really, you can positioned any words you want on this record right here. i'll have a depressing. And.
Let's do a different. How approximately depressing? okay, so we will make it so every time the bot sees . the sort of words in the message, it's going to reply. So what is the bot going to reply with? . good, now we will upload a list of encouraging messages that the spot will respond with when it . sees a kind of words. So i'll make a new variable known as starter and encouragements. . Now, i'll tell you why this is known as starter encouragements. . This is because these are the there's going to be just a few encouragements that the . bat starts offevolved with, yet users will be able to add extra encouragement later . which are saved in the database. So the start encouragements i am simply gonna do a cheer up. . Hang in there. . And you are a notable individual, or perhaps it's just aanother bot. So we are going to do person shrink bot.
be at liberty to add any number of encourages you like, and remember later, person would be able . to feature extra encouragements. Now we have to update our bot to use both lists that we just created. . So first thing we're going to do is import the random module, since 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 will add . whatever on in this on message async function, that is gonna assess all the messages to work out if . they comprise a notice from the sorrowful phrases list. And then if the sorrowful be aware is found, the bot will send . a random message of encouragement. So here is the code for that. once we verify to see if it has . starts offevolved with motivate, then we're going to check just if it has any other word from the sad note.
List. So that is how we are gonna do if any, if any weight, if any word in message for be aware and sad . phrases. So thiis just ability it's going to go through every word, and the unhappy phrases. And gonna see if . any of those words are in the message. If it sees any of the word in the sorrowful words within the message, . then it will Oh, wait. Message dot channel dot send. This is rather like this line above right here. . and what's it going to send, it is going to send random dot selection, it is going to select a random . selection from starter encouragements from that list that we created. okay, we can . test the button Now, let's examine if there's anything wrong here. undefined name message. . Ok, bought it. I got previous to myself here. In fact, this message ought to be, . it's aactually supposed to be message content by using change to message, due to the fact considering the fact that we are.
Going to be utilizing message content material a bunch of times, what I want to do in fact, is . create the message variable here message equals message content. And then every time we . see message content, we can change it with message here. Ok, let's experiment the bot now. . Ok, let's go back over here. Let's see if the encourage still works. . Worked. Now let's examine the sorrowful notice. i am sad. . You're a superb person guess Brett person's life spot. Well, now, i am depressed. . dangle in there. Oh, that's making me that's making me feel far better. . thanks . This encouraging by means of is incredibly encouraging. So it is operating, it may now respond, each time it . sees sees a stad technique, it will try to inspire the person that positioned that sad message. . So at discord bot app , you basically be aware of sufficient to create your personal bot..
Yet let's hold going due to the fact we're going to add a few more progressed features that I already . mentioned. Now let's give the opportunity to update the bot correct from Discord. A person must be able . to feature more encouraging messages for the bot to use when it detects a tragic notice. Like I mentioned . before, we'll use repple. it is built in database to shop person submitted messages. . This database is a key magnitude shop that is built into every insurgent. So let's upload 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 typical instructions for the database. . And you don't really want to get in from here if him because this type of guys, . but let's visit the tip of the code and we're going to Alka simply insert this so I inserted from.
Replique import dB. this enables us to apply the reflect database. earlier than we upload new commands . for the bot. Let's upload two helper functions that the first one will have the ability to upload custom messages . to the database. And the second helper function would be used to delete messages from the database. . So let's pass below the get quote operate. . This new function goes to be called replace encouragements. . So that's gonna it will update the encouragements in the database. . And iit's going to be given an encouraging message as an argument. . So first, it will examine if encouragement is a key in the database. So if encouragements . do this, 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 will get.
Those all the key all the incursions are already in the database. encouragements . equals the way to get a cost from the database that stored under a certain . key's like this DB encouragements. okay, so we've gotten the encouragement. . Now we simply ought to add the hot encouragement to this record. So encouragements dot append that's . going to feature something new to this list. And we're going to variety in the encouraging message, which is . the argument that became passed into this operate. And then once we append the hot encouraging . message to the old encouraging messages, we have to reserve it back 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 placed a bracket here due to the fact this is going to be a listing, . yet it's only going to have one message. And the only message is the encouraging message . that was exceeded into this function. okay, so now, let's examine what we did incorrect up here. . Ok, that is speculated to be if encouragements in dB key now not is I simply excellent typed an . essence of it in. So that's supposed to be in. Ok, so now, we comprehensive our helper function, . let's create a brand new one, that is to delete an encouraging message. So def delete, encouragement. . And iit's going to take an index, as is an argument, the index of the message to delete. . So first, we have to get a list of the messages from the database. So encouragement . equals dB. encouragement. So we received the list of encouragement from the database..
we now have to examine if the size of encouragement is greater than the index, because . the man would move in an index that is, that's in fact no longer in that that record. . the person would pass in an index that is actually not portion of the list. So if the length of . encouragements is more than the index, then we're going to delete DLS. Delete. we'll delete . the encouragements at that index. and eventually, we are going to put it aside into the database again . equals encouragements. And this can correctly be during this if announcement. . okay. performed. We can now upload an encouragement and delete and encouragement. . Now let's update our bot code to use these capabilities. So users and discord can update . encouragements and delete encouragements, correct from Discord. Let's move down here. . So first thing we will do, if you remember, presently, it returns a decision from.
The starter encouragements if it sees a sad notice. Yet we want to also be able to go back a random we . want the random encouragement to also be able to come from the database. So let's add some code to . be capable of use the starter encouragements with any encouragement that has been further to the database. . So first off, I'm going to create a new variable suggestions equals starter encouragements. . Ok, so now, if there are if there's any incursions in the information in the in the database, . if encourage intended in dB keys, . then we can, we will add these to the options. So strategies equals techniques plus DB encouragements. . Ok, so now as opposed to picking out from starter encouragements, we'll 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 is going to be searching for a new, . a new command. So if the message from discord begins with the command, . knew, if it starts with the command new, that capacity it is going to be a brand new message to feature to . the database. So the command that the bot is going to be looking for is the command dollar sign new, . but then in an identical message that you that the consumer places greenback sign new, they're going to put . an area, and then they'll put the new encouraging message. So we have to wreck off . the encouraging message from the the new command due to the fact we don't need to add the new command to . the database, we simply want whatsoever text comes after the greenback 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 equivalent message dot . split, we're going to cut up off the message from New. So we are gonna do dollar signal new. . okay, so we are actually going to get an array of the message that splits at New. And so we're . getting the second one element within the array. And that moment section of the array is going to be . the hot message. Now you will see that we're not just splitting at New, we are splitting up new . area. So there's a area after that, because we don't desire the user is going to place dollar . aspect new space after which the message and we don't desire to add the gap to the database. . We simply favor the message after the database or after New. So we got the hot encouraging . message. And now we simply have got to replace the encouragement with that new encouraging message..
Then we want to send something returned to the user in order that they know that it labored correctly. So wait . channel or message dot channel, dot ship. New and courage couraging message added. Oh, yeah, . that that ought to be and lower back, I don't know why retain doing that. If incursions and DB keys. . okay, now we are going to add the ability for a user to delete a message. So . a brand new operate if message that starts with if it starts with Dell, or delete, that is what Dell . stands for. That begins with Dell, we are going to get the list of messages. encouragements or . we are not going to get the record of messages, we're simply going to really simply 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 begin it all started this with an empty list. And we're going to assess if there is.
Any encouragement already. So if there's any encouragement already, in dB keys, . then we're going to delete one of them. First, we need to get the index from the command. So . So if matches that starts with greenback signal, . delete. So if it starts offevolved with Dell, Dell, that ability delete. Then we'll first create an . empty record. incur, we'll create an eight encouragements variable as an empty list that we . will upload to in a moment, if encouragements, we're checking if the incursions are already in the . database. due to the fact if there is not any inspire us in the database, and we don't need to aim to delete . any of them, so if it's already, whoops, we will see if increments are in dB keys, then let's get the . index from the message that the user submitted via Discord. So we have to get the index,.
Or you should split it from from the right here. So message that split. . And similar to earlier than, we are going to, it will look very similar . Dell. And this time, including the distance there is optionally available. What let me show you what I meant. . So earlier than we upload a space right here, this time, we don't necessarily have to have an area due to the fact we are . going to transform it to an integer. So whether there's a area or now not, it will nonetheless correctly . convert to the integer, not there, we're going to type in a variety of, the user goes to . have a number of after the command del, and it's going to be transformed to an integer in Python. . Now, we simply need to delete the encouragement . that's at that index. And then finally, we need to get a catalogue of each of the encouragements that . get the liver present record after it's been deleted. Because we're going to.
Go back that to the users on the way to see the up-to-date record after iit's been deleted. . And now we'll ship that back a weight message dot channel dot send . encouragements. okay, so the reason why we made an empty list this is due to the fact if there, if there's no . encouragement in the database already, it's just going to come the empty list right here. but when there . is encouragements in the database, it will get those encouragements after which return return these. . Ok, let's experiment this bot. And then we'll upload some a few extra features. . So I'm strolling the bot lower back. . Ok, the bots walking and we can test it. So let's experiment any other functions first. . We still get an inspiring message. Now let's try out to add a brand new message. . You are amazing. . okay, hi, just found out I made an error way up right here. database has no characteristic key..
So that's what's the main with an S at the tip. So let's discontinue discord points bot . Let's run this again. . that is why you want to test frequently to capture those, these blunders like this. . Let's go back over right here. Ok, now let's try typing this lower back. again, new, you are amazing. . New encouraging message added. Let's upload another one new way to move. that won't make sense based . at the message that the man installed to get that message yet that's okay. New, you're the best . coder in the world. okay, now in order to receive get the sort of messages, . we mean we must have a few sad words. So I am sad. hold in there. we have already seen that one. . Let's simply type in unhappy . Back. Way to go. Now, way to cross. that is one . of our new messages. So this become one of the common messages, the starter encouragements,.
Yet now we have received randomly one of our new messages. i'm not sad anymore. . You're a tremendous individual bought. i am depressed. good, I guess you have to spell it right. . Cheer up attempting to see yet another new one. . You are amazing. that is another new one which we positioned in there. . okay, now let's see if we will delete a message. So we should always have the ability to delete a message with . doe. And let's simply delete the first one. So i am going to simply spend 0. . Now it is going to return the messages that are left. Now simply thanks to move and you are the . best coder on earth. It doesn't have a me So that one was deleted. in order that labored. . right this moment the sole manner you will see all the messages which are all the person submitted messages . is to delete one, it would be pleasant to be able to see the user submitted messages with no need to.
Delete one. So let's add some additional functions. Let's go back over to the bot. . So after including the flexibility to get a listing of user submitted messages, . from correct from discord, we will add the flexibility to turn off and on even if the . bot responds to unhappy phrases. So you will be able to actually update that right from discord, . no matter if it is going to be responding or no longer. So we'll kind of do all that aat once. . first off, we will create a new key magnitude pair in the database. . So correct after the starter encouragements, we're going to check if responding is in the database . but. So it is gonna be a brand new key in the database responding. If discord music bot lagging 's not in the database, db keys, . then we'll upload it. And it's going to default it's going to start off . with the aid of being authentic respond, issue equals real. And we do have to positioned a colon here..
And now we in basic terms prefer to respond if it really is actual. So down right here, in which it responds to words. That's . this entire section here is when it's is when it's responding to the sorrowful word. So . i'll press tab right here, due to the fact this is all going to be within an if assertion. If . dB, music bot for discord will get the price of the responding key in the database responding. . So if that is true, then it's going to respond to the sad words. If now not, it won't respond to the sad phrases. . In a minute, we will add the ability to update this value. . So let's go back before we add the flexibility to update that importance, . we're going to upload the power to record encouraging messages. So if message that begins with . me, maybe like guess what the user command is gonna be used, it is gonna be a catalogue. . So if the user, the user models in list into discord, then first off we're going to get.
We're going to make a empty list encouragements equals an empty list because there will not be any . encouragements in the list yet. So we're going to investigate if encouragements is in dB keys with an S. . Then encouragements, we're going to feature them we're going to . set the encouragements in the database to the incursions, variable DB encouragements . after which we simply have to return the encouragements. So Oh, wait that message . or send the message to the channel. That ship and encouragements. okay, now, users should be able . to get a listing of the encouragements. Now we're going to have the characteristic of changing whether . the bot goes to respond to unhappy words or not. So if message that begins with risk . responding, quotation mark responding, . then we're going to get the value. So the idea is that the user is going to place responding real.
Or responding fake, if the discord message that the bot is going to accept as a command. . So if a nano importance, we're gonna get the value that the user typed in, so is it true or false split . we're going to do clone of earlier than responding. And returned, we're going to placed responding space . due to the fact we don't need this space correct after that. So we need the 1st element in that array, . that's the second one the second element of the array and index one. So we got the price. So now . if value dot decrease, even if they typed in lowercase or uppercase, we're going to . convert it to lowercase. So if it equals genuine, then we're going to ensure that DB responding . equals real. And then let me scroll down just a little right here. we will send . the message message again to Discord. Oh, wait. Message dot channel dot ship. Responding is on..
Ok, we are gonna have an else, I'm just gonna replica this complete factor. So if valued at decrease does . now not equal authentic. i am going to just difference this to else. we're correctly making it so the person doesn't . even have to kind in fake anything yet genuine would be assumed to be fake. So . encouraging, responding is going to be set to false. And iit's going to say responding is off. . The code for the bot is total. This is all we are adding to the bot. . we're not fairly achieved, though. We're no longer relatively completed. we will test it. . And then I'm going to tell you approximately one more very important . step. So let's do this. Let's go to again to our bot. Oh, we need to run the bot back. . Ok, the bot is strolling. whats up, bot. I'm sad. Ok, it further that . now let's get a listing of our messages. Remember despatched is stored in the database..
There should still be messages. Yep. See, these are the messages messages we further before . the last time we ran our bot. but since it stored in the database, they get saved dependent . between instances that you've got run your your code. So let's add one. Oh, placed add. nice. I'm simply nice. . Oh, I did that incorrect. It's aactually supposed to be new. Ensure you type within the instructions, right. . And let's record them back. exhibit is nice. Now let's delete whatever delete . index one that ought to be the middle one. And Yep, it indicates them that we may even list them again. . Now let's examine if we will change turn off and on whether the body's responding. Let's confirm . that the bot is responding. i am sad. Pleasant, huh? Perhaps it's asserting nice since the data mentioned it's . tough to tell with this bot. Yet let's have a look at if we can turn off the messages. So responding false..
If I sort in unhappy , and it does not say whatever, if I sort in depressed, it doesn't say whatever. . But if I do responding, authentic. okay, let's do say i am depressed. . okay, the bot thinks that is especially pleasant. Now, due to the fact that we created the responding variable in the . database, it's going to shop weather between instances even if you close down your bot and rerun it, it will . still store no matter if it's alleged to be responding or not. okay, so technically, our bot is done, . but you don't need to discontinue now. due to the fact if I close my tab, the bot will simply stop strolling. And then . it is not gonna work. If I close my browser, the bots gonna stop going for walks, it's not gonna work. . There's a workaround repple it is going to continue strolling an internet server even after the tab is closed. . Now this is not a web server. but we can create one to run it whilst this..
Yet even a web server will simply run for up to an hour without any use. Oh, . here's the rep doctors concerning the information superhighway servers. See it says as soon as deployed, the server will continue . to run within the background even after you shut the browser tab. The server will stay awake and active . till an hour after its final request after which it's going to input a drowsing level. . So even in case you have a web server internet server walking, it is going to finally discontinue jogging after an . hour after its final request. So if nobody's using the disk or bottles have stopped operating. . notwithstanding, there's a way to make the bot constantly get requests so it in no way shuts down. . So in fact rip insurgent has announced that in the future they are going to provide a paid plan that . enables code run continuously, no matter what it will simply by no means enter the snoozing sage..
Yet they do not offer that relatively yet. It's coming very soon, maybe it will be available . by the point you spot this video. but till that is implemented, there is aanother way to keep . your bot jogging longer than an hour. And this technique even works with the loose tier of replicas. . So to maintain this bot walking constantly, we will use another loose carrier called . uptime robotic, up and rollback can be set to ping the bots cyber web server on duplicate each five minutes . with constant pings, the bot won't ever input the slumbering degree, and will simply keep running. So we . have two extra things to do to ensure our bot is working as we want to ensure it's working . jogging constantly. we will create a web server and repple it and set up uptime robotic to . continuously ping the internet server. Ok, creating the internet server is simpler than you may think. To.
Do it, we're going to create a brand new dossier, so visit documents and click on the Upload Dossier button. . And it is just going to be referred to as maintain alive.pi. since the complete goal of the internet server . is to keep preserve everything alive. okay, i am just gonna paste within the code from the internet server here. . It's exceptionally simple. Like I reported, and iit's not, it's not likely specially significant for the . bot, it is just aa thing that we do in replique, to make it hold going. So I will have a hyperlink to the . textual content of this internet server right in the description here. So you can simply replica and paste it additionally . . but you will see it is not that long. So in this code, we're going to import flask, . and we will use flask as the web server. And iit's going to go back hey, 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, if you want to both be walking simultaneously. . Yet the rest of this is absolutely not relevant to our bot, we just want the bot to run this web server. . So let's return into main.pi. And then at the top, we're going to must import it. So from . hold alive, import, preserve alive. And if you just go back over here, that that it's uploading this, . that is it's what runs our web server right here. So from it's going to import the internet server, and then . we will cross all the way to the bottom. And right before the client runs, we will call . maintain alive. And this would run our information superhighway server. So if I stopped the bot and rerun the bot, . it's anything distinct goes to happen due to bot due to the information superhighway server. . Ok, you'll find a new window open correct up right here..
And it truly is our internet server going for walks and see how it says hey, I'm alive. That's from . our cyber web server code in which it says go back Hello, i am alive. So everytime you go to the server, . it says whats up, i am alive. Now the important factor right here is this URL. So this URL became created. . And that's the URL that we'll ping from uptime robotic. So i'll reproduction this URL right . right here. And then we will use it in the next area. So now we have to set up uptime robot. . So step one is to create a unfastened account, after which get logged in. . Ok, once you're logged into uptime robot, simply click add new track for the track kind, . https and the pleasant name, you can name this whatever we are gonna simply put, . bought. And then for the URL, it truly is where you paste within the URL that you copied all.
Over from repel repple it and the monitoring period we'll simply depart thiis the same as . each 5 minutes. And then create, monitor after which create track again . monitor created so I'm able to near this. And we should be able to see it here. . right here it's. that's now going to ping our bot every five minutes. So at this factor, . it's going to be continuing, continuing retain to run even when we close our tab . and even after an hour. no matter if there's no activity in discord, this bot will simply run . continuously. Let's just try out last the tab really fast. i am simply gonna copy this . URL, near a tab. let's have a look at if we can just open the tab back up on here . and you will discover it's nonetheless running nonetheless running during this here. . It may mean it will say something like this whenever it's pinged..
I don't know if that's from the ping or now not, or if it's simply from reloading. . Yet we are actually accomplished. Now the bot will run continuously, so persons can always interact with . it on replicat. it is time for an advantage part, you already know how to set up a Python bot and run . it continuously from repple it. Now I'm going to allow you to mounted another discord bot . to work with web hooks. we are going to connect the bot to GitHub in order that the bot will submit a . message in your server each time a certain GitHub repository is up-to-date. And for this GitHub bot, . you do not even ought to code anything, or hosted anywhere. let me show you how it really works. okay, . to add this GitHub bot, we will go to our server. And then we'll go to the server settings right here. . after which we are going to click integrations. And then there's cyber web hooks create cyber web hook..
And iinstead of calling discord fast food bot , I'm going to name it. I will name it GitHub. Ok, . and we will placed it in the general channel. And then i'll simply reproduction the webhook URL. . okay, i am going to be sure to save. . Ok, now i am gonna cross over to the GitHub repo that i want to hook up with this bot. . So i'll use the loose code camp.org GitHub repo. after which i am going to visit Settings. . And then at the facet right here, it says internet hooks. okay, now I'm going to click add information superhighway hook. . after which I'm just going to stick within the URL that I copied from Discord. Now here's aan important side, . you can't just use the URL, you also have to upload anything, i am gonna upload reduce GitHub at . the top of the URL. And then for the content sort, I will change it to software diminish JSON, . and they're going to scroll down. And for what event should set off the internet hook,.
I'm gonna do send me every thing. depending on what you are trying to do, you might want to do just the push event or select person events. Yet I'm gonna do everything. So i'll add the web hook. And now I'm able to return correct over to my discord channel. If I wait, we should always begin to see a few routine happening at the from the unfastened Code Camp GitHub repo. let me cross and simply try to make an event take place. i'll just unstarred big name the repo. And it worked. We simply bought a message from the GitHub bot, new big name further. So now we've connected our bot to GitHub via internet hooks. And the GitHub bot will submit a message anytime whatever happens at the GitHub repo. you could create bots to work with a lot of other outside prospective buyers with cyber web hooks. Ok, you've reached the end of this educational. Now you've prepared to exit and code your personal discord bot.