Code a Discord Bot with Python - Host without charge in the Cloud

Hi, I'm Beau Carnes with Loose Code Camp. In  this tutorial  that you're watching today,  . I'm going to provide help to create a discord  bot with Python that runs completely in the  . cloud. you do not need to put in whatever on your  computing device, and you do not need to pay whatever to  . host your bot. We are gonna use countless equipment,  adding the discord API, some  Python libraries,  . and a cloud computing platform referred to as repple  it you probably already understand what discord is,  . and what a discord bot is, since  you're looking this academic,  . yet simply in case you do not permit me provide you with a brief  evaluation. discord is an app used for genuine time  . text chat, discord servers are hooked up by way of people  and generally are concentrated on a certain community.  . Many groups and enterprises have discord  servers for people to talk with each other..

broken image
broken image
broken image

Recommended bot simply slid into the server. But also  you spot over 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's time to start  that. Now I stated previous that you don't have  . to install anything, you do not have to apply any  application apart from your information superhighway browser, we're going  . to be creating the total discord bot, right in  the cloud on replique. The website for replique  . is our epl.it. And replique is an internet ID  EA, that you can simply use in your cyber web browser.  . So it makes it a lot simpler to code things if  you do not have to install any distinct software  . in your computing device. And then you can log into  it from dissimilar browsers, distinctive computers,  . and then you can easily entry your code and your  courses whatever computer you are using..

So we are gonna make our button this and then  it can also be totally hosted in the cloud,  . we can even run the bot in this website, so  we do not have to run it on our regional computer.  . And that will make it that will preserve the bot  walking, no matter if our desktop shuts down, or we're  . gonna in which we near the browser tab or anything  like that, i am going to allow you to make the bot. So  . it'll retain strolling within the  background constantly.  after which you don't have to stress approximately it.  . If you do not already have account, you're  going to sign up. but i'll log in here.  . Ok, now that i am logged in, and I  click this new repple within the  nook here,  . now we just need to create, choose the language  that we'll use for this new repple. The  . cool element approximately repple it's that you can use  a bunch of alternative languages with it. And so.

That makes it simple. If you are going to run some  of these languages in your computer, you probably  . are going to have to install some  additional software  or extraa things in your desktop in order to run  . those languages. but when you just visit repple, it,  you could do exactly it correct in your browser. And you  . haven't got to put in whatever. but we're going  to be doing this in Python. So I will click on Python.  . And it'll have a default name right here. But  i am just going to be calling it advocated.  . Ok, and I'll just create the repple.  . So let's move over the most portion of Rep. What we  have here, that is right here is the code editor.  . And you'll discover a default with a file open  known as main.py. And we are able to variety our Python  . code right in right here. And then right here over here  are the files, and you'll create new files,.

 

We would be creating a few new documents within the  course  of creating this bot, you also can create new  . folders. And then over here is the console. The  console is where the conventional output will show up.  . and you will be caused to enter enter if  you are going to have input on your code.  . And in Python, it's going to it is going to double  as an interactive prompt. So you can type  . in Python code here. And it'll evaluate  the code interactively. So that's distinctly cool. And  . then over right here, we've some things at the side  right here. so files, version manage, if you're going  . to go together with git, and or we're not going  to do that during this tutorial , though. after which if  . you're going to set up applications, but there's also  otherwise to put in patches that we'll see.  . after which the main element we're correctly going to  be using is the replique database. So one thing.

Cool approximately replique is it contains a database  as part of it. And so you may effortlessly store  . matters that will retain saved in there even if you  stop your program and run it again the everything  . store in the database will will still be in there.  So we will be utilizing that later during this educational.  . after which you also can difference some  settings,  just like the theme of font length, all sorts  . of stuff like that. but let's return to the  files because we'll begin creating  . our discord bot right this moment. So I mentioned that we  are going to be utilizing the discord.py library. And  . the very first thing we will do is import that  library. So at the top has this do import Discord.  . And the cool component here's that during repple it it  will immediately set up this dependency when  . you press the Run button. So I imply my permanent  and do greatly at this time but when I click Run,.

We will see at the part right here, that it is Installing  the discord despit dependency, after which it will  . be capable to use it in the program straight away. So  there's nothing additional you should do to install  . dependencies besides simply uploading them right  in your application. So let me talk a little bit  . about discord.pi. Before we maintain with this bot  here, it revolves around the concept of movements,  . and event, it is anything you listen to  after which respond to. for example, while a  . message occurs in discord, you'll receive  an event about it that you can respond to it.  . So let's make a bot that replies to a  particular message, we're going to start  . with a very easy bot, and then we're going to  upload to it and just add more features as we cross.  . So the simple bot that we will start  with the code is truly taken straight from.

