Python: Making a Discord bot (Part 7: Cogs)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to part 7 of my making a discord bot in Python a youtube series in this video we're going to be going over cogs now cogs are incredibly useful when creating discord BOTS they essentially allow you to sort of organize your code in a way where you can split up your commands your events whatever and you can organize them into different files you don't have to per se but you just sort of create classes containing different events in different commands and in this video I'm just going to show you how to implement them in your bot and yeah let's get right into that so first things first I want to create two commands that will help me load and unload these cogs or these extensions I should say and I'm just going to create two commits I'll do that really quickly now so first things first we are going to say that client doc command with the parentheses there next for our function header we are going to say async def load and this is going to be my load command and then within the parentheses here I'm going to say CTX to represent the context and extension so CTX is going to represent the context as I said an extension is going to be the COG or sorry the well yeah the COG that I want to load all right so pretty straightforward so far now the method that we want to use to load the extension or the COG is going to be the load extension method and the way we use that is we say clients because this is a client method client dot load extension and then the parentheses there at the end now there is going to be something going inside these parentheses here but before we do that I'm just going to sort of create some more files and folders over here so I'm going to create a new folder and I'm going to call it cogs all right it's going to be in the same directory as my bot py file and inside this folder as you can see right now it is empty but I'm going to create a new file and I'll call it example dot py and this is going to be the folder that will hold our cog now we're not going to deal with that just yet we'll come back to that but since the cog is now in a folder called cogs I need to access the Python file inside that folder so what I'm going to do is I'm going to create an F string here and I'm going to say cogs dot extension and essentially what this will do is when I run the command giving it the extension example because I'll have an example cog it will go into the cogs folder and then look for the COG right so that's essentially what that sort of cogs dot extension part is doing there so that is my load command and the unload command is very similar so similar to the point where I'm just going to copy and paste the command over here and I'm just going to change the load extension method to unload extension all right everything else can stay the same let's just double check oh right we have to change our function header so that we have a different command so we have load which loads the extension and unload which unloads the extension all right now I'm going to show you one more thing this isn't required but it's probably recommended what we want to do is sort of when our bot goes online we want to load all of the existing cogs that we have and to do this we can use the OS module so what I'm going to do is I'm going to come up here and I'm going to say import whoops import OS alright let me save that I guess I didn't have to say it's a habit and it's a good habit but down here just before client dot run I put it here I don't think you necessarily have to do that yeah probably not but I do it down there just to keep things clean but I'm going to say for filename in I just want to move this out of the way OS dot list dirt and dir order as I said stands for directory and list dur is going to list out all of the files in a given directory here now the directory that I want to sort of loop through is going to be the cogs folder that I have here right because I want to get I want access to the files within that folder right so what I'm going to do is I'm going to say dot forward slash and essentially that's going to say well the dot forward slash represents the current directory that I'm in and then I'm going to say cogs so it's essentially saying look in the directory I'm in for the cogs folder and then give me all of the files within that within that directory it's essentially what that line there is doing here and we're going to loop through it and say that for each file we're going to call it filename alright and file name is just going to be a string containing the name of each file so in this case it would be example dot py now within the loop here we need to check to make sure that the file that we are that we are currently on is a py file right so what I'm going to say is if file name ends with dot ends with this is a string method here and then inside I'm going to pass the string dot py put a colon there at the end so now what's happening here is we are looping through all of the files within the cogs directory and we are checking if it's a py file if it is a py file then we're going to do something with it and what we are going to do with it is we are going to load it so we are going to say client dot load extension and again we are going to create an F string here cogs dot like not extension final name now this still isn't perfect the file name has to be or sorry folks this needs to be square brackets here and this needs to go back there we don't want to have the dot py at the end of the file because remember the file name here is going to be example dot py and we know it ends with dot py because we checked it in our if statement so we don't want to load cogs phile py we're sorry we don't want to load cogs example DUI we want to load cogs dot example so what I'm going to do is I'm going to use splicing and I'm just going to cut off the last three characters there which is essentially going to take example dot py and just cut it down to example and this will work for all dot py files because they all end with three characters there and since we know they're all dot py we we know that we can just remove them right so that is essentially it now we have our file here and this should load them properly now let's go ahead and create the COG before we test these things so let's go in our example dot py file now first things first as you may have guessed import discord and from this scourge ext import commands all right so rather standard code similar to what we have up here in our main file next we want to create a class so I'm going to create class example because this is going to be my example cog which is why I called the file example anyways and this is going to inherit from commands dot cog all right very important you have that there I'm going to have the colum there at the end next I'm going to create my init function so def underscore underscore init underscore underscore pass itself and we are going to pass in the client and this is essentially going to allow us to reference the client that we have created up here within the cog we're passing it into the cog next we are going to say it's self dot client is equal to client and actually I guess this this will be the part that will allow us to access the client within our cog but that's it for our initialization you can add more there but you don't really have to and we also need a setup function and this is essentially a function that will allow us to connect this cog to our bot and the way we create that is we say deaf setup and inside the parentheses here we need to say or we need to pass in client or take in a client and then inside of the function we are going to say client dot add cog example an example is going to be we're going to pass in B client again so we are running a method of client add cog and inside of the parenthesis there of the add cog method we are going to be passing in an instance of the example class that we have just created right it doesn't do anything so far it just initializes but we'll add some code to that alright so let's look through back through quickly and just sort of recap what we've done so far because I feel like I might have been going a bit fast but let's just sort of go through step by step so first things first we created a load command right and this is going to load our extension pretty straightforward and we'll use this to sort of demonstrate what it does and you'll be able to see when I use the help command to see when we've loaded up commands and unloaded commands etc as you may imagine the unload command is the opposite of the load command but you don't know if the load command does just yet just understand that it's the opposite and we've created a loop here which essentially when the box turns on its going to go through all the files in this cogs folder and check if there's a cog that or sorry if there's a py file if there is load it like a cog alright so that's essentially what it does here and then the bot runs and then this is the cog that we want to load unfortunately it doesn't do anything just yet but let's add some code so first things first actually let's go back here actually can I split this down let's split it to the right perfect this is actually kind of nice so if you noticed here in a lot of my other videos at least after the event video I guess it was in my first video as well but I've had a non ready function or a non ready event and in this case I don't have it here and because I want to sort of show you how you can add events to cogs so the way we do this and some in my example dot py file here I'm going to say at man's docked cog dot listen listener if I can spell that right L is T and ER with the parentheses there and this is going to be our function decorator for within a cog so if you want to create an event within a cog you must always have this decorator here alright so in the same way that we had a client event you must have at commands dot cog dot listener and then the parentheses there all right next we are going to say async death on ready and as you may remember the on ready function or the on ready event I should say does not take anything in and if you look through the documentation which is linked down below you will see that it does not take any does not take in any arguments when it is called however since it is inside of a cog or a class I should say you must pass in self all right very important self must be the first parameter or sorry the first argument that every function inside of your class takes all right so async def on ready and then within the parentheses we have self nothing else and then our colon and last but not least I'm just going to say print what what did I have before bot is ready bot is on line let's say bot is on line I don't even remember and that's it for our event now I'm going to show you how to create a command so similarly sorry I burped there I don't know if you heard it but at commands dot command with the parentheses at the end very important you don't forget the parentheses and especially up here we also have a capital here so don't forget those so act commands docked command with the parentheses there next I'm going to be creating a ping command similar to the one I created in my earlier video where I went over I think it was in my commands video where we created a man that just said pong and said the ping or the latency but we're not actually going to have the latency part it's just going to say pong for demonstration purposes don't worry but async def ping and then we are going to pass in self and we are going to pass in CTX to represent the context all right we don't need anything else and then inside of the command itself I'm going to say await CTX send pong and that's it that's how you create a command in a cog so you need to have this function decorator up here for commands maybe I'll put a little comments here commands and then events all right just make sure you have the setup function here very important and make sure you actually load the extension in your main file so let's try running this let's see if we have any errors bot is online so as you can see the event is working right so okay let me sort of walk you through what happened here let me let me maybe make this a bit smaller and move it down here so first things first the client was created it created a load command it created an unload command and then it loaded an extension which it found in another folder right and in this extension it happened to have an unready function and a ping command so it loaded that on ready function and then loaded the ping command and then when it ran itself once it became ready it knew that it already had an on ready event so it triggered it right it knew it was supposed to do something when it became ready so it triggered that custom event right so that's essentially it let's head over to discord now as you can see the bot is online now let me show you so if I type dot help there is the default help command and as you can see here it says example and we have our ping command right so we know the ping command has been registered we can even try it out if I do duck ping it says pong so perfect our cog seems to be working our command works and our event works now I want to demonstrate sort of what happens when I unload the function here so again let's look at the help command I guess you could have just looked up here but it doesn't really matter we have our cog and the command associated with that cog up here and we also see that in no category which is sort of the default main file we have the help command which comes by default and the load and unload command which we created here commands which we created here now watch what happens if I unload unload the example doesn't do anything because all it does is load or unload I probably should have put something like CTX and extension loaded whatever but we don't have any errors here so we can assume that it worked let's check if it did if I do not help the example cog which you remember from up here is no longer there if I try to do dock ping it doesn't work and I mean I guess if I had another event such as like you know maybe if someone joined the server or if someone sent a message but as you can see here we did get an error saying the command ping is not found right because the the command doesn't exist anymore we essentially removed the reference to what was supposed to happen when the dot ping command was sent right so if I want to get it back I can do dot load example again it won't do anything because unfortunately I did not ahead or think to add a message notifying us when something was loaded or unloaded but bear with me we're going to check to see if it works if I do not help as we can see the example cog is back and the ping command is back there and if I do dot ping it's back alright so it seems to be working and we are able to now unload and load various various cogs so and by the way if you noticed when I ran this command it didn't say anything like the command ping wasn't found because it was found it knew now what it was supposed to do when when the ping command is run right and I just want to sort of demonstrate one more thing I'm going to create maybe a last sort of test command we can leave this running actually but I'm going to create another file new file I'm going to call this a little test op py and I'm going to import this score from disgorge ext import man's I'm going to go through this real quick class we'll call this one test and commands cog we are going to initialize it self client self dot client is equal to clients all right and I'm going to create one more command and if you remember here similar to how we did it here we need to have the add commands dot command with the parentheses there and then we can create our command normally the only difference being we need to add self so I'm just going to create a test command let's say I whoops man stop man async death yeah it's just called test we're going to pass in a self we are going to pass in context and we are going to say await CTX not send test I don't even need to capitalize it this is just demonstrating something here def setup we need to have our setup function we are going to pass in client and now inside of this the setup function again if you remember like we did here we need to add the cog so we are going to say climb dot add cog and we are going to take the name of the class here which is test and pass in client all right so very quickly I have made a brand new cog right I'm going to save it now keep in mind the bot has been running this entire time ok I'm going to head on over to the discord here now let me just sort of debt or explain what I'm trying to do here I've created a new cog meaning this this main file here has not rerun itself right so this for loop has not triggered again meaning this here has not been loaded right if I type dot help as you can see here we only have example but since I have my brand new test cog here and it exists in the file or in the folder here I can view dot load test again nothing's going to happen but we can check for errors doesn't seem to have been an error here and if I type dot help as we can see here we have a test cog appearing with a test command and if I do not test the bot says test so now I can modify cogs or sorry I can modify the content of a cog unload it reload it I guess you can make a reload command for just sent which essentially just unloads and then reloads and sort of like this actually we can even do it right now so we'll change this from unload to reload and we'll just unload it and then load it alright so that's that's all it's doing when I run reload and an extension name just unloads it and then loads it of course since I changed this in the main file I would have to rerun that file since it's not a cog but that's essentially how you would create a reload command but yeah you're essentially able to modify the content of a cog unload it and then reload it or just load it or use your reload command I'm sort of confusing you at this point just unload the the existing or the COG that you had initially and then load the new version of it and essentially you'll just have those updates again it will not work for your main bot file in this case in my bot py file here I was not I'm not able to actively see those changes without rerunning the file or rerunning the bot if I do not help the reload command is not there but as you can see here if I turn the bot off rerun it and then do dot help I now have a reload command all right so that's essentially sort of the different things you can do with a cog you can unload them reload them or load them got them confusing myself at this point you can use them to organize your events you can use them to organize different commands into sort of different categories of commands you can use them for so many things and they're so incredibly versatile or versatile and I say that properly but yeah that's that's cogs so I hope you guys enjoyed the video if you did leave a like I know this has been a longer video than usual I don't know how long it's been but I'm guessing longer than 10 minutes maybe less than 15 but yeah if you liked it leave a like comment down below for videos you want to see in the future join my discord invite link will be in the description too just I don't know chat or ask for help whatever someone will be there to help you if not me and yeah I hope you guys have a good night alright bye
Info
Channel: Lucas
Views: 167,579
Rating: undefined out of 5
Keywords: python, programming, discord, bot, rewrite, v1.0, youtube, tutorial, english, part 7, cogs
Id: vQw8cFfZPx0
Channel Id: undefined
Length: 22min 54sec (1374 seconds)
Published: Sun May 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.