Discord.NET Bot Development (S2) · Getting familiar with commands · Episode 4 [OUTDATED, SEE DESC]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to the fourth episode of the discord.net bot development series in this episode it's time to get a bit more familiar with commands how do they work what features do they have and how can we make our own commands let's get started the first thing that we will do is go back to visual studio and we will go back to the general.cs class this class is a class that we made previously in episode 3 to quickly show you how commands work and that we had a working command handler let's first go a bit more in depth about this command and let's first see what does this very line do because we are doing something that's called inheritance a general class inherits from module base if you hover over module base and press f12 you will actually be led to the definition of this very class and some of the things that it contains when you go here you can see a few things the first thing is that you can see the type called context you have something called after execute before execute on module building and reply async now these three methods are irrelevant we won't use them and they are used by the module base in the background but will not be used by us the one that is interesting however is the reply async method over here this method allows us to simply reply to an incoming message which is exactly what we use in general.cs we use this function which if we press on f12 is also in the module base and is a simple method to reply to a message as you can also see it has various parameters the message if the message should be text-to-speech if there's an embed attached to it request options mentions that should be put into it and which ones are allowed and the message reference now these methods all originate from the module base and therefore when we actually inherit the module base and then make our own class called general which we'll then inherit from module base it means that we will have these methods at our disposal this means that in theory you could also make your own module base attach its own methods to it to make your life a bit simpler but that's basically what's going on there the second thing is the command attribute what is it this command attribute simply marks the execution information for a command that's basically the explanation and what that means is that this attribute will let our module know that hey this method under it is a command it should be ran as soon as this command is executed which is ping so as you can see in discord as soon as we executed the ping command what would happen is it would execute away reply async and then we would receive the pong spawns but the command attribute is not the only attribute that there is there are in fact a lot more the first one that i want to start off with is the alias command say for instance you have a music pod you probably know rhythm or groovy with those mods there sometimes is the option to use a full command like play but sometimes you can also just use p these are all aliases for instance for ping we could use the same we could instead of typing out ping fully just say p now if we were to run the bar head over to discord and now say exclamation mark p we would immediately get a response because now it listens to all aliases as well and we have an alias that's called p even if we type out the full command that would still work it's just an alias with the already existing command the nice thing is is that you can add as many as you wish because the parameter is not just a string it's an array of strings you can provide as many as you want they can be pin they can be test and all of these will now work as soon as you start running the bot and head back to discord so now if we do examination mark test you would also get the response it's a pretty nice attribute and it can be really useful when you want to make it a bit easier for your users let's look at some more attributes for instance there's an attribute called require user permission this is a nice attribute that you can use to check if a user has a certain permission this can be channel permissions gold permissions all kinds of permissions that you might want to check for instance let's say that we want this command to check for gold permission administrator meaning that only people with administrative permissions will be able to execute this command then he would simply do it like this now if we get rid of the namespace we can hover over guild permission press the light bulb and say using discord this means that as soon as someone tries to execute this command without having administrator permissions it will fill let's give it a try we run the bot head back to discord and let's see what happens when i try to run it it will work but if i now head over to my old account and say ping here i would not get a response now you might have expected an error there but remember we have not yet implemented any error handling so right now when an error occurs while executing your command it will simply do nothing let's head back to visual studio because there is even more when we head over to the next attribute we can also check for about to have a certain permission this might be useful if your command for instance changes something about the server or the channel and then you want to make sure that your bot's actually capable of making that very change so you could for instance check does this spot also have administrator permissions now if you run the bot again we go to discord and we run the ping command again it will respond because the bot has administrator permissions just like we added it previously but if we head to the settings go to the server roles edit the watermelon roll and take away its administrator permissions and then try to run the ping command again it will not be able to because it's missing the administrator permissions but now if we go back again to the roles the watermelon roll permissions and give it the administrator permissions again you will see that it will respond again because now it has the required permissions therefore the preconditions are met the next attribute is pretty simple it's called require owner this is a very simple attribute that you can use in order to check if the person that's executing 10 command is the owner of the bot because this is also a precondition it actually replaces the require user permission but we will quickly get rid of that but keep in mind you can still use these two if you for instance want to just check this but since i find them a bit unnecessary i will just get rid of them for now if you run the bot head back to discord and i execute it it will work perfectly fine since i am the owner of the bot but if i go to my old account and run the command again it will no longer work because my alt account is not the owner of the bot the common misconception is that this attribute is actually for the server owner that's not true this is only for the bot owner but this does not check whether or not the person executing this command is the owner of the server but if the person executing the command is the owner of the discord bot the next attribute that we can use is require nsfw this might be very useful for commands that have not saved for word content basically this attribute makes sure that the channel that you're trying to run this command in as nsfw enabled so if we run the bot now go back to discord and we try to run this command in here it will not work if we create a new channel called nsfw and then enable nsfw channel then you will see if we enter this and now try to run the ping command it will work perfectly fine because again the condition is met and this channel is nsfw there are more attributes that you can use and you can simply find these by just typing in something random here going all the way back and then pressing on this filter which will only show classes here you can find some of the attributes that you can use for instance alias which we already have command which we again already have but there's also things like name to give the command a specific name or for instance there's also remarks which is to give a simple explanation about your command but also summary which is maybe a bit of a longer description now why would someone use summary or remarks or name these might be very useful in the future if you want to make a help command because then you can refer to these attributes you can then simply in your help command say hey reach command at the prefix grab the name of the command and then attach the summary or remarks to it this makes making a help command way easier because you don't need to fill in everything manually but you do it per command which is a lot more efficient these are basically a few of the attributes that you can use when making some commands let's look a bit further into the command itself because reply async is not the only thing that we have here in fact we have a lot more when we try to refer to the context let's take a look if we go to context there's a lot of options that you can use here and context is simply a reference to the current context in which channel is this command being run to which client are reattached in which service is command being run who executed this command all these things belong to the context and you can use those to run certain methods get some information all those kind of things one of the things you can for instance do is you can trigger the bot to start typing this can be simply done by saying await context then where do we want to type the channel so we say context.channel and then trigger typing async here you can also see that it will broadcast the user's typing message to all users in this channel lasting for 10 seconds keep in mind that you can also use a bit of a different method here which is dot enter typing state this works a bit different because this will keep going until the return object is disposed a bit of a simpler way to put that is as soon as you for instance reply with a message it will stop typing we will use trigger typing async for now because this also basically does the same then instead of using reply async you can also just send a message using the context.channel because that's where we want to send a message and then send message async and then we just say palm you can also even send a file by using away context channel dot send file async to just send a file there's a lot more methods that you can use you can find all of them by just using the period and then look at all the suggestions that you can see then again channel is not the only thing you can use you can also use the user referring to the person that executed the command and for instance send them a message and then say hey this is a private message just like that let's give it a try we run our bot add to discord and let's go to the general channel and say ping now as you can see not only did the bot start typing it responded with pong and i also received a private message saying hey this is a private message if we go back to our code there's also things that you can do with the server you can for instance say await context dot gold referring to the server and here you can also do a lot more things you can get information such as the description of the server or some other information but you can also ban people create an emote an integration you can create roles a lot more things that you can do the next thing that we will learn is arguments and we will do this interactively by creating a command that will be capable of getting some information of a user let's start off by using the command attribute we will give this command the name info just to make it very simple we will not give it an alias and then we will attach a method to it by saying public async task info async now i just mentioned arguments what are arguments say for instance you want to get information about another user how would you do that how would you allow another user provide some kind of information so that you know which user to look for this is pretty simple if we go to discord i'll quickly give you an example what we could do is we could say info like this to get some information about the current user but we can also say simulation mark info and then mention a user to get some information about the mentioned user in this case we will be talking about optional arguments because you can either provide a user but you're also allowed to not provide a user let's take a look if you go to visual studio a very simple way to just add arguments will be to first provide the type of argument that you want this can for instance be a string this can be a number this can be anything but since this is a mention we will use a socket guild user now what we will do is we will use control period to go to the suggestions and say using discord.websocket and then we say socket guild user because this argument is optional we want to make sure that you can also provide no user in that case what you do is you say equals null this means that by default the argument is nothing but it is optional and if someone adds something to it it will be replaced and will no longer be null now what we can do is we use curly brackets to open up this method and the first thing that we want to make sure is that we have checked whether or not the argument is null and if it is that the value still has a value this means that if there is no socket good user provided we will replace it with the user confusing the command we do this by saying if the socket build user equals null then we say socket guild user equals the context dot user as sorted guild user just like this that way if we have no argument provided we will replace it with the user executing the command the next thing that we can do to display some information about the user is to just reply let's use the method await reply async to return some information about the user a nice thing that we can do here is use dollar sign the dollar sign if you put that in front of a string like this it allows you to use variables within a string as long as you put curly brackets around them for instance if we were to say id so we're going to display the id of the user we can use curly brackets and say socket guild user dot id which will then display the id of the user that was either mentioned or if none was mentioned the current user then we create a new line by saying backslash n then what we can do is we can for instance display their name and a discriminator we do this by saying name and then we say socket guild user dot username the hashtag and then socketguilduser.discriminator so we nicely displayed our username and then the hashtag that they have then we of course create a new line and another nice thing that we can display is for instance when their account was created we do this by again using the dollar sign quotes and saying created at and then we'll use the curly brackets don't forget to add a plus over here and then we'll say create it at early brackets socket guild user dot created at which will nicely display the date when this account was created let's give it a shot let's run the bot add to discord and say exclamation mark info it should return some information about us as you can see it gave me my id my name and tag and the date and time when my account was created plus the time zone now we can also try this by seeing if our argument is working we do this by saying info at paul paris to for instance check this information as you can see it's not working why let's check our console there's nothing really here so that leaves us to wonder why are we not able to check the information of this user for that let's head back in our code because we at this point have no idea what the error is it's time to add some basic error handling we do this by going to the command handler and here we can bind a new event to the service or the command service let's do this by saying this dot service dot command executed plus equals on command executed this means that whenever a command is executed we will run our own method press on the light bulb and say generate method command handler dot on command executed i'll make sure you can make this task synchronous and let's change the names of the arguments let's make this name command info this one the context command context this one the result a simple way to check if the command had any errors is to just check if result dot is success then we simply return so in this case we have an early return statement which means that if the command was a success nothing happens if that's not the case we can log the error to our console but it is optional you can also just give the error back to your user which is a bit more convenient if you want to let them know what went wrong but as for now just simply return the error to the user and later on you actually also log it into the console you do this by simply saying away command context dot channel dot send message async and then we just say resolve dot error reason and then end with a nice same column this kind of works like the commands we have a variable called command context we have the channel and then to the channel we will send the error reason let's give it a try if we head to discord and we now run the command on this user we get the message user not found it is strange because the user is there and the bot should be able to see it but this is where we finally bump into the presence intense since a while now when you want to request information about other users you need to enable presence intents we do this by going to the browser going to the discord developer portals and enabling the privileged gateway intents we do this for the presence intent and the server member's intent we then save the changes and if we try to run the command now by saying info paul barayas the user will still not be found it is because to let these changes take effect we need to restart our bot if we run the command now you will see that everything works perfectly fine i will leave a link down in the description below to explain a bit more about presence intents why they need to be enabled and why discord made them keep in mind that as soon as your discord bot hits over 100 servers you're required to verify your bar to use these intents that was it for this episode in the next episode we will go a bit more in depth about analyzers and coding conventions if you want to know more about arguments and commands you can take a look back at season 1 where we go a bit more in depth about the arguments but we will also use more arguments later on in this series in episode 6 we will talk a bit more about embeds and then also use more arguments within commands but before we end this episode i would also like to personally thank one of our very first patrons since a few days now we have our own patreon page and our very first patreon is jason thompson with patreon there's a few tiers that we have the official patreon which comes with patreon only updates behind the scenes content and a shout out we have all access patreon which comes with early access to new episodes patreon only updates behind the scenes content and again a shout out but we also have a vip patreon this includes private live streams where you can for instance ask me questions or request some support patreon only updates get early access to new episodes behind the scenes content and shout outs i would like to mention that all the things that we receive from the patreon page actually goes back into the channel for instance as soon as we hit five patrons we will start doing live streams that are public for everyone and coding nights on the coding with effingham discord server through stage channels and money that we receive through this patreon page will be given back to the users in for instance the christmas giveaways or for instance to explain how servers work or for instance to buy software that's required for instance record these episodes edit them and publish them if you would like to take a look at our patreon page you can find the link down in the description i hope you've liked this episode and i hope to see you in episode 5. [Music] [Music] you
Info
Channel: Coding with Efehan
Views: 1,596
Rating: undefined out of 5
Keywords: discord, discord bot, c# bot, c# discord bot, discord.net, discord.net bot, c#, c# tutorial, programming, discord commands
Id: plvHEXoitG0
Channel Id: undefined
Length: 24min 0sec (1440 seconds)
Published: Sun May 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.