The discord.pi documentation. it is a really good  example of like the most basic bot you can also make.  . and like I said, then we'll retain to build on  that after we get this simple bot created. Ok,  . so we received the discord library, imported. So  let's add a few extra lines right here. So now we are  . going to create an example of a consumer. This  is the relationship to Discord. So do a client  . equals discord dot purchaser. that's simply part  of the discord.py library. Now, we're going  . to apply the client that occasion decorator to  sign in an event that seems like this.  . So I keep in mind i discussed that this client  uses pursuits to make it work. So that's 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 named while whatever else occurs..

So in this code, wherein you're going to use  the unready event, so i will placed async.  . def on all set. So this event is going to be called  when the bot is able to start getting used . And  . when it's start, when it's prepared, whilst the bot is  all set, we'll do this print. And this is  . going to print correct to this console right here.  So whilst the bot is run and ready to be used,  . it will print this to the console, it's  going to assert we have logged in as and then we're  . going to truly be capable of get the username  that's logged in as, and it is the way you do that.  . So there's a few methods to do it.  Yet so it truly is taking the customer,  . this zero is being changed with the client. So  it's doing the client that user So that's this  . purchaser, this zero receives changed with this client  here as client, that user to get the username..

And to make sure I have thiis right next  to the quote is going right here. And there we cross.  . okay, let's create an additional occasion. So that's the  occasion that occurs as soon as the bot is able,  . and it's operating. So the subsequent event is that if it  receives a message, if the bot senses a message  . right within the  discord server. So it's going to  begin simply the same to register the development.  . And we're going to do a different async  function. but this one is on message.  . So those function names are specifically  from the discord.pi library, it is looking  . for these function names, to know what  to do, while sure matters take place.  . This onmessage occasion triggers every time a message  is obtained. but we don't desire to do whatever if  . the message is from ourselves, that is the  bot. If the message is from the bot, we need.

 

To do not anything. And this event takes an argument  known as message. Or you may name it anything you  . prefer. But it's classic to name a message  because it's the message that's being acquired.  . So this event triggers each time a message is  obtained. but we don't need it to do anything.  . If the message is from ourselves, we are gonna make  it investigate if the message is from ourselves. So if  . message dot author equals purchaser, that user  seems like I spelled whatever incorrect there.  . So if the author of the message is the user,  our bot person, all we'll do is go back.  . subsequent factor we'll do is we're going  to work out if the message starts with a command  . that's being despatched to our discord bot. And we can  make up these instructions to be anything we want.  . So subsequently, i'll show you we're going to  do if message dot content material that begins starts.

With and we are just going to make it as much as be  dollar sign hiya. For our bot. we'll say  . each of the instructions are messages that begin with the  dollar sign. So if the message begins With dollar  . sign Hello, then the boss should be aware of  that  it is received a command so it should return  . anything. And to come back a message  back to discord, we're going to await  . message dot channel dot send. So this goes to  ship a message to the channel. And the message is  . going to be whats up. Ok, so now so anytime the  bot notices that somebody says dollar signal Hello  . inside discord, it is going to respond good day with  an exclamation point. So it truly is every thing we  . desire for the bot. but we still have to run the  bot. So here is the line to run the bot client  . dot run. after which right in the client that run  we need to positioned our token, we have to placed our bots.

Token that is like the password for the bot.  earlier than we just go and replica the token returned and  . positioned it correct in here, there's something I should  inform you approximately replica it is advisable to be careful  . with. by means of default, all replicates are public,  they are visible to anybody. So anyone can just  . entry your duplicate. And they may potentially  see whatever you install your replica, including  . any secrets and techniques or keys. And keep in mind, I instructed you  that thiis key is sort of a password, you don't want  . anyone else to find out about it. but there is a  way to maintain things mystery. And that's to use an  . environment variable. So in repple, it permit me show  you the way you should use environment variables, we are  . going to first off, create a brand new dossier right here. And  the recent dossier is going to be called dot E and V.  . in order that anything we put in  a dot E and V file will  not be made public, no one will be capable of see.

Whatever in a dot E and V file in replique, pretty  a lot on a public account on a free tier account.  . Everything is public necessarily except just this one  dossier dot E and V, we are gonna placed token equals,  . and we are gonna set the token to equal our  token. Now we're gonna have to go back and  . get our token. So let's go back to our development  portal web page, the discord comm diminish developers,  . and then i am gonna visit bot gonna copy the  token. after which we will go back over right here. Ok,  . there's my token. I'm going to disable  this bot later and change the token. So  . do not trouble  trying to figure it out to use it  in your possess code. Yet now that we've got our token,  . we need to make it so it gets into our main  file. So to do this, we will have to  . do have a different import announcement here. So I'm  going to put import OS, after which down here,.

