Code a Discord Bot with Python - Host for free of charge within the Cloud
And I'll say for me and my friends, and I'll just name it robotic family server. Ok, let's create . that. And we have our server. Ok, so before we will code our bot using Python, and using the . discord API, we need to first create a discord bot account right within Discord. So you're going to . have to go to the discord developer portal. I'll placed the hyperlink in the outline, but it's just . discord comm cut back developers scale down purposes. So once you're on the applications web page, you're . going to click on this new functions button. And then we will create a name for it. This bot . goes to ship pleasant encouragements to people from in Discord. So i'll call it encourage . bot. Ok, i am going to create that. okay, now I'm going to visit the bot tab here. after which I will.
Click add bot and say convinced, do it. okay, we are going to keep the default the default settings as a public bot, . and then we won't require the OAuth to grant the code furnish. So now the bot has been created. . the next move is to repeat the bot token. okay, so i'll simply click replica to copy the token, . we will desire that later. Now this token is your bots password. So ensure not to . percentage it with anybody. It may enable somebody to log into your bot and do all sorts of bad matters. . And iit could even permit someone to debris with your server. and likewise, you may definitely regenerate . the token if they by accident get shared. So we have to invite the bot to our server, . we have the bot user, and we have to get into our server. to try this, . we'll have to create an invitation URL for it. So let's visit the off to tab here..
And now we're simply gonna visit the scope part. on the grounds that this is a bot, we will just click on purchase here. . we have now to scroll down a little bit more and visit the bot permissions area. So we'll choose . the permissions we want for the bot. Our bot is going to be chiefly used for text messages. . So we don't want numerous permissions. So your bot may do various things. but let's just . select these kind of right here. In fact, we'll do them all on this line, apart from ship TTS . messages. Now you do must be cautious with this administrator permissions. If you give . your bot administrative permissions it has, it will have the ability to control your server in ways that . you'll not in fact want. And then I guess the final one we need to add is the view channels or . bots going to ought to view the channels. So after choosing the correct permissions, we are going to click on.
The copy button here. And this would copy a URL that can be used to add the bot to a server. . And II guess we are going to also have to recopy the again key, we copy the back key previous, . yet now it has been re copied with this URL. So we'll have again later to get the again key. . but now let's just open up a brand new tab and I'm going to visit this URL Ok, so now we have . to select the server, we want to upload the bot to I will select robotic household server, the only option, . but you will probably have greater than more strategies. So we will click on hold. And we want to approve . that we wish to do all these things, we want to authorize the bot for all these items. . And II'm not a robot and the bot is permitted. . which means the bot has joined the server. So if we really cross over into our server, we will see.
encouraged bot just slid into the server. But also you see over right here that the bot is offline, it's . unlikely to be online until we write our Python code that creates the bot and run the Python code. . And talking of Python code, it is time to start that. Now I pronounced earlier that you don't have . to install anything, you do not have to use any program except your information superhighway browser, we are going . to be growing the complete 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 can just use in your cyber web browser. . So it makes it a lot easier to code matters if you do not have to put in any different software . on your machine. after which you may log into it from diverse browsers, distinct computers, . after which you may easily access your code and your programs whatever machine you are using..
So we are gonna make our button this and then it will also be totally hosted within the cloud, . we may even run the bot on this web site, so we do not have to run it on our nearby computer. . And which will make it in an effort to preserve the bot going for walks, no matter if our desktop shuts down, or we're . gonna where we close the browser tab or anything like that, i am going to allow you to make the bot. So . it'll keep walking in the historical past continuously. And then you do not have to fret about it. . If you do not already have account, you're going to sign up. Yet i'll log in here. . okay, now that i am logged in, and I click this new repple in the corner right here, . now we just ought to create, select the language that we're going to use for this new repple. The . cool aspect about repple it's that you'll use a group of alternative languages with it. And so.
That makes it easy. If you're going to run some of these languages on your laptop, you probably . are going to have to put in a few extra software or extraa things on your machine with a view to run . these languages. but when you just visit repple, it, you could do exactly it correct on your browser. And you . haven't got to put in whatever. Yet we're going to be doing this in Python. So i'll click on Python. . and it will have a default name right here. But i am just going to be calling it recommended. . okay, and I'll just create the repple. . So let's pass over the most portion of Rep. What we have right here, that's right here is the code editor. . And you will see a default with a file open referred to as main.py. And we can variety our Python . code right in here. after which right here over here are the documents, and you'll create new documents,.
We will be creating a few new documents within the course of making this bot, you can also create new . folders. after which over this is the console. The console is wherein the conventional output will appear. . and you will be prompted to go into enter if you're going to have input on your code. . And in Python, it will it is going to double as an interactive prompt. So you could type . in Python code here. And it'll evaluate the code interactively. So that's relatively cool. And . then over here, we have some things at the side here. so documents, version manage, if you're going . to associate with git, and or we are not going to try this during this tutorial , although. And then if . you're going to set up packages, but there's also in a different way to put in patches that we will see. . And then the main issue we are correctly going to be utilizing is the replique database. So some thing.
Cool about replique is it encompasses a database as portion of it. And so you can easily store . things which will keep saved in there no matter if you discontinue your application and run it again the everything . store in the database will will nonetheless be in there. So we will be utilizing that later during this tutorial . . after which you can also difference some settings, like the subject matter of font size, all sorts . of stuff like that. but let's return to the documents because we'll start creating . our discord bot at the moment. So i discussed that we are going to be using the discord.py library. And . the very first thing we'll 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 instantly set up this dependency when . you press the Run button. So I suggest my permanent and do a great deal immediately but when I click on Run,.
We will see on the aspect here, that it's Installing the discord despit dependency, and then it will . be capable to use it in this system right away. So there's nothing extra you have to do to install . dependencies besides simply uploading them right on your software. So allow me communicate a little bit . approximately discord.pi. Before we maintain with this bot right here, it revolves around the thought of movements, . and event, it is whatever you listen to after which reply to. as an instance, while a . message occurs in discord, you'll receive an event about it that you can reply to it. . So let's make a bot that replies to a specific message, we'll start . with a very easy bot, and then we're going to add to it and just add extra features as we go. . So the simple bot that we're going to start with the code is really taken directly from.
The discord.pi documentation. it is a really good example of just like the most elementary bot you may make. . and like I said, then we are going to preserve to build on that once we get this straightforward bot created. Ok, . so we received the discord library, imported. So let's add a few more strains right here. So now we are . going to create an instance of a client. This is the relationship to Discord. So do a client . equals discord dot client. it truly is simply part of the discord.py library. Now, we're going . to use the customer that event decorator to sign in an event that looks like this. . So I remember i discussed that this client uses pursuits to make it paintings. So it truly is how you . sign in an event. And this is an asynchronous library discord, that pie. So matters are done . with callbacks. a callback is a function that is called while something else happens..
So during this code, where you are going to use the unready occasion, so I'm going to positioned async. . def on ready. So this event is going to be called whilst the bot is ready to begin being used. And . when it's begin, when it's prepared, whilst the bot is prepared, we'll try this print. And this is . going to print correct to this console here. So whilst the bot is administered and able to be used, . it will print this to the console, it's going to assert we've logged in as and then we're . going to actually be able to get the username that's logged in as, and it really is the way you do that. . So there is a few how you can do it. Yet so it truly is taking the customer, . this zero is being changed with the customer. So it's doing the client that consumer So that's this . client, this zero receives replaced with this client here as client, that person to get the username..
And to make sure i've thiis right next to the quote goes here. And there we go. . okay, let's create another occasion. So that's the event that happens once the bot is able, . and it's working. So the next event is 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 check in the event. . And we're going to do a different async operate. Yet this one is on message. . So those operate names are specifically from the discord.pi library, it's looking . for these operate names, to know what to do, while certain matters take place. . This onmessage occasion triggers every time a message is acquired. Yet we don't need to do whatever if . the message is from ourselves, that's the bot. If the message is from the bot, we'd like.
To do not anything. And this event takes an argument referred to as message. Or you can call it whatever you . want. But it's classic to name a message because it's the message that is being acquired. . So this event triggers every time a message is received. but we don't want it to do whatever. . If the message is from ourselves, we are gonna make it check if the message is from ourselves. So if . message dot writer equals client, that user seems like I spelled anything wrong there. . So if the author of the message is the user, our bot person, all we're going to do is go back. . Next issue we're going to do is we are going to see if the message starts with a command . that's being sent to our discord bot. And we can make up those instructions to be whatever we need. . So thus, i am going to show you we're going to do if message dot content material that starts begins.
With and we are just going to make it as much as be dollar sign hiya. For our bot. We're going to say . all of the commands are messages that start with the dollar sign. So if the message starts offevolved With dollar . sign good day, then the boss ought to understand that it is acquired a command so it should return . whatever. And to come a message again to discord, we'll await . message dot channel dot ship. So this goes to ship a message to the channel. And the message is . going to be hi there. okay, so now so every time the bot notices that someone says greenback sign Hello . within discord, it is going to respond whats up with an exclamation factor. So it is every thing we . desire for the bot. but we still have to run the bot. So here's the line to run the bot client . dot run. after which right in the customer that run we have to positioned our token, we need to put our bots.
Token that is just like the password for the bot. earlier than we simply go and copy the token lower back and . put it right in here, there is something I should inform you about duplicate you want to be careful . with. by using default, all replicates are public, they're noticeable to anyone. So anyone can just . entry your reproduction . And they may potentially see whatever you install your replica, including . any secrets or keys. And remember, I told you that thiis key is like a password, you don't want . anyone else to learn about it. but there is a way to keep things secret. and that is to apply an . environment variable. So in repple, it permit me show you the way you need to use environment variables, we are . going to firstly, create a brand new dossier here. And the hot file is going to be referred to as dot E and V. . in order that anything we put in a dot E and V file will now not be made public, nobody will be capable to see.
anything in a dot E and V dossier in replique, pretty a lot on a public account on a unfastened tier account. . every little thing is public always apart from just this one dossier 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 return and . get our token. So let's return to our development portal page, the discord comm slash developers, . after which i am gonna visit bot gonna copy the token. And then we can go back over here. Ok, . there's my token. i will disable this bot later and change the token. So . don't hassle trying to determine it out to apply it for your own code. Yet now that we've got our token, . we have to make it so it receives into our main dossier. So to do that, we will have to . do have another import announcement right here. So I'm going to put import OS, and then down right here,.
We need to entry that token from the dot EMB dossier. So what we're going to put in our code here . is oh s dot get e and V. after which I'm going to put token. Now the reason why it is token here . is because that's the call I positioned here, you can actually use any call for the token doesn't . should be token, you simply have to make sure that if you are getting our environment variable, . you employ an identical call right here. So now when you run the code, it's just going to replace . this right here together with your token. Now, if you're doing this regionally, or you are doing in such a . way wherein you recognize, no person else is ever gonna see this code, you'll be able to put your token right . in right here. And you don't even have to import o 's right here. Yet for the reason that we are doing it in a public way, . we'd like to make sure that no one else sees our token, otherwise they may get entry to our bot..
And that's actually it for our easy bot. Like I stated, we will add some more . additional functions later. but let's run it now and test this out. So click on run right here. okay, appear, . we've logged in as encouraged bot. Now let's go to our server. you can see look earlier than it showed . as logged out, yet now it suggests it as logged in. So permit me let's do that. i am going to say hiya. . Now you will discover that is from from me, and it didn't reply. due to the fact keep in mind, the one of a kind command is . actually dollar signal Hello. So let's try out that. And iit works encouraged by means of just spoke back with . whats up. So our bot works if we do yet another command like Hi, how are you or not not yet another command . another message? it is unlikely to respond at all. but when we do the dollar signal good day. Then it will . respond. So we just created a common discord bot that responds to the command greenback signal hiya..
and clearly you may make it any command. So if you do not need to respond to dollar signal hey, . possible use any command right here. And then you could change what it is going to respond with. . Now that we have our basic bot operating, we will enhance it. . I reported it is known as recommended bot for a reason, the bot will respond with a message . of encouragement. every time somebody sends a message containing a sad or miserable note, . we are gonna make it so anyone will be capable of add encouraging messages for the bot to apply. And these . messages will be stored in a database. So they will store even when you stop and begin your bot. . we're also going to add another thing in the database. that activates even if that turns . on or off whether the bot is is responding to messages. And one other characteristic of encouragement.
We're going to do is that the bot goes to go back a random inspirational quote from an API, . when somebody models the message, greenback sign inspire into the chat. So let's start with . that very last thing I said, we'll start with the aid of including the dollar sign spider feature. . I found this API known as Zim quotes.io. That will return inspirational prices. Now, you need to use any . API. So if you don't want to use in rates.io, you could simply find aa different one. If somehow . this isn't operating, just attempt to locate a different API, but we're going to use is in rates.io. And . a majority of these concepts will observe to really any API that you are utilizing. So let's go back to . our bot. And we will import a few new matters to work with this API. So what we're . going to do is we will import a pair more Python modules, we will upload 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'll have got to import . the JSON module. What because that is going to make it simpler to paintings with the info that is again. . Ok, now we're going to upload a helper operate. It's called get, . quote, this can be a function 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 go like this, . the response we'll shop the response from the API in a variable referred to as response. . And so we need 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 means of gazing the documentation of this in charges API. So HTTP, . S, colon cut back scale down is in quotes.io. scale down API slash random, it is going to go back a random quote. . okay, now we will convert that response into JSON. So we're going to do JSON, data . equals JSON dot loads, reaction, that text. So this response that textual content, I just had to appear in . the documentation of the API, and found out that the response is going to have . this response that text that I'm able to, i will load into JSON. So we've to do something with . this JSON, we need to get the quote out of it. Now, this become a number of trial and error, I had to . print out the JSON information and try to figure out how how to get the precise portion of that JSON . that I wished. So so that's 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 fees API, the outcome that's returned from this API goes to have a key referred to as q . whilst the value is going to be the quote. So we'll begin with the quote, . but then we wish to add after the quote, the author of the quote who mentioned 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 iit's going to be a touch, the sprint is going . to be correct earlier than the person's name. it is just a way to sort of show who's who is doing the quote. . So we'll do yet another plus right here. And let's go back into the JSON information. And this time, rather than the . queue, we're going to do a for author. okay, so we received a quote in here. Now, i can return the quote..
Ok, we've to replace 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 somewhat right here. So now I'm just . going to replace these strains right here. This become just an example to rich to answer the message hiya. . Yet that's not what our final bot goes to do. So i'll change this to motivate. . And let's circulate this over a little bit. with the intention to see more . of the code. So if the bot gets this message motivate, we'll ship a quote. So . the style we will send a quote is first adding one other line. So I'm gonna positioned quote, . equals get, quote. So it's going to call the get, quote, operate and return the quote here. . And now i will just go back the quote, or I can send the quote to Discord. Ok, at this factor, we.
Can run the code and try it out. If it's already strolling, we bought to prevent it, after which run it returned. . after which we need to wait till it says it's logged as endorsed bot. that means we will go back to . our Discord server, and say, Hi, bot would not say whatever. let's examine if it is going to say, hiya, whats up. . okay, that command doesn't paintings anymore. due to the fact we alter it. Let's do encourage. Ok, . we've an inspirational quote. And let's try inspire some more times to see if it is random. . There's another one. There's another one. See, we're getting a random quote, . for each time we do it. So this is the quote part by failing to arrange you're making ready to fail. . And then we add that the gap after which the sprint, after which the author Benjamin Franklin. . okay, good, that works. Let's pass, go back to our bytecode. Now we'll implement the feature.
in which the bot responds with encouraging messages when a person posts a message with a tragic word. . So in this, this time, the user isn't putting a certain command into discord to get a outcome. This . time, the bot is going to be checking every word in each message to see if there's a sad notice. And . if it's if it notices a sad be aware, it's going to go back an encouraging message. So the first thing . we'll do is to create a Python list that contains the unhappy phrases that the bot will respond . to. So correct after the customer variable is created, I'm going to create yet another one known as unhappy . . Words. And the CI notice. it truly is just going to be a listing like I reported, oops, there we pass. depressed, . unhappy . Now I'm gonna have a catalogue of words here, you could add additional phrases. . surely, you can placed any phrases you like on this list here. i am going to have a miserable. And.
Let's do one other. How approximately depressing? Ok, so we will make it so anytime the bot sees . this kind of words within the message, it's going to respond. So what's the bot going to respond with? . Well, now we'll upload a list of encouraging messages that the spot will respond with when it . sees a type of phrases. So i'll make a new variable called starter and encouragements. . Now, i'll inform you why this is called starter encouragements. . it is because those are the there is going to be just some encouragements that the . bat begins with, but users will be able to upload more encouragement later . that are saved in the database. So the start encouragements I'm just gonna do a cheer up. . grasp in there. . And you are a wonderful individual, or even it's just aanother bot. So we will do individual diminish bot.
be happy to add any number of encourages you like, and keep in mind later, user will be able . to feature extra encouragements. Now we need to update our bot to apply the two lists that we just created. . So the first thing we'll do is import the random module, because the bot would 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'll update the on message response right here, we will add . whatever on during this on message async function, that is gonna verify all the messages to determine if . they incorporate a word from the sorrowful words list. And then if the sad note is found, the bot will send . a random message of encouragement. So this is the code for that. after we determine to see if it has . starts offevolved with encourage, then we will check simply if it has any other word from the sorrowful note.
record. So it is how we are gonna do if any, if any weight, if any note in message for word and sad . words. So thiis just ability it's going to go through each note, and the sorrowful words. And gonna see if . any of these words are in the message. If it sees any of the word in the unhappy phrases within the message, . then it is going to Oh, wait. Message dot channel dot ship. This is just like this line above here. . And what is it going to ship, it's going to send random dot selection, it is going to select a random . choice from starter encouragements from that list that we created. okay, we can . test the button Now, let's have a look at if there's something wrong right here. undefined name message. . Ok, acquired it. I got previous to myself right here. actually, this message must be, . it's aactually speculated to be message content by using swap to message, due to the fact on account that we are.
Going to be utilizing message content material a bunch of instances, what I want to do in fact, is . create the message variable here message equals message content. after which anytime we . see message content material, we can change it with message right here. okay, let's experiment the bot now. . Ok, let's go back over here. Let's see if the encourage nonetheless works. . labored. Now let's verify the sad note. i am unhappy . . You're a extraordinary person wager Brett person's life spot. good, now, i am depressed. . Hang in there. Oh, that is making me that is making me believe far better. . Thank you. This encouraging through is extremely encouraging. So it is working, it can now respond, anytime it . sees sees a stad technique, it will try to inspire the person who put that unhappy message. . So at this point, you basically comprehend sufficient to create your individual bot..
Yet let's hold going due to the fact we're going to upload some extra stepped forward capabilities that I already . discussed. Now let's give the opportunity to update the bot correct from Discord. A user must be able . to feature more encouraging messages for the bot to use when it detects a tragic word. Like I mentioned . earlier than, we're going to use repple. It's built in database to shop consumer submitted messages. . This database is a key significance store that is built into every rebel. 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 simply show you the commands the most common instructions for the database. . And you don't actually need to get in from here if you know him because one of these men, . yet let's visit the end of the code and we're going to Alka simply insert this so I inserted from.
Replique import dB. this permits us to apply the reflect database. Before we add new commands . for the bot. Let's add helper features that the 1st one will be capable to upload custom messages . to the database. And the second helper function will be used to delete messages from the database. . So let's go lower than the get quote function. . This new operate goes to be called replace encouragements. . So that's gonna it is going to update the encouragements in the database. . And iit's going to be given an encouraging message as an issue. . So first, it will check if encouragement is a key in the database. So if encouragements . do this, correct, 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.
these all of the key all of the incursions are already in the database. encouragements . equals how to get a value from the database that saved lower than a certain . secret is like this DB encouragements. Ok, so we've gotten the encouragement. . Now we simply have got to add the new encouragement to this list. So encouragements dot append that's . going to feature anything new to this list. And we're going to kind in the encouraging message, which is . the argument that was handed into this operate. And then when we append the hot encouraging . message to the ancient encouraging messages, we have to save it again into the database. So DB . encouragements equals . encouragements. Else, if there's not already endorsed us in the database, . then we're going to have to create it. So dB, encouragements.
Equals, and we'll put a bracket here because this goes to be a catalogue, . yet it is only going to have one message. And the one message is the encouraging message . that become exceeded into this operate. Ok, so now, let's have a look at what we did incorrect up right here. . okay, that is presupposed to be if encouragements in dB key no longer is I just perfect typed an . essence of it in. So that's alleged to be in. Ok, so now, we comprehensive our helper function, . let's create a 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 got the record of encouragement from the database..
we now have to envision if the length of encouragement is greater than the index, because . the person would move in an index that's, that's in fact not in that that record. . The man could circulate in an index that is actually now not portion of the record. 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 eventually, we'll reserve it into the database again . equals encouragements. And this can in fact be in this if assertion. . okay. carried out. we are able to now add an encouragement and delete and encouragement. . Now let's replace our bot code to apply these functions. So clients and discord can update . encouragements and delete encouragements, right from Discord. Let's move down here. . So the very first thing we're going to do, if you remember, presently, it returns a call from.
The starter encouragements if it sees a sad observe. Yet we want to additionally be able to return a random we . desire the random encouragement to also be able to come from the database. So let's add a few 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 ideas equals starter encouragements. . okay, so now, if there are if there's any incursions in the info in the in the database, . if encourage meant in dB keys, . then we will, we are able to add these to the options. So ideas equals suggestions plus DB encouragements. . Ok, so now instead of settling on from starter encouragements, we're going to pick from options . of encouragement to come. Now we're going to add a few code to add a new.
User submitted message to the database. So it's going to be looking for a new, . a brand new command. So if the message from discord begins with the command, . knew, if it begins with the command new, that capacity it will be a new message to add to . the database. So the command that the bot is going to be seeking is the command dollar sign new, . but then in a similar message that you that the consumer puts dollar signal new, they are going to put . a space, after which they are going to positioned the new encouraging message. So we have to ruin off . the encouraging message from the the recent command because we don't need to add the new command to . the database, we just desire whatever textual content comes after the dollar signal 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 goes is going to equal message dot . split, we'll cut up off the message from New. So we are gonna do greenback signal new. . okay, so we at the moment are going to get an array of the message that splits at New. And so we're . getting the second factor within the array. And that second component to the array is going to be . the new message. Now you will see that 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 need the user is going to place dollar . facet new area and then the message and we don't need to add the space to the database. . We just want the message after the database or after New. So we acquired the hot encouraging . message. And now we just have to replace the encouragement with that new encouraging message..
Then we want to ship something again to the user so they recognize that it worked correctly. So wait . channel or message dot channel, dot send. New and braveness couraging message further. Oh, yeah, . that that ought to be and returned, I don't know why maintain doing that. If incursions and DB keys. . okay, now we'll add the ability for a user to delete a message. So . a brand new function if message that starts offevolved with if it starts offevolved with Dell, or delete, that's what Dell . stands for. That begins with Dell, we are going to get the list of messages. encouragements or . we are unlikely to get the record of messages, we're just going to actually simply create an empty list . referred to as encouragements, because we're going to want to come the recent list of encouragements later. . So we will start it started this with an empty list. And we'll determine if there is.
Any encouragement already. So if there's any encouragement already, in dB keys, . then we will delete one of them. First, we need to get the index from the command. So . So if fits that begins with dollar sign, . delete. So if it starts offevolved with Dell, Dell, that means delete. Then we will first create an . empty list. incur, we'll create an eight encouragements variable as an empty record that we . will upload to in a second, if encouragements, we're checking if the incursions are already in the . database. due to the fact if there is not any encourage us in the database, and we don't need to attempt 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 need to get the index,.
Or you need to split it from from the right here. So message that break up. . And clone of before, we're going to, it's going to seem very similar . Dell. And this time, including the gap there is optionally available. What permit me show you what I meant. . So before we upload an area right here, this time, we don't always should have a space because we are . going to transform it to an integer. So whether there is a space or no longer, it's going to still correctly . convert to the integer, whatever not there, we're going to type in a number 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 ought to delete the encouragement . that's at that index. after which finally, we need to get a catalogue of each of the encouragements that . get the liver current record after it's been deleted. due to the fact we will.
Go back that to the users for you to see the up-to-date record after iit's been deleted. . And now we are going to ship that back a weight message dot channel dot send . encouragements. okay, so the explanation why we made an empty record this is because if there, if there's no . encouragement in the database already, it's just going to come back the empty list here. but when there . is encouragements in the database, it will get these encouragements after which return return those. . okay, let's test this bot. And then we are going to upload some a few additional functions. . So i am jogging the bot returned. . Ok, the bots walking and we are able to test it. So let's experiment any other capabilities first. . We nonetheless get an inspiring message. Now let's try out to feature a new message. . discord gambling bot are amazing. . okay, hi, just realized I made an error manner up right here. database has no attribute key..
So that's what's the main with an S at the tip. So let's stop this back. Let's run this returned. . that is why it's essential experiment frequently to capture these, those mistakes like this. . Let's return over here. Ok, now let's try typing this returned. Back, new, you are amazing. . New encouraging message further. Let's add another one new way to pass. That isn't make experience based . on the message that the man put in to get that message but that's ok. New, you're the best . coder in the world. okay, now in order to receive get one of these messages, . we imply we must have some unhappy words. So I am sad. cling in there. We've already obvious that one. . Let's just kind in sad. again. Way to go. Now, way to cross. that is one . of our new messages. So this was one of the original messages, the starter encouragements,.
but now we've obtained randomly one of our new messages. I am not sad anymore. . you are a first rate person purchased. i am depressed. Well, i suppose you should spell it right. . Cheer up trying to see one other new one. . You are amazing. that is another new one that we positioned in there. . Ok, now let's see if we will delete a message. So we should be capable of delete a message with . doe. And let's simply delete the first one. So i am going to simply spend 0. . Now it's going to go back the messages that are left. Now just way to pass and you are the . finest coder on the earth. It does not have a me in order that one become deleted. in order that labored. . right this moment the only manner you will find all the messages which are all the person submitted messages . is to delete one, it might be pleasant to be able to see the user submitted messages while not having to.
Delete one. So let's add a few additional features. Let's go back over to the bot. . So after including the power to get a list of user submitted messages, . from correct from discord, we'll add the flexibility to show off and on no matter if the . bot responds to unhappy words. So you will be able to truly replace that right from discord, . no matter if it is going to be responding or no longer. So we'll type of do all that aat once. . firstly, we are going to create a brand new key importance pair in the database. . So right after the starter encouragements, we're going to envision if responding is in the database . yet. So it really is gonna be a new key in the database responding. If it isn't in the database, db keys, . then we'll upload it. And it's going to default it will start off . by means of being actual respond, component equals true. And we do have to put a colon right here..
And now we basically desire to respond if it's true. So down right here, wherein it responds to words. That's . this whole section right here is when it's is when it's responding to the sorrowful observe. So . I'm going to press tab right here, because this is all going to be within an if declaration. If . dB, we're going to get the value of the responding key in the database responding. . So if that is real, then it will respond to the sad words. If not, it won't respond to the sad words. . In a minute, we will upload the ability to update this importance. . So let's return earlier than we add the power to update that value, . we'll upload the power to record encouraging messages. So if message that begins with . me, perhaps like guess what the user command is gonna be used, it is gonna be a listing. . So if the user, the user models in record into discord, then first of all we will get.
we'll make a empty record encouragements equals an empty list because there will not be any . encouragements in the list yet. So we're going to examine 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 . and then we simply have to come the encouragements. So Oh, wait that message . or ship the message to the channel. That send and encouragements. Ok, now, clients ought to be able . to get a catalogue of the encouragements. Now we're going to have the characteristic of changing whether . the bot is going to respond to unhappy words or not. So if message that starts offevolved with risk . responding, quotation mark responding, . then we're going to get the price. So the idea is that the user goes to place responding genuine.
Or responding fake, if the discord message that the bot goes to accept as a command. . So if a nano value, we are gonna get the value that the user typed in, so is it genuine or fake split . we're going to do similar to before responding. And returned, we will positioned responding space . because we don't need this space right after that. So we need the 1st factor in that array, . that is the second one the second one part of the array and index one. So we received the price. So now . if significance dot lower, even 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 actual. after which permit me scroll down a bit here. we'll send . the message message back to Discord. Oh, wait. Message dot channel dot send. Responding is on..
okay, we are gonna have an else, I'm simply gonna copy this whole thing. So if valued at decrease does . not equivalent actual. i'll simply difference this to else. We are in fact making it so the person doesn't . actually have to sort in fake anything yet actual will be assumed to be fake. So . encouraging, responding goes to be set to fake. And iit's going to say responding is off. . The code for the bot is total. This is all we're adding to the bot. . We are not relatively executed, although. We're not fairly completed. we'll experiment it. . And then i will tell you about an extra very important . step. So let's do that. Let's visit again to our bot. Oh, we have to run the bot again. . Ok, the bot is running. hiya, bot. i am sad. Ok, it further that . now let's get a list of our messages. keep in mind despatched is saved in the database..
There may still be messages. Yep. See, those are the messages messages we added before . the last time we ran our bot. Yet since it stored in the database, they get saved dependent . among instances that you have run your your code. So let's upload one. Oh, placed upload. nice. I'm just pleasant. . Oh, I did that wrong. It's aactually supposed to be new. Ensure you type within the commands, right. . And let's list them lower back. Show is pleasant. Now let's delete anything delete . index one that ought to be the middle one. And Yep, it suggests them that we may even record them returned. . Now let's have a look at if we will change flip off and on even if the body's responding. Let's confirm . that the bot is responding. i am sad. Pleasant, huh? maybe it's saying pleasant since the information pronounced it's . tough to tell with this bot. but let's see if we can flip off the messages. So responding fake..
If I type in sad, and it does not say anything, if I type in depressed, it would not say whatever. . But if I do responding, real. okay, let's do say I'm depressed. . Ok, the bot thinks that's rather nice. Now, for the reason that we created the responding variable in the . database, it will shop weather between instances even if you shut down your bot and rerun it, it will . still save even if it's presupposed to be responding or now not. okay, so technically, our bot is completed, . but you don't need to stop now. due to the fact if I close my tab, the bot will just discontinue going for walks. And then . it isn't gonna work. If I near my browser, the bots gonna stop going for walks, it's not gonna work. . There's a workaround repple it's going to continue going for walks a web server even after the tab is closed. . Now this is not an internet server. Yet we can create one to run it even as this..
but even an internet server will merely run for up to an hour with none use. Oh, . here's the rep docs in regards to the cyber web servers. See it says once deployed, the server will continue . to run within the history even after you shut the browser tab. The server will not sleep and active . till an hour after its last request after which it's going to input a snoozing stage. . So even if you have an internet server cyber web server jogging, it will eventually stop jogging after an . hour after its last request. So if nobody's utilizing the disk or bottles have stopped operating. . notwithstanding, there is a way to make the bot continually get requests so it in no way shuts down. . So actually rip insurgent has introduced that in the future they'll provide a paid plan that . enables code run constantly, no matter what it is going to just never input the dozing sage..
Yet they don't offer that rather yet. It's coming very quickly, maybe it will likely be available . by the point you spot this video. Yet until that is applied, there is aanother way to keep . your bot strolling longer than an hour. And this technique even works with the loose tier of replicas. . So to maintain this bot going for walks continuously, we'll use yet another free service called . uptime robotic, up and rollback might be set to ping the bots web server on reproduction every five minutes . with constant pings, the bot will never input the sleeping degree, and will simply keep running. So we . have more things to do to make certain our bot is working as we want to make certain it's working . strolling continuously. we'll create a web server and repple it and set up uptime robot to . constantly ping the internet server. okay, creating the web server is simpler than you may think. To.
Do it, we're going to create a new file, so visit files and click on the Upload file button. . And that is just going to be referred to as hold alive.pi. since the complete goal of the internet server . is to keep preserve everything alive. Ok, i am just gonna paste within the code from the internet server here. . it is exceedingly easy. Like I reported, and iit's not, it is not likely specifically significant for the . bot, it is just aa thing that we do in replique, to make it retain going. So I may have a link to the . textual content of this internet server correct in the description here. So you could simply reproduction and paste it also. . but you will find it's not that lengthy. So in this code, we'll import flask, . and we will use flask as the web server. And iit's going to return howdy, 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 robotic, that will the two be going for walks simultaneously. . Yet the remainder of this is simply not relevant to our bot, we just want the bot to run this information superhighway server. . So let's go back into leading.pi. And then at the suitable, we'll must import it. So from . keep alive, import, keep alive. And if you just return over here, that that it is importing this, . which is that is what runs our internet server here. So from it is going to import the net server, and then . we're going to go all how to the bottom. And right earlier than the client runs, we're going to call . keep alive. And this will run our internet server. So if i finished the bot and rerun the bot, . it's whatever one of a kind goes to happen due to the bot because of the internet server. . okay, you'll find a new window open correct up here..
And that is our web server jogging and see how it says hiya, i am alive. That's from . our information superhighway server code in which it says go back good day, i am alive. So everytime you go to the server, . it says howdy, I'm alive. Now the important issue right here is this URL. So this URL become created. . And that's the URL that we'll ping from uptime robotic. So i will replica this URL right . here. after which we will use it in the next part. So now we have to installed uptime robotic. . So the 1st step is to create a free account, and then get logged in. . okay, once you're logged into uptime robot, just click on upload new track for the track kind, . https and the pleasant call, you can call this anything we're gonna simply positioned, . bought. after which for the URL, it truly is where you paste within the URL which you copied all.
Over from repel repple it and the monitoring interval we are going to simply depart thiis the same as . every 5 mins. after which create, track and then create monitor again . track created so i will near this. And we must always be capable of see it right here. . right here it's. It's now going to ping our bot each 5 mins. So at this factor, . it will be carrying on with, continuing preserve 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 simply try out closing the tab really fast. i am just gonna copy this . URL, near a tab. let's have a look at if we can just open the tab lower back up on here . and you can see it's nonetheless running still strolling during this right here. . it could mean it is going to say something like this anytime it is pinged..
I don't know if that is from the ping or now not, or if it is simply from reloading. . Yet we are actually finished. Now the bot will run constantly, so people can always work together with . it on replicat. it is time for a bonus section, you already understand how to set up a Python bot and run . it continuously from repple it. Now I'm going to allow you to hooked up an extra discord bot . to paintings with internet hooks. we'll connect the bot to GitHub so that the bot will put up a . message for your server each time a definite GitHub repository is updated. And for this GitHub bot, . you don't even have got to code anything, or hosted anywhere. let me show you how it really works. okay, . to add this GitHub bot, we are going to visit our server. after which we'll go to the server settings right here. . after which we are going to click on integrations. And then there's internet hooks create web hook..
And iinstead of calling it Captain Hook, I'm going to name it. i'll call it GitHub. Ok, . and we are going to positioned it in the overall channel. And then i am going to simply replica the webhook URL. . Ok, I will be sure to keep. . okay, now I'm gonna cross over to the GitHub repo that i want to hook up with this bot. . So i will use the loose code camp.org GitHub repo. And then I will visit Settings. . after which on the side right here, it says cyber web hooks. okay, now I'm going to click add internet hook. . And then i am just going to paste in the URL that I copied from Discord. Now here's aan important facet, . you can not simply use the URL, you even have to upload whatever, I'm gonna add cut back GitHub at . the tip of the URL. And then for the content kind, i'll difference it to program slash JSON, . and they'll scroll down. And for what occasion should set off the web hook,.
I'm gonna do send me every thing. counting on what you're attempting to do, you may want to do just the push event or choose person hobbies. Yet I'm gonna do every little thing. So i am going to upload the net hook. And now I'm able to go back correct over to my discord channel. If I wait, we should begin to see some hobbies happening on the from the free Code Camp GitHub repo. Allow me pass and just attempt to make an event occur. i am going to simply unstarred megastar the repo. And it labored. We simply received a message from the GitHub bot, new star added. So now we've connected our bot to GitHub through internet hooks. And the GitHub bot will publish a message each time anything occurs on the GitHub repo. You may create bots to work with a large number of other external services with cyber web hooks. Ok, you've reached the top of this academic. Now you've organized to go out and code your personal discord bot.