How to make Discord Bot || Introduction to Cogs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to my new video of how to make your own discord bot in python so let's get started so guys in today's video we'll be converting our code into cogs and we'll be will be subclassing our commands dot bot so let's try get into it [Applause] [Music] so first of all i used environment variables for my tokens you can do the same and you should be doing the same secondly you can just make one folder named cogs and you can just name one file like bot.buy and you can just import stuff like discord then like from discord.exe import commands now what you can do is you can create one class which will be uh the bot class you can just name your bot plus something like custom bot or maybe i'll name my class as youtube bot and you can just pause commands dot bot in it we'll just define the init for it so i can say define init and as you can see it auto filled everything for me but uh we don't want this exactly so i'll just remove the parameters except the quarks and for these what i can do is i can just you know make it more cleaner so i'll set my command prefix as uh you know my command prefix was exclamation mark my health command was none my description would be something like this is my youtube bot so you can just fill these stuff and then you can you know select the intent so i'm gonna say intents equals my intents dot all okay it was discard dot intents dot all yeah so that is it now bot class is ready now what we can do is we can just save the file and head over to our main dot pi and in this file we can just remove the bot variable and i'll just i'll just you know comment all of these functions for now so i've commented them now what i can do is i can say if my name is equal to my main so why i am doing this i am doing this so that even if we import something from a main file then the bot should not run so i'll just get my bot object i'll firstly have to import it so i'm gonna say from abort import my youtube bot now my bot will be equal to my youtube bot and we can pass the quads if we want and then what i'll do is this is my cogs folder and let's say there's a file in it named main.pi or i should be saying basic dot pi now we'll leave it empty for now now what i'll say is for my name in my os dot list directory so os does list directory i'll just have to pass the path and i'll just pass my cogs folder now we'll check if the file which is located in my cogs folder is a pi file or not so i'll just say if my name which is the file name dot ends with and i'll just say dot py so its name will include the dot by extension now we will just load the file so i'm going to say bot dot load extension and i'll just i'll just pass cogs which is my folder name folder name is right here which is cox dot and i'll just do some formatting i'll say dot format and i'll have to pass the file name without the extension so this name includes the extension i'll have to remove it so i'll just say right name and i'll say up until negative three so this will not include the extension which is dot p y so these are three letters of the extension now we we will be loading all the files in it next we will be doing is we'll just run a bot so i'll just ctrl exit and paste it right here so if you if you want to know how these environment variables work then i have made a video for it you can just check it out now we'll just save a file and we'll move to a basic dot pi so i'll just import the basic stuff import discard from discord dot ext import commands now what i'll do is i'll create one class which will be my basic dot file so i'll just pass commands cog now i'll define the init so i'll just say init we'll pass the self and the bot and myself dot bot will be equal to my bot so this is a property we are creating and this is the bot variable and also we'll just say commands dot bot so this is just to verify the type of the parameter we are passing so that we don't pass any other client in it so now what we will be doing is firstly we need to create some commands like these were the commands and we need them to be in our cogs for an uneven function what we will be needing is we will say at the rate commands dot cog dot listener so if you notice here we have board dot event and here we have commands dot co dot listener with these parentheses now say async that on ready so this is just exactly the same and i just have the self parameter and i'll just print yo i'm there now this is for our uh on even command now for a command i can just say command start command and i can just pass my aliases as well something like emo and i'll say async def emote so i'm just replicating my other command so i'll just self and ctx so self parameter is obviously my data tree since it's a class now uh i just need to copy it as this away i'll just copy all of it and i'll save it and similarly i can do with the third command now it's not over yet we need to create one setup function as well it is required for loading the extension so i'm going to say define setup and i'll pass my bot parameter i'll say dot add cog and i'll have to pass my class which is my basic so i'm going to say basic and i'll pass my bot inside so it requires a commands.com which is my board and here we go so now go to your main dot pi this is my bot file this is my main dot file i can just remove all of this stuff for now and basically we don't need them anymore if we try running a bot okay so we got an error is just actually the aliases was to be a tuple or a list so basically i just i just forgot that now let's try running it again it says yo i'm there let's test it out in the on ready function it was to print yo i'm there and it's working now let's test our email command so i'm gonna say email and it sends all the emotes i can even say emote and it still sends them so yeah it's working so that's all for this video guys i hope you all enjoyed it like the video if you liked it dislike if you didn't subscribe and share for more and i'll see you guys in the next video you
Info
Channel: Code Stacks
Views: 186
Rating: undefined out of 5
Keywords: Code Stacks, Python for Beginners, Python Computer Vision, Python NLP, Python Speech Recognition, Python Tkinter, Python GUI, Python Turtle, Python Games, Python Data Science, Numpy, Pandas, MatplotLib, python tutorials, python for beginners, python tutorials in Hindi, python programming, python gaming, python full course, python projects, python snake, python tutorials for beginners, python interview questions, python code with harry, Cogs discord
Id: Hcwf4nWcRiM
Channel Id: undefined
Length: 8min 32sec (512 seconds)
Published: Sat May 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.