We need to entry that token from the dot EMB  file. 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 is token here  . is due to the fact that's the call I placed right here, you  can actually use any call for the token doesn't  . have to be token, you only have to make sure that  if you end up getting our environment variable,  . you use an identical name right here. So now when you  run the code, it's just going to replace  . this here together with your token. Now, if you're  doing this regionally, or you're doing in such a  . way in which you recognize, no one else is ever gonna see  this code, one can put your token right  . in right here. And you don't even have to import o 's  here. Yet considering we are doing it in a public manner,  . we need to ensure that nobody else sees our  token, or else they could get access to our bot..

and that is in fact it for our trouble-free bot.  Like I pronounced, we'll add a few more  . extra capabilities later. Yet let's run it now  and test this out. So click run here. Ok, look,  . we've logged in as advocated bot. Now let's go  to our server. you will discover look before it showed  . as logged out, yet now it indicates it as logged  in. So permit me let's try this. i'll say whats up.  . Now you will find that is from from me, and it didn't  reply. due to the fact remember, the one of a kind command is  . actually dollar signal whats up. So let's test that.  And iit works encouraged by means of simply answered with  . hiya. So our bot works if we do yet another command  like hello, how are you or now not not an additional command  . an additional message? it is not likely to respond in any respect.  but when we do the dollar sign hi there. Then it will  . reply. So we simply created a simple discord bot  that responds to the command dollar signal hiya..

and clearly you can also make it any command. So if  you do not need to respond to greenback sign good day,  . you'll be able to use any command here. And then  you can change what it's going to reply with.  . Now that we've got our basic  bot operating, we will improve it.  . I pronounced it is known as endorsed bot for a  motive, the bot will respond with a message  . of encouragement. every time someone sends a  message containing a sad or miserable notice,  . we're gonna make it so anyone will be capable to add  encouraging messages for the bot to use. And these  . messages would be stored in a database. So they  will store even when you stop and start your bot.  . We are also going to feature another thing in the  database. that activates whether that turns  . on or off even if the bot is is responding to  messages. And a further function of encouragement.

We're going to do is that the bot is going to  return a random inspirational quote from an API,  . when somebody versions the message, dollar sign  inspire into the chat. So let's begin with  . that last item I stated, we're going to start  by way of including the dollar sign spider function.  . I found this API referred to as Zim prices.io. That will  return inspirational fees. Now, you should use any  . API. So if you don't need to use in rates.io,  you may simply find aa different one. If somehow  . this is not operating, just test to find a different  API, but we'll use is in charges.io. And  . all these principles will practice to really  any API that you are utilizing. So let's go back to  . our bot. And we'll import a few new  things to work with this API. So what we're  . going to do is we're going to import a pair more  Python modules, we will upload a get quote.

Operate. And we will update our bot code  to call the operate. So let's import requests.  . This request module enables your  code to make an HTTP request  . to get data from the API. The API returns  JSON. So we'll must import  . the JSON module. What because that's going to make  it simpler to work with the info that's lower back.  . okay, now we're going to add a  helper operate. it is known as get,  . quote, this is a function we will call to return  a quote from the API. So first, we're going to  . use the request module to request data from  the API URL. So that's going to go like this,  . the response we will 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 locate this URL through staring at the  documentation of this in quotes API. So HTTP,  . S, colon cut down cut back is in quotes.io. minimize API  cut back random, it is going to go back a random quote.  . Ok, now we will convert that response  into JSON. So we'll do JSON, data  . equals JSON dot a lot, reaction, that textual content. So  this response that textual content, I simply had to look in  . the documentation of the API, and found  out that the reaction goes to have  . this reaction that textual content that i will, i can load  into JSON. So we have to do whatever with  . this JSON, we have to get the quote out of it.  Now, this turned into numerous trial and error, I had to  . print out the JSON data and take a look at to figure out  how how to get the precise part of that JSON  . that i wanted. So so this 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 is part of  . this in prices API, the end result that's returned  from this API is going to have a key called q  . while the price goes to be the quote.  So we're going to begin with the quote,  . but then we want to upload after the quote,  the writer of the quote who mentioned it.  . So i am simply going to feature a plus we're going to  do a little 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 right earlier than the person's call. It's just a  way to variety of show who's who is doing the quote.  . So we will do one more 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. Ok, so we  got a quote in here. Now, i can go back the quote..

Ok, we have now to replace our bot. So it uses  this get, quote operate to get a quote, and then  . return the quote, as a message to Discord. Let  me scroll down a little here. So now I'm just  . going to replace those lines right here. This changed into just an  instance to rich to reply to the message good day.  . but that isn't what our final bot is going to  do. So i'll difference this to inspire.  . And let's flow this over a  little bit. so we can see more  . of the code. So if the bot receives this message  inspire, we'll ship a quote. So  . the way we'll send a quote is first  including another line. So I'm gonna placed quote,  . equals get, quote. So it is going to call the  get, quote, operate and return the quote here.  . And now i will just go back the quote, or I can  ship the quote to Discord. okay, at this point, we.

