Python: Making a Discord bot (Part 3: Commands)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to part three of my making a discord bot in Python youtube series in this video we're going to be going over how to create commands so what our commands well you can think of them sort of as like an event because an event is a piece of code that is triggered when the bot detects something has happened right while a command is a piece of code that is triggered when a user tells the bot to trigger right they send a quite literal command or in order to the bot essentially saying hey I want to run this command can you do it for me and then the bots go there can't I if you can it does that's essentially a command so with Ted in mind we're gonna create our very first command so how do we do this well first we need our function decorator similar to the one we have above our event here we are just going to say at client dot command and we need to have a pair of parentheses here at the end why do we need that because there are a couple of attributes for our command as a whole that we can change so for example we could set whether a command is hidden let's say you're making command that you want only people who you know maybe have worked on the bot to know exists right and you don't want the general public you know maybe other people who have invited the bot to your server to know that a command like that exists you can hide it or you want to set an alias you know multiple ways to run the same command which will actually do later in this video you can set that to so again you need that there and I will show you how to use it when we set an alias for a command that we're creating later in this video it's just a fun one too so next thing we need is our function header so we say async def and then the name of our command so by default the name of your function is the name that you will use to run the command all right the name of the command that you will use with your command prefix I don't know if I explained that perfectly name the function what you want your command to be all right that's basically it all right so we're going to be creating a ping command so we are going to say async def pain right we need our parentheses there and our colon there so if you remember from my last series if you guys excuse me if you guys did not watch my old series and you're new to the channel something like that you can skip ahead to when I start working working on the rest of this but if you remember from the last series in the async version of discord py we had to go into these parentheses here and set past context equal to true right well in the rewrite version or version 1.0 context represented by CTX here is passed in automatically so you don't have to worry about passing context in or forgetting to do that or anything like that because it's all there automatically just make sure you have this or you just have a a parameter set to represent context as your very first one or as the very first one the very first one and um you should be good to go just make sure you have it there or your commands you know you guys might be using the wrong parameters within your commands etc etc so context is passed in automatically that's my point there and I think I might have confused a couple people there so that was the end point that I was trying to make all right so we have our command created or sorry we have our function created so this is now a working command but it doesn't do anything so we want to make it do something we are now going to make it make we are now going to make our command make our bot reply when we invoke the command all right so we are going to say a weight CTX again that is the context that we have obtained here see TX dot send and in the parentheses here we are going to say pong all right so if you MIT as you may have guessed this line right here makes our bot say or when the command is run the commit of the bot will say pong jeez I'm stuttering so hard today but let's see let's see so if we run this BOTS online if I type dot ping here pong alright as you can see it worked here and we have ping which is the command name if you remember I said the function name is the command name and then as you can see when the command ran it sent Punk using the method send from the context object alright so that's essentially it for the ping command however I'm going to I'm going to also make the ping command show the latency of the bot just to give it a bit more functionality and because some of you guys might be like yo what the heck you made a pink man it just says pong that's that's stupid I'm pretty sure I had a couple comments like that in the in the last video I made so I'm just going to make this an F string and then we are going to use client dot latency alright and that's essentially the ping the only problem is it's a it is a value given in seconds so if you guys you know maybe play a couple games or something like that or have seen your ping anywhere else it's usually measured in milliseconds right so how do we get it two milliseconds well as you may have guessed just multiply it by a thousand right because there's a thousand milliseconds in a second and then and then jeez stuttering so badly please excuse and then we are just going to round it off I can even put em s there so that people 100% know that it's in milliseconds so if we run this now let's wait for the bots to be ready yep and go back here we do ping pong 51 m/s so that is the latency of our BOTS and and that is sort of our first command and we've given it a bit of added functionality by adding that in there so now we can check the ping anytime we want using our pink mint all right so now we're going to create something a bit more fun now that we know how to create a command so client command async death and this function is going or this function this command is going to be an Eightball command right so if you've ever had a magic 8-ball you know yeah get a question shake it up and it'll it'll have something up there saying oh yeah sure it's gonna happen or no it definitely won't or something like that right so we are going to make the 8-ball command however there is a problem here if I wanted to make a command 8-ball where it looks like this actually you can see here from the syntax highlighting we can't do that if you guys know basic Python syntax you cannot name a function starting with a number right so what I'm gonna do here is I'm just gonna put a little underscore there just to sort of say okay you know make this a valid function name and I can tell it's 8-ball I could just type it out or something like that and you know eight but if I type it out like this for example I mean it's kind of pointless because it's longer to type out and I could just set an alias which is what I'm gonna do anyways so I'm going to say underscore eight and then here I'm going to say alias is make sure you spell that right is equal to a list of strings so within this list here I'm going to say 8-ball now what this will do is all of the strings within this list can be used to invoke this command so if I put something here like I don't know test then I can use dot test to run the exact same command all right so what do I want to have here I need to have my context I need to have I want to have a question because I want I want my command to say you know dot eight-ball will my YouTube video get lots of views I don't know we'll try that one actually and the way I've set it here this here with the asterisks here allows me to take in all of the rest so you'll see you'll see it'll allow me to take in multiple or what would be multiple parameters as as sort of one multiple arguments as like one argument right parameters I believe is the wrong word to use there but let's get into that so first things first have a list of responses for an Eightball which I pulled off of Wikipedia it's a it's a rather sizable list I believe there's 20 of them there there's ten positive ones five neutral ones and five like negative ones so these ones here are no these ones are like you know try again later kind of thing and all of these ones here are yeah you know you're right it's gonna happen I don't know but that's essentially that the question here we're going to use when we give our reply and but what I need to do is I'm going to import random here because I want to give a random response and I'm going to say a wait see TX dot send all right so the exact same thing that we did up here right I'm going to make an S string and I'm going to say question is equal to or not is equal to but question the question was question and then on a new line I'm going to say the answer is going to be random dot choice and then we're gonna get it from responses right here so just like that let's double check everything here is looking good so we have our aliases 8ball and tests I can probably remove this one actually no I'm gonna have it there for demonstration purposes we'll use it twice 8ball again we cannot have it starting with an eight right so we use the underscore there we take in a question we have a list of responses and then we essentially spit out a random one all right so let's run this hope there are no errors Bhat is ready cool cool cool so let's test out the first alias we have there actually it's kind of covering it let's move this down actually we don't really need to have that up so we have our two aliases it's like it just off to the side you can see some of the code and everything so we have our two aliases 8ball and tests all right so if we do not 8ball and we say we hold this YouTube video get lots of these question will this video YouTube video get lots of views answer yes definitely yes and that's a hundred percent random so you get is better like this video hit the like button thank you now we can try the test command or the test alias are you sure we'll put a question mark there to concentrate and ask again alright maybe one more time test are you sure yes alright this is looking good this is looking good but um that's essentially it we've made a little 8-ball command I've showed you guys how to use aliases I've showed you how to create commands in the first place we've made a simple command sort of showing off or not showing off but that displays the bots latency and we've created a fun command that you guys can implement in your BOTS and just sort of mess around with your friends with I don't know but that's that so yeah hope you guys enjoyed the video if you did leave a like sorry for stuttering so much this is like the fiftieth take just because I've constantly been screwing something up to the point where I just don't like it which is usually why these videos take so long to make I probably started this at like 12 no not 12 like 1 so I've been trying this for like an hour and then I still have to make a thumbnail and everything so again thank you for watching if you guys stuck around to the end you guys are the best and leave a like if you liked it comment down below what you want to see in later videos I'll have plenty of links to the documentation the official help server and my help server and yeah hope you guys have a great day but I'm out bye
Info
Channel: Lucas
Views: 308,798
Rating: undefined out of 5
Keywords: python, programming, discord, bot, rewrite, v1.0, youtube, tutorial, english, part 3, commands
Id: DEqrCI1018I
Channel Id: undefined
Length: 12min 45sec (765 seconds)
Published: Fri Apr 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.