Discord.NET Bot Development · Commands & embeds · Episode 3 [OUTDATED]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the third episode of the discord.net bot development series in this episode we are going to make our own commands now before we make our own commands please make sure to watch episode 1 and 2 as we are going through installing the software and learning the basics in these episodes now if you've watched these episodes let's actually start making our command handler and start making our own commands now you just heard me saying let's make our own command handler but don't we already have that well yes and no we have a command hunter class but we don't really have a method that is handling our commands and that is exactly what we're going to make today because we want our bot to be able to handle commands let's get started in the previous episode we talked about the ready event which basically runs when the discord button is ready today we're going to use another event which is called discord dot message received and this event is triggered when a new message is received we're going to use the same exact way as that we did already so we're just going to use a plus equals and then we're going to say on message receive and a semicolon now again this will become red we can just hover over it and select the light bulb and click on generate method command handler on message received and this task will basically handle all our commands first thing we're going to start with is making a variable for our message so we don't need to call it every single time a simple way of doing this is by using var msg equals arg as socket user message and this basically handles the message puts it in a socket user message and then we can call it by just calling message like this now there are a few things that we want to make sure of the first thing we want to check is if the message atta is a part we just want to return because we do not want any messages sent by a discord bot to be read by our own bot now again it might become red and there is a very simple explanation for this because as you might have seen in the previous episodes we need to return task dot completed but because we're actually going to use await and asynchronous functions actually want to transform our task to a asynchronous task and now the error is gone and everything is fine now let's make a new line and define the next thing that we're going to use and this is basically called the context so what you want to do is say var context equals new socket command context and then underscore discord and then the message now what we can do with context is we can for example get the author from a message we can get the server from where the messages send in the channel and every single other thing that we might need now what we're going to do is we're going to say int pause equals zero and this position is going to be used to make sure where to search for the prefix and we want that just to be at the beginning of the command now let's actually make something that is going to handle the prefix we want to start by making an if statement and between these brackets we're going to say message has string prefix and then we're gonna pass in our configuration prefix now how did we do this before we went to the startup service and we did this we said config and then we said tokens discord now because we don't need to see in this tokens and then discord we can just use prefix so we're just going to copy this and don't forget to remove the semicolon and paste it in here and now we can replace this with prefix and now it's actually gonna get the preflix that we have defined here and then paste it in here now what we also want to do is we want to tell where to search for the prefix and that is exactly at ref pass so that will be zero at the beginning of the string now what we also want to do and this is optional you can skip this if you want but we also want to see if the box is being pinged so when the mod is being pinked at the start of a message the mod will also work for this we can just use has mentioned prefix and then underscore discord current user and then again ref pause so it actually search for the beginning of the command now we want to add another bracket and then open with two curly brackets now what we want to do is we want to execute the command that it comes after the prefix for this we're going to save our result equals await underscore commands dot execute asynchronous and then context which we made on line 34 and then we're gonna pass the position and we're gonna pass our service provider now basically this will return the result of the command being executed this means that if there's for example an error we can fetch that error based on the result now what we want to make sure of is if the message has been successfully executed or the command so what we're going to do is we want to say if resolved and then is success which basically checks if the command has successfully been executed if this is not the case then we're going to use two curly brackets and when you enter you know the reason so we're going to say var reason equals result dot error and this basically gives us the reason why it did not succeed now what we can do is we can say await context not channel dot send message async which is going to send a message to one of our channels and it's going to say the following error occurred now here's something new that we're going to use we're going to use backslash n now what does backslash n do very simple backslash and actually means that it generates a new line for us because in this string we can just do enter because then we're going to get some errors or it's going to use a plus sign but then we don't get a new line so to get a new line we are using backslash n now here we can actually pass the reason to actually know what's went wrong so here we're gonna just pass the reason between two curly brackets and don't forget to add a dollar sign at the beginning of the string now we use a bracket again just to close the string and then we use a semicolon because it's the end of our line now something else that's optional but not necessary is console logging the error so we can actually let the console know what went wrong so we can also do console.writeline and then reason so the console also gets alerted about what went wrong now this is actually what all we need just to execute the command we could actually use more if statements to more filter messages or commands and we could also use more complicated versions of error handling but that is not something we're going to do in this episode because in this episode we are going to make our own commands now to keep all our commands a bit organized we are going to use a folder to store all the command files in we're going to make a new folder under the solution explorer going to click on our solution click on add click on new folder and you can call this whatever you want doesn't really matter but for the tutorials we're just going to call these modules now in this folder you want to create a new class and you can call this again whatever you like it doesn't really matter but all that's important is that you give us a name that you can recognize so i am just going to use general not cs so this basically means that we just have the general slash basic commands in this file click on add to create the file and there we go we now have a class now to tell our program that this file can be used in order to execute commands we're going to use a column and then say module base and if you use tab it'll also automatically add the using disk or documents but if it doesn't make sure to add this otherwise you're going to get an error also let's make this class public and now we can make our commands between these two curly brackets how do we make commands you might ask this is also very simple you use two square brackets and in between these type in command now we're going to use normal brackets and a string what did we just do we told our program that the command is whatever is here between the string and we're going to start with a simple ping command so we're just going to say ping so now when the bot receives a message that has the prefix and this string ping it will execute the task that we're gonna make under this so let's make the test we're gonna say public async task ping and then between two curly brackets we can make the commands do something now here we want to make sure that we actually include the tasks library so click on the light bulb and click on using system trading tasks now we can use something that we've defined earlier and that is the context because here we don't have something like message like if we head over to the command handler we have here socket message argument but here we have nothing if you paid close attention then you notice that in the command handler we pass the context with the executing command so here the nice thing we can do is we can say context and then for example get the message or use a channel or use the server or whatever you want but you can just call context instead of arg now what we want to do on this line is let our bot respond with palm because ping pong so what we're going to do is we're going to say wait because this is an asynchronous function that we're going to use we're going to say await context dot channel dot send message async and we've used this function before remember where we use it in the command handler to let our users know that there was an error now we can just say a string so we're going to use the string and we're going to say pong exclamation mark and a semicolon just one line to make our bot send a message i told you things would get a lot easier now save it and run our program now the program is running let's check discord as you can see our bot is running and let's use our prefix exclamation mark because we set that in a config file and then ping and there we go responded with pong which is exactly the message that we told it to respond with perfect everything works now we want to make another command because we can make as many commands as we like in this file another nice thing we can do is embeds let's start with a nice command is actually very nice and we're gonna call it info we want this command to return some information about the user that is using it so what we can do is we can say public async task and then info two brackets curly brackets and now we're gonna make an embed you might be wondering what is an embed i can showcase you a very simple example for the example i went to the discord api server and as you can see here our danny send a embed so an embed is actually something that looks similar to this and it's not really just text it's like a nice formatted text with some nice colors a picture here and there and now let's make our own one to actually create our embed let's say var builder is because we want to use a embed builder equals new embed builder brackets and again notice that now we automatically also added using discord so if it didn't add it automatically for you make sure to edit now we can pass a few arguments the first argument we want to pass is dot with thumbnail url and in between brackets we can now tell a nice picture to let the bot send along with the embed now something nice we can do is we can get the profile picture of the user that used our command to show in the thumbnail a simple way of doing this is saying context dot user dot get avatar url in some cases there are people that don't have a avatar set and in that case you want to use two question marks and then context dot user dot get default avatar url the default avatar url is for example the profile picture that our bot has it is the default discord profile picture so what did we just do with these two question marks if this is actually nothing and nothing in code is actually null so if this is null so if there is no avatar url and the person just has the default profile picture then it will use this so if the thing on the left side is nothing it will use the thing on the right side so we can always make sure that there will not be nothing now we can also pass another argument which is gonna be with description and then a string and here we're gonna say in this message you can see some information about yourself or any other messages you want to put in it's fine we can also define the color that is being used on the left side of the embed and for that we're going to say dot with color and then new color and here it's getting interesting now between these two brackets we need to define the red green and blue value basically rgb just pick a nice color you can go to google and just search for color picker for example if you search on google for color picker you're gonna get this nice color picker you can select a nice color that you want we're gonna go for a nice bluish color and you can then copy the red green blue value right here go back to visual studio and paste the value right here now the last thing we're going to do is we're going to say with current timestamp and this will add a nice timestamp at the end of the embed telling us when this embed was generated now let's add a semicolon because now we've passed some few arguments that we like you can even add more and a full list of arguments that you can use will be available in the description now we want to transform this builder in an actual embed to do this we can say var embed equals browser dot build and this will build the builder and then transform everything that we just passed in here to an actual embed now last but not least we want this embed to be sent in a channel that we used so now we can say await context dot channel dot send message async we don't want to send any text so we're gonna say null we're gonna say false and we're gonna say embed now this is basically gonna send our embed let's try it out save the program run the tutorial go to discord and try out a new command there you go a nice embed with some text here and there and now let's actually add some information about the user now let's close our console and let's go back to visual studio now to add some information about this user we want to add fields now how do we add fields just let's go here behind the collar press enter and let's use add field now here we can define the title of our field so let's start with saying user id so we're going to get the id of the user and to get that we can just say context dot user dot id and we want to make this inline and inline means that it will show up next to each other so fields will show up next to each other instead of under each other now let's run this and see what happens let's go to discord and say info and there we go now we have our user id now let's go back close our console go back to visual studio and let's add some more information for example we can add the discriminator so what we can say here is discriminator and then we say context dot user dot discriminator and again we make this an inline field and let's also add the creation date of the user now to do this things get a bit more complicated but for the title we can just say created at but for the date we need to use something that is called a to string function now we can use context.user.createdapp but this doesn't give us a nicely formatted way of the date it gives us some weird kind of date time which is the value or the type of this if you hover over this you can also see that this is a date time offset but that's not what we want because this is going to tell you the time the seconds the milliseconds if it was a.m or p.m and a bunch more information that you might like but for the purpose of this tutorial i want to format it to a date and because i live in europe or more in the netherlands we use the day then the month then the year but you can adjust this to your liking for example you can say to string which is the function i was talking about earlier use brackets use a string and here we can pass our format the form we're going to use or what i am going to use is we're going to use day and then the month and then the year so how did i format this dd stands for day and uppercase m uppercase m stands for month and four times y stands for the year now you can adjust this to your liking like i said before for example you could put the month at the start and then the day here as i said earlier we're just going to use day month and then year now if we want to add the date when the user joined our server then we can actually do the same thing but we're just going to get a different value so what we're going to do is we're going to say at field and then we're going to say joint at now we want to get the context user but we wanted to get the context user as a member of the server and because it just contacts the user isn't actually the user that's from part of the server but it's just the user it's our user we need a socket guild user and how are we going to do this very simple we're going to put one bracket here and we're going to say this is context user as socket guild user and notice how this automatically adds using this quarter web socket so if you didn't get it automatically edit here and now we can use another bracket and we can say here start joint at we want to get the value of when we joined and then we want to transform that value into a nicely formatted date so we can just use td slash mm slash yyyy and then again we need to make this nice field inline so we see true let's save this and run it and see how this works now the bot is online and let's use our command there we go we now have the user id the discriminator the created and the join that now why is this here and not nicely here let's check our code ah i see something and i hope you see it too we forgot to add true here so let's edit right now save it and run it open discord all right so now if we execute the command there we go now you might be wondering why is it now under each other and next not again next to each other why don't we have four columns well three columns is the maximum amount of columns that can be placed next to each other so for example here what it does is because it's three it will create a new line and then add the column right here and so on so if we add more fields they will just stack under each other so if we add one more ball code here if we add another one it will go here and so on now as you can see we now have the user id the discriminator the date when the account was created and the day when we joined the server pretty cool right now let's close our console again and go back to visual studio now let's add even more information let's go here let's say add field and then what we can also do is we can get a list of the roles that the user has so we're going to say roles we're going to use a comma and here again we need to get the socket guild user so we're going to do the exact same as that we did to get the join date so we can say context lock user as socket guild user dot rules so now we have the raws of the user and again we're going to use another function going to use a string function and to do this what we can say is we're going to say string dot join and then here we pass the rules but before passing the rules we want to pass the separator so what we're going to do here is we're going to say two quotation marks and then a space and then use a comma here and then don't forget to add one more bracket at the end and now let's save our code run it and see how this turns out let's run the command info and we only have the everyone role and that is correct because we only have the everyone role in this server so let's go to our roles and as you can see we only have the everyone role and the tutorial role the tutorial raw is from the bot so we cannot add that to ourselves but if we create a new role we're going to call it test make it blue save changes exit it give it to ourselves and now let's execute the command and voila we also now have the test rule now this might not look so nice because you want to like for example mention the raw or we want to nicely show it or maybe even get more information about the raw so let's do that close your console go back to visual studio and you might have noticed in discord that we didn't have a new line but that is on purpose for example if we have a lot of raws you don't want that to be in line because then it's not going to look very nice so we're just going to use a non-inline one you could choose to remove the joint at one or you could remove the discriminator for example and then everything will look nicely for the tutorial we're going to remove the discriminator value and then we're going to use the rules to actually select the mention of the role and not just a name to do this we can say roles dot select then we're going to use a x then a lambda expression then you're going to use x dot mention and this is going to mention the role it will not mention everyone for example but only thing it's going to do is it going to make the road look nice and it's going to use the actual color of the rule to highlight it now don't forget to add another record save your code run it and let's execute the command in discord so let's go here info and tada we now have the raws nicely next to each other and they are now highlighted with a nice ad in front of them but as you can see it did not ping me because the message if i would ping everyone the message becomes yellow and it didn't happen here because it didn't ping everyone it just nicely mentioned the raw so we can for example see the nice color of blue and the background now let's close our console go back to visual studio and this is actually everything we need to do for this nice info command let's make another command and we're going to call this the purge command now purge that is a very very serious command because we don't want all our users to just purge messages and here is something new that we're going to use we're going to use two square brackets and then we're going to say require user permission and then in two brackets we can say guild permission and then dot manage messages this basically means that if the user has permissions to manage messages like removing them then the user is able to use the purge command you can also require bot mission so for example if you want to make a command that requires a specific permission then you could use this but we're not going to use this we're just going to use require user permission because our bot has administrative permissions now make a new line and let's create the task so public async task purge and here we're going to learn something new too now what we want to pass is we want to use for example an amount of messages that should be purged so here what we can do is we can say int amount and this basically means that there will be selected that amount of messages in the channel and those will be removed i will showcase you how this works in this score when we have finished making the command but in the meanwhile let's make everything that's gonna happen in the background here's two curly brackets and between these two brackets we want to get all the messages within the amount of messages that should be requested so what we can do for this we can say var messages equals await context dot channel dot get messages asynchronous and then we're going to pass the amount of messages that should be requested and then we also want to flatten asynchronously all those messages now what did we just do we said get all the messages from the contacts channel and it should be no more than the amount that we've passed now you could also use plus one and what this will do is it will also remove the message that initiated the purge command so that it will also remove the purge message and the amount of messages that has been provided now to delete these messages a simple thing we can do is we can say await and then context channel as socket text channel and then delete messages synchronously and pass the messages that we want to be deleted now this deletes all the messages in the channel that we just requested here now all we need to do is let the user know that it went successfully and then all the set but i want to make something nice because some people don't like it when the success message still appears after the purge they want it to be removed after a specific amount of time again this is optional you could skip this but i would like to make something like this what we're going to do is we're going to say var message equals await context.channel dot send message async now we want to pass the number of messages that are deleted and a simple way of doing this is we can say dollar sign and use curly brackets messages dot count and this is the count of messages that have been requested to be deleted now use two brackets to actually get the count and then use a curly bracket to close it and then say messages deleted successfully now use a semicolon at the end to close the line and now what did we just do we send a message to tell the user how many messages have been deleted but we also stores the message that we sent in a variable now we want to wait a few seconds and i would like to wait exactly two and a half seconds so we can do here is we can say wait task dot delay two and a half thousand milliseconds and then a semicolon now what does this do is it waits exactly two and a half thousand milliseconds before executing anything under this line and this is perfect because we want to wait two and a half seconds before the message get deleted so what we can do here is we can say wait message dot delete asynchronously use two brackets use a semicolon and you're done let's save our code run the tutorial and go back to discord and let's execute the command purge 50. now this will also let us know how many messages have been deleted and as you can see the message gets automatically removed you could see if you look back that there were only 19 messages deleted and that is exactly why we don't use the amount of messages the user wanted to be deleted but we actually get the amount of messages that actually have been deleted so for example if we now do purge 100 it would just tell us one message is actually deleted because that's the message that we used to initiate the command now let's close the console and let's go back to visual studio and this works perfectly fine let's make one more command for this command we're going to call it the server command and this command is just going to return some basic information about the server we don't need any permissions for this command so we're just going to make a task public async task server and we're also not going to pass any arguments now we can use a curly bracket and we can here now actually get some information about the server let's again make a nice embed so var builder is new embed builder here's two brackets enter and then a dot and let's use the logo of the discord server as a thumbnail url so we've thumbnail url contacts.guild dot icon url and we can use this to get the icon of the server we also want to use with description we can say in this message you can find some nice information about the current server now we're going to add a nice title so we're going to say with title now in this title we're going to use a dollar sign at the start and then curly brackets we're going to say context not gold dot name this is going to return us the name of the server and then we're going to say information so for example in the tutorial server it will say tutorial information and then let's add a nice color to the embed we're going to use the same color that we used earlier but you can use different one if you like now here we're going to add a new field we're going to say created add which will return us when the server has been created so we're going to use context dot gold got created add and then we're gonna say to string and nicely format our date we're gonna add how many members there are in the server so i'm going to say member count now to get the actual member can of the server we want to do something like we did before we're going to use contacts not girls as socket gold which is right here and we're going to say dot member count and we're going to use plus and then say members now as you can see we didn't use the brackets and that is always optional but in some cases it might be more efficient in some cases it might be nicer to just use this i'm going to stick with this but you can also use the brackets it doesn't really matter and again we almost forgot but we want to make these fields in line because they're not going to be a very long text just going to tell us how many people on the server and when the server was created and here we can do something nice with the members we can let the bot tell us how many people are actually online and to do this we can say add field online users we're going to say context dot gold and socket guild i'm going to say dot users and then what we want here is we want to select all the users that are online so we can say dot where user bracket user x lambda expression x dot status and then equals user status dot on line now now we have all the users in an array but we want to get the amount of users in this array of users so we're going to say dot count and this is going to give us the amount of uses that are online then we're going to say plus and we're going to use members and again we make this field in line and we use a semicolon now why did i use a space here and a space there when we use plus it doesn't add the space automatically for us so we need to do this manually now if we would have used curly brackets if this would be a bit shorter then we could have just added the space in the string now we just need to do it at the start of the string let's make an actual embed out of our builder so let's save our embed equals builder dot build and then let's send the message that we just made in the channel so contacts of channel send message asynchronously null because we have no text we don't want it to be text to speech and we just want to pass in our embed that's it save your code run the tutorial go to discord and use your new command and there we go we now have some nice information tutorial information in this message you can find some nice information about the current server created at this date member count two and online users one because the only thing that's online is the bot and i am on do not disturb now let's actually get the amount of users that are on online in total and what we can do here is we can simply filter if the user is not offline so let's go back close the console go back to visual studio and here what we're going to do now is we're going to say x and then in lambda expression excel status and then it's not so exclamation mark equals user status start offline this means that we are going to get the account of every user that is not offline save your code run the tutorial go to discord and now let's use it and see what it returns and there we go online users is now two because i am online and the bot is online now if i would turn my status into invisible and we would still use server then we are just going to get one because i am offline let's get back online check if it works and ta-da two members online now before we end this episode there's one more thing that i want to talk to you about earlier we made a info comment about a user but if you wouldn't want to know information about another user this command won't work so let's actually make this command in a way that we can use it on other users by mentioning them so what we can do here is we can say socket guild user and then user equals null now what does this mean we don't want the info command to only work when we mention a user we also want it to work when we don't mention a user when we don't mention a user we want to use the user that send the message while if we do pass a user mention then we do want it to use the mentioned user so let's make a if statement here and say if user equals null so if we did not mention anyone we can just use the code that we just made but if we did mention someone then we want to go in else curly brackets and then just copy paste this code and you want to replace context dot user with just user so we can go here we can say user and we can say user replace this right here replace this and because we made the user a socket guild user we no longer need to use this we can just replace all of this with just user and the same goes for here we can just say user now save your code and run it [Music] go to discord and use info at tutorial or someone else that is in your server and let's see how this does there we go we now get some information about the bot and as you can see it also provided us with the nice profile picture of the bot and not just nothing now we can see the user id when it was created when it joined the server and the roles that it has as you can see it also had the tutorial rule because it has that one and everything works nicely now if we use the info command without mentioning anyone we're just gonna get some information about ourselves really cool right all right guys that's it for today i hope you've enjoyed this episode and have noticed that things are getting a lot easier and we can make some really cool things with c-sharp if you have any questions you can find the discord link in the description you can join this discord server to get some additional support i will be there almost all day to support you with your questions or if you need any advice i am also there to help you out i hope to see you in the next episode have a nice day and until then
Info
Channel: Coding with Efehan
Views: 11,518
Rating: undefined out of 5
Keywords: discord.net, discord, discord bot, tutorial
Id: uOV1rg_ecMo
Channel Id: undefined
Length: 38min 57sec (2337 seconds)
Published: Sat Aug 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.