Can run the code and check it out. If it's already  jogging, we got to forestall it, after which run it returned.  . after which we have to wait until it says it's logged  as encouraged bot. that means  we are able to go back to  . our Discord server, and say, Hi, bot would not say  anything. Let's see if it will say, hi there, hey.  . Ok, that command doesn't work anymore.  due to the fact we change it. Let's do motivate. Ok,  . we have an inspirational quote. And let's try  inspire some  more instances to work out if it is random.  . There's another one. There's another  one. See, we are getting a random quote,  . for every time we do it. So it truly is the quote part  with the aid of failing to organize you're preparing to fail.  . after which we add that the gap after which the  sprint, and then the author Benjamin Franklin.  . Ok, well, that works. Let's move, return to our  bytecode. Now we'll implement the function.

in which the bot responds with encouraging messages  whilst a user posts a message with a sad word.  . So in this, this time, the user isn't placing a  sure command into discord to get a outcome. This  . time, the bot goes to be checking each word  in every message to work out if there's a sad word. And  . if it's if it notices a tragic notice, it's going to  go back an encouraging message. So the first thing  . we will do is to create a Python list that  contains the unhappy words that the bot will respond  . to. So right after the customer variable is created,  I'm going to create a different one known as unhappy .  . Words. And the CI notice. that's simply going to be  a catalogue like I mentioned, oops, there we cross. depressed,  . unhappy . Now i am gonna have a catalogue of  phrases right here, you may upload additional words.  . genuinely, you could put any words you want on  this record here. I will have a miserable. And.

Let's do yet another. How about depressing? okay, so  we'll make it so every time the bot sees  . one of these phrases within the  message, it's going to  reply. So what's the bot going to respond with?  . Well, now we will upload a list of encouraging  messages that the spot will reply with when it  . sees a kind of words. So I'm going to make a  new variable called starter and encouragements.  . Now, i am going to let you know why this is  known as starter encouragements.  . it is because these are the there's going  to be just some encouragements that the  . bat begins with, yet users will be  ready to upload extra encouragement later  . that are stored in the database. So the start  encouragements I'm simply gonna do a cheer up.  . Hang in there.  . And you are a wonderful individual, or perhaps it's  just aanother bot. So we are going to do person slash bot.

be happy to feature any number of encourages you  like, and keep in mind later, person will be able  . to feature more encouragements. Now we have to update  our bot to use both lists that we simply created.  . So first thing we will 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're going to update  the on message reaction right here, we'll add  . anything on in this on message async function,  that's gonna assess each of the messages to see if  . they comprise a note from the sorrowful phrases list. And  then if the sad notice is located, the bot will send  . a random message of encouragement. So here is the  code for that. after we determine to work out if it has  . starts offevolved with motivate, then we will check  just if it has any other note from the sorrowful word.

record. So it's how we're gonna do if any, if any  weight, if any be aware in message for be aware and sad  . phrases. So thiis just skill it is going to go through  each be aware, and the sorrowful phrases. And gonna see if  . any of those words are in the message. If it sees  any of the observe in the sorrowful phrases in the message,  . then it will Oh, wait. Message dot channel  dot send. This is just like this line above here.  . And what is it going to send, it's going to send  random dot selection, it is going to select a random  . choice from starter encouragements from  that record that we created. Ok, we can  . experiment the button Now, let's have a look at if there's  something incorrect right here. undefined call message.  . okay, received it. I got in advance of myself  right here. correctly, this message should be,  . it's aactually purported to be message content  by swap to message, because when you consider that we are.

Going to be using message content a bunch  of times, what I want to do actually, is  . create the message variable here message  equals message content. And then every time we  . see message content material, we will switch it with  message here. Ok, let's test the bot now.  . Ok, let's return over here. Let's  see if the encourage still works.  . labored. Now let's examine the sorrowful notice. I'm unhappy .  . you are a extraordinary individual bet Brett person's  lifestyles spot. Well, now, I'm depressed.  . Hang in there. Oh, that is making me  that's making me think much better.  . thanks . This encouraging through is extremely encouraging.  So it's operating, it may now reply, whenever it  . sees sees a stad method, it will try to  inspire the person that put that unhappy  message.  . So at this factor, you basically  understand sufficient to create your personal bot..

but let's preserve going because we're going to  upload a few extra stepped forward functions that I already  . discussed. Now let's give the chance 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  . before, we'll use repple. It's built  in database to store person submitted messages.  . This database is a key significance shop that's built  into every insurgent. So let's upload that to our code.  . Now. Now, I confirmed you this earlier than this tab here,  this the only really the sole factor of this tab  . is to just show you the commands the  most common instructions for the database.  . And you don't really need to get in from here  if you understand him due to the fact one of these guys,  . yet 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 permits us to use the  mirror database. earlier than we upload new commands  . for the bot. Let's add two helper functions that  the 1st one will have the ability to add customized messages  . to the database. And the second one helper function  would be used to delete messages from the database.  . So let's move lower than the get quote operate.  . This new operate is going to  be referred to as replace encouragements.  . So that's gonna it will update  the encouragements in the database.  . And iit's going to take delivery of an  encouraging message as an issue.  . So first, it will verify if encouragement  is a key in the database. So if encouragements  . try this, correct, encourage myths is in dB  keys. So DB keys simply returns a listing of  . the keys in the database. So if increment  is already in the keys, it is going to get.

Those all the key all the incursions are  already in the database. encouragements  . equals the way to get a price from the  database that saved lower than a certain  . key is like this DB encouragements.  okay, so we've gotten the encouragement.  . Now we simply need to add the hot encouragement to  this list. So encouragements dot append that's  . going to add whatever new to this record. And we're  going to kind in the encouraging message, which is  . the argument that was handed into this function.  after which when we append the new encouraging  . message to the old encouraging messages, we  have to put it aside again into the database. So DB  . encouragements equals  . encouragements. Else, if there's not  already advocated us in the database,  . then we're going to have to  create it. So dB, encouragements.

Equals, and we'll placed a bracket  here because this goes to be a catalogue,  . yet it's only going to have one message. And  the single message is the encouraging message  . that changed into passed into this operate. Ok,  so now, let's have a look at what we did wrong up right here.  . okay, that's presupposed to be if encouragements  in dB key no longer is I just perfect typed an  . essence of it in. So that's imagined to be in.  Ok, so now, we complete our helper function,  . let's create a brand new one, which 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 bought the  list of encouragement from the database..

 

we have now to examine if the length of  encouragement is greater than the index, because  . the person could pass in an index that's,  that is actually not in that that list.  . the person could circulate in an index that's actually  now not part of the record. So if the size 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 save it into the database again  . equals encouragements. And this can  correctly be in this if declaration.  . okay. finished. we will now add an  encouragement and delete and encouragement.  . Now let's replace our bot code to apply these  features. So clients and discord can update  . encouragements and delete encouragements,  right from Discord. Let's go down here.  . So the first thing we will do, if you  keep in mind, currently, it returns a choice from.

The starter encouragements if it sees a tragic observe.  but we wish to additionally  have the ability to return a random we  . want the random encouragement to also be able to  come from the database. So let's upload a few 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 recommendations equals starter encouragements.  . Ok, so now, if there are if there's any  incursions in the information in the in the database,  . if inspire intended in dB keys,  . then we will, we will add those to the choices. So  concepts equals innovations plus DB encouragements.  . Ok, so now in preference to settling on from starter  encouragements, we will select from options  . of encouragement to come. Now we're  going to feature a few code to feature a new.

person submitted message to the database.  So it's going to be seeking a brand new,  . a new command. So if the message  from discord starts offevolved with the command,  . knew, if it begins with the command new, that  capacity it is going to be a new message to feature 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 places greenback sign new, they'll put  . an area, and then they're going to positioned the new  encouraging message. So we need to break off  . the encouraging message from the the hot command  because we don't want to feature the hot command to  . the database, we just favor whatever text comes  after the dollar sign new,  text that  . the user submits, after DOS, I knew we want  to feature that to the database. So let's do let's.

Get the encouraging message. And the couraging  message goes is going to equivalent message dot  . break up, we'll cut up off the message  from New. So we're gonna do dollar sign new.  . Ok, so we are actually 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 portion of the array goes to be  . the recent message. Now you will discover that we're not  just splitting at New, we're splitting up new  . space. So there is a space after that, because  we don't need the user is going to place dollar  . part new space after which the message and we  don't desire to add the gap to the database.  . We simply want the message after the database  or after New. So we bought the new encouraging  . message. And now we simply have to update the  encouragement with that new encouraging message..

Then we wish to ship whatever returned to the user  so they understand that it worked correctly. So wait  . channel or message dot channel, dot send. New  and courage couraging message further. Oh, yeah,  . that that ought to be and back, I don't know  why retain doing that. If incursions and DB keys.  . Ok, now we will add the ability  for a consumer to delete a message. So  . a brand new function if message that starts offevolved with if  it begins with Dell, or delete, that's what Dell  . stands for. That starts with Dell, we are going  to get the record of messages. encouragements or  . we're unlikely to get the list of messages, we're  just going to actually just create an empty list  . called encouragements, because we're going to want  to return the hot list of encouragements later.  . So we're going to begin it started this with an  empty list. And we'll examine 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 fits that starts with dollar sign,  . delete. So if it starts offevolved with Dell, Dell, that  ability delete. Then we're going to first create an  . empty list. 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. Because if there is not any encourage us in  the database, and we don't want to attempt to delete  . any of them, so if it's already, whoops, we are going to see  if increments are in dB keys, then let's get the  . index from the message that the user submitted  by way of Discord. So we need to get the index,.

Or you must split it from from  the here. So message that cut up.  . And clone of earlier than, we're going  to, it is going to seem very similar  . Dell. And this time, including the distance there is  elective. What let me show you what I intended.  . So earlier than we add a space here, this time, we don't  always have to have an area due to the fact we are  . going to convert it to an integer. So whether  there's a space or not, it will nonetheless correctly  . convert to the integer, whatever not there, we're  going to kind in a variety of, the user goes to  . have various after the command del, and it's  going to be converted to an integer in Python.  . Now, we simply must delete the encouragement  . that's at that index. after which finally, we need  to get a catalogue of all of the encouragements that  . get the liver current record after it's  been deleted. Because we'll.

return that to the users for you to see  the updated list after iit's been deleted.  . And now we will ship that back  a weight message dot channel dot send  . encouragements. okay, so the explanation why we made an  empty record here's because if there, if there's no  . encouragement in the database already, it's just  going to return the empty list here. but when there  . is encouragements in the database, it will get  those encouragements and then go back go back those.  . Ok, let's test this bot. And then  we'll add some  a few extra functions.  . So i am going for walks the bot returned.  . Ok, the bots jogging and we can test  it. So let's experiment the other capabilities first.  . We still get an inspiring message.  Now let's test to feature a new message.  . You are amazing.  . okay, hi, simply found out I made an error  manner up right here. database has no characteristic key..

So that's what's the key with an S at the top.  So let's discontinue this again. Let's run this back.  . that is why you have to test frequently  to trap these, these errors like this.  . Let's return over here. Ok, now let's try  typing this returned. returned, new, you are amazing.  . New encouraging message further. Let's upload another  one new way to pass. that won't make sense based  . on the message that the man put in  to get that  message but that's okay. New, you are the best  . coder on the earth. Ok, now in order  to receive get this kind of messages,  . we suggest we must have some  sad words. So I am  unhappy . Hang in there. We've already seen that one.  . Let's just variety in unhappy . again. Way  to head. Now, way to go. that is one  . of our new messages. So this was one of the  fashioned messages, the starter encouragements,.

 

 

Yet now we've acquired randomly one of  our new messages. i'm not sad anymore.  . you are a extremely good individual bought. i am depressed.  Well, I guess you ought to spell it correct.  . Cheer up attempting to see an additional new one.  . You are amazing. that is another  new one which we placed in there.  . Ok, now let's have a look at if we will delete a message.  So we should have the ability to delete a message with  . doe. And let's simply delete the  first one. So I will just spend zero.  . Now it is going to return the messages that  are left. Now just way to cross and you are the  . best coder in the world. It doesn't have a  me So that one became deleted. in order that labored.  . immediately the only way you will discover all the  messages which are all of 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  features. Let's go back over to the bot.  . So after adding the power to get  a listing of user submitted messages,  . from correct from discord, we will add  the ability to turn on and off no matter if the  . bot responds to sad words. So you will be able  to truly replace that correct from discord,  . even if it's going to be responding or no longer.  So we're going to type of do all that aat once.  . first off, we are going to create  a new key magnitude pair in the database.  . So correct after the starter encouragements, we're  going to envision if responding is in the database  . yet. So it truly is gonna be a new key in the database  responding. If it's not in the database, db keys,  . then we will add it. And it's  going to default it's going to start off  . with the aid of being genuine reply, factor equals genuine.  And we do must placed a colon right here..

And now we only favor to reply if that's genuine.  So down right here, in which it responds to words. That's  . this entire section right here is when it's  is when it's responding to the unhappy note. So  . I'm going to press tab right here, because this is  all going to be inside an if declaration. If  . dB, we'll get the value of the  responding key in the database responding.  . So if that is genuine, then it will respond to the sad  words. If not, it won't respond to the sorrowful words.  . In a minute, we are able to upload the  potential to replace this value.  . So let's go back before we add  the power to update that magnitude,  . we're going to add the ability to record encouraging  messages. So if message that starts with  . me, perhaps like wager what the user command  is gonna be used, it is gonna be a listing.  . So if the user, the user versions in record into  discord, then first off we will get.

we will make a empty list encouragements  equals an empty record because there may not be any  . encouragements in the list yet. So we're going to  check if encouragements is in dB keys with an S.  . Then encouragements, we're  going to add them we're going to  . set the encouragements in the database to  the incursions, variable DB encouragements  . 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 ought to be able  . to get a list of the encouragements. Now we're  going to have the function of changing whether  . the bot goes to reply to unhappy  words  or no longer. So if message that begins with risk  . responding, quotation mark responding,  . then we will get the price. So the idea  is that the user goes to put responding true.

Or responding fake, if the discord message  that the bot goes to accept as a command.  . So if a nano value, we're gonna get the price that  the user typed in, so is it authentic or fake split  . we'll do identical to earlier than responding.  And returned, we will positioned responding space  . due to the fact we don't need this area correct after  that. So we'd like the 1st factor in that array,  . that's the second the second one component to the  array and index one. So we bought the value. So now  . if value dot decrease, even if they typed  in lowercase or uppercase, we're going to  . convert it to lowercase. So if  discord bot invite  equals genuine,  then we are going to ensure that DB responding  . equals actual. after which permit me scroll down  somewhat right here. We're going to send  . the message message back to Discord. Oh, wait.  Message dot channel dot ship. Responding is on..

okay, we're gonna have an else, i am just gonna  replica  this whole aspect. So if worth decrease does  . now not equal actual. i'll just difference this to else.  We are actually making it so the person doesn't  . even have to sort in fake anything  but real will be assumed to be fake. So  . encouraging, responding is going 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 rather executed, although. We're  no longer rather executed. we will experiment it.  . And then i'll tell you  approximately yet another very important  . step. So let's do this. Let's go to again to  our bot. Oh, we need to run the bot again.  . Ok, the bot is walking. hey,  bot. I'm sad. Ok, it further that  . now let's get a listing of our messages.  Remember sent 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  saved in the database, they get saved dependent  . among times that you've run your your code. So  let's upload one. Oh, put upload. nice. I'm simply pleasant.  . Oh, I did that incorrect. It's aactually supposed to  be new. make sure you  sort within the  instructions, correct.  . And let's record them again. Show is  pleasant. Now let's delete whatever delete  . index one that ought to be the center one. And Yep,  it shows them that we may even record them again.  . Now let's have a look at if we can change flip off and on  even if the body's responding. Let's confirm  . that the bot is responding. I'm sad. nice, huh?  maybe it's announcing pleasant since the information suggested it's  . tough to inform with this bot. Yet let's have a look at if we  can flip off the messages. So responding false..

If I variety in sad, and it would not say anything,  if I sort in depressed, it doesn't say whatever.  . but when I do responding, actual.  Ok, let's do say I'm depressed.  . Ok, the bot thinks that is particularly pleasant. Now,  in view that we created the responding variable in the  . database, it will shop weather among times even  if you shut down your bot and rerun it, it will  . nonetheless save even if it is presupposed to be responding  or not. okay, so technically, our bot is completed,  . but you don't need to stop now. Because if I close  my tab, the bot will just discontinue going for walks. And then  . it's not gonna work. If I near my browser, the  bots gonna stop running, 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 an internet server. but we can  create one to run it at the same time as this..

Yet even a web server will in basic terms run  for up to an hour without any use. Oh,  . here is the rep doctors about the cyber web servers. See  it says as soon as deployed, the server will continue  . to run in the background even after you close up the  browser tab. The server will stay awake and active  . till an hour after its last request after  which it is going to enter a snoozing stage.  . So even when you've got an internet server internet server  going for walks, it is going to sooner or later discontinue walking after an  . hour after its final request. So if nobody's  using the disk or bottles have stopped operating.  . notwithstanding, there is a thanks to make the bot  endlessly get requests so it by no means shuts down.  . So in fact rip insurgent has introduced that in  the long run They will provide a paid plan that  . facilitates code run constantly, no matter what  it's going to just not ever input the napping sage..

but they don't offer that quite yet. It's  coming very soon, maybe it will be available  . by the point you spot this video. Yet till that  is carried out, there is aanother way to keep  . your bot walking longer than an hour. And this  technique even works with the unfastened tier of replicas.  . So to maintain this bot jogging constantly,  we will use an additional unfastened carrier called  . uptime robotic, up and rollback might be set to ping  the bots internet server on duplicate each 5  minutes  . with constant pings, the bot will never input the  sleeping level, and could simply hold going for walks. So we  . have two extra activities to make certain our bot  is working as we need to ensure it is working  . walking constantly. we will create a web  server and repple it and mounted uptime robotic to  . continuously ping the internet server. okay, creating  the web server is simpler than you might imagine. To.

Do it, we'll create a brand new dossier, so  go to files and click the Upload Dossier button.  . And it truly is just going to be called hold alive.pi.  since the total objective of the web server  . is to maintain maintain everything alive. Ok, i am just  gonna paste in the code from the net server here.  . it is quite easy. Like I said, and iit's not,  it's not likely specially important for the  . bot, it is just aa thing that we do in replique, to  make it hold going. So I may have a hyperlink to the  . textual content of this cyber web server right in the description  here. So you could simply reproduction and paste it also.  . Yet 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 net server.  And iit's going to return hi there, 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,  on the way to both be going for walks concurrently.  . but the remainder of this is simply not relevant to our  bot, we simply want the bot to run this information superhighway server.  . So let's return into main.pi. And then at the  excellent, we're going to must import it. So from  . keep alive, import, keep alive. And if you just  go back over right here, that that it's importing this,  . which is that's what runs our information superhighway server here. So  from it's going to import the internet server, and then  . we'll cross all how to the lowest. And  correct earlier than the customer runs, we're going to call  . maintain alive. And this will run our cyber web server.  So if i ended the bot and rerun the bot,  . it is whatever distinct is going to happen  due to the bot due to internet server.  . okay, you will see a new window open right up right here..

And it really is our cyber web server walking and see  the way it says whats up, I'm alive. That's from  . our web server code in which it says go back howdy,  I'm alive. So everytime you  go to the server,  . it says hiya, I'm alive. Now the important  component here is that this URL. So this URL become created.  . And this is the URL that we will ping from  uptime robotic. So I'm going to replica  this URL right  . right here. after which we will use it in the next  area. So now we need to set up uptime robotic.  . So the first step is to create a  unfastened account, and then get logged in.  . Ok, once you're logged into uptime robot,  just click add new track for the monitor sort,  . https and the pleasant call, you can  name this anything we are gonna just placed,  . purchased. And then for the URL, that is where  you paste within the  URL which you copied all.

Over from repel repple it and the monitoring  interval  we will just leave thiis the same as  . each five mins. And then create,  monitor after which create monitor again  . monitor created so i will close this. And  we must always have the ability to see it right here.  . Here it is. this is now going to ping our  bot every five mins. So at this factor,  . it is going to be carrying on with, continuing  keep to run even once we close our tab  . or even after an hour. even if there's no  exercise in discord, this bot will simply run  . constantly. Let's simply attempt ultimate the  tab surely speedy. I'm simply gonna copy this  . URL, close a tab. let's have a look at if we  can simply open the tab again up on here  . and you will discover it's nonetheless running  still walking in this right here.  . it could imply it will say something  like this each time it's pinged..

I do not know if that is from the ping  or not, or if it is simply from reloading.  . Yet we are actually accomplished. Now the bot will run  continuously, so people can necessarily interact with  . it on replicat. it is time for a bonus area, you  already know how to set up a Python bot and run  . it constantly from repple it. Now I'm going  to aid you hooked up one other discord bot  . to paintings with cyber web hooks. We will connect  the bot to GitHub so that the bot will publish a  . message in your server anytime a definite GitHub  repository is up to date. And for this GitHub bot,  . you do not even have to code anything, or hosted  anyplace. permit me show you how it really works. Ok,  . to add this GitHub bot, we will visit our server.  after which we will visit the server settings right here.  . And then we will click on integrations. And  then there is web hooks create cyber web hook..

And iinstead of calling it Captain Hook, I'm  going to call it. I will call it GitHub. Ok,  . and we'll positioned it in the final channel.  And then I will just replica  the webhook URL.  . Ok, i am going to make sure to save.  . okay, now i am gonna move over to the GitHub  repo that I need to hook up with this bot.  . So I'm going to use the loose code camp.org  GitHub repo. after which i'll visit Settings.  . after which on the facet here, it says web hooks.  okay, now i'll click on Upload information superhighway hook.  . And then i am simply going to stick within the  URL that I  copied from Discord. Now here's aan important aspect,  . you can't simply use the URL, you also have to  add anything, i am gonna upload reduce GitHub at  . the top of the URL. And then for the content  sort, I will difference it to application slash JSON,  . and they'll scroll down. And for what  occasion should set off the web hook,.

I'm gonna do ship me every thing. looking on what  you're attempting to do, you may want to do just the   push occasion or select individual hobbies. but I'm  gonna do everything. So I will add the net hook.   And now i can return correct over  to my discord channel. If I wait,   we should always start to see some  activities happening  on the from the Loose Code Camp GitHub repo.   Allow me move and simply attempt to make an event  ensue. I will just unstarred megastar the repo.   And it worked. We simply acquired a message  from the GitHub bot, new superstar added.   So now we have linked our bot  to GitHub by means of cyber web hooks.   And the GitHub bot will publish a message  every time anything happens at the GitHub repo.   you could create bots to work with a lot of other  external services with web hooks. okay, you've   reached the tip of this educational. Now you've  prepared to go out and code your own discord bot.