Discord.NET Bot Development · Database & prefix · Episode 6 [OUTDATED]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys and welcome to the sixth episode of the discord.net bot development series in this episode we are going to work with databases and we're going to allow servers to create their own prefixes so for example we're going to do is we're going to create commands that allow us to modify the prefix within a server and we're going to make the entire infrastructure that's needed around it now if you enjoyed this video please don't forget to leave a like comment and subscribe and if you have any issues whatsoever please take a look in the description down below as there is a link to our discord support server where we are able to help you if you have any questions now let's get started to make a nice infrastructure in regards of making prefixes changeable and that kind of stuff we need a database now things can sound very difficult but they are not the first thing you want to do is open up your browser and search in google for exam now the first thing that pops up is exam from apache friends that's the one you want to go to click on your computer operating system and then download the appropriate file now when the download is finished open up the file and it will guide you through the entire installation of exam it might say this warning just press ok now follow the steps along make sure everything is being selected here choose the location where you want to install exam and because i have already installed it it will not allow me to go any further but for you you just need to follow the steps very easy if they ask you to use some form of browser or offer you any applications just deny decline or whatever options they give you in order not to install this application because you do not want them you only want exam now when you're done you will be able to open up exam search for example in your windows bar and you'll find this application now there are a few options that you can pick there's apache mysql filezilla mercury and tomcat the one we are going to use is mysql what is mysql mysql is a database structure that we will be using it's a completely free database application that we can use in order to store all our data in this allows us to for example store the prefixes of our servers so when you're done installing and you open up exam click on the start button right here this will start the entire system working the first thing you want to do is you want to create a new database for our discord box a simple way of doing this is by clicking on admin this will open up a new page in your browser heading towards localhost phpmyadmin it will not work for me because we forgot to activate one more thing and that is apache now apache is a web server it allows us to run websites it allows us to use php but for now that's all irrelevant because the only thing we need is to access the control panel of mysql which is now working as you can see there are already a few databases information schema mysql performance schema and php my admin but we want a completely new one for our disk click on databases and type in a name that you like i'm going to go with tutorial because this is a tutorial so let's just use the name tutorial but you can choose a completely different one it doesn't matter and press create and this will create a new database for us you also see it right here in the list it says tutorial the next thing you want to do is go to your code editor as you see i got rid of the message id that we used earlier but i can now fill in again you might ask why did i get rid of it that's a good question because as you also can see here are a few blue icons right in front of the files this means that these files are also linked to the github repository that you can find in the description down below so if you have an issue or you cannot really find out what you're doing wrong you can also go to the repository and find the code that we use in the tutorial now we need to create our database how are we going to do this well we could go we're just executing a lot of queries and making things way more complicated than needed but we can also use a wonderful thing called entity framework entity framework it's a bit of a complicated thing to explain but simple way to put it is it allows us to do a lot of database operations in a really easier way than just using curies you'll see in a minute what i mean the first thing we need to do in order to use entity framework is go to your solution right click it add and then create a new project we're going to call we're going to make this a class library but make sure it's net core so search for class library there might be multiple ones from.net core but just pick the one with the green icon so the green c sharp class library.net core press next and here we're going to call this tutorial infrastructure for example it doesn't matter what name you give it for this tutorial i'm just going to call it infrastructure so it's bright and clear what this is so infrastructure or give it any other name that you like press on create and this is going to create a new project that we can create our database in now why are we making a separate class library for our database operations a thing that conflicts with discord.net is the database library that we use for some reason there are a lot of conflicts when you want to use entity framework and discord.net i guess the developers somewhere did not do the right thing but it's also a bit more structured you know you have a separate project for your database operations and a separate project for your discord bot now let's get started with some code so in order to do so let's go to tools nuget package manager and manage nugets packages for solutions now in here you want to search for entity framework this is going to give you multiple results but the one that we are looking for is microsoft.entity framework core now here instead of choosing the template you want to choose the infrastructure and then say install accept the license and the next thing you want to search for is empty framework space sql actually more specifically maya scale but it already appears here what we need and that is pomelo entity framework called.mysql it's made by lawrence meyer and caleb lloyd and yuko zhang so if you're searching for this make sure you get exactly this one because that's the one we need now again select the infrastructure press install accept the license and that is it we need no more other packages now close the package manager and let's recall this class to tutorial context now you can give this a class another name that's fine it's not really a big deal but make sure there's context at the end like i did so your name and then context not cs it might give you this message just press yes it's not a problem it will just replace everything in this file with tutorial context now what we want to do here is the first thing we really want to do is make sure that tutorial context is a db context now it will not appear here because we're not using the entity framework library yet so go over here go to the light bulb and say using microsoft entity framework core now no errors at all the next thing we want to do is you want to make a new database set now what is a database set a database set is basically a table in our database for example you could make a table that contains all the data of all our servers for example the id of the server the prefix that they have all that kind of data that you want to store so people can use your commands properly now we're going to make a db set of server so this is a custom class that we did not yet make but we're going to call it servers and then the last thing we want to do is get set now this means that we can fetch all the servers but we can also modify our servers that's why we use getset now the next thing you want to do is make a protected override void and you want to call this on configuring and then in this we want to pass a db context options builder and we're going to call this options now here you want to use a lambda expression and then say options use mysql which appears right here and in these between these two records you want to provide our connection string but that might sound very difficult what is a connection string a connection string basically includes the password the ip address the host for example that's the op address and all kind of data that the program needs to connect to the database so let's provide every data that it needs and in order to do so properly we need to first provide the server now because the server is actually on our computer the only thing we need to do let's say server equals localhost because it's on a computer the database does not have a password yet which is great because we don't really need one it's on our own computer and it's also not accessible from anywhere else so just say server localhost and the next thing you want to do is user now the user is always root it's always on default root you could change this if you know if some experience with mysql you can change your username and then just use a different username here but for now if you did not modify anything just use root the next thing we want to pass in is a database and that's the one we just created and i call it tutorial if you gave it a different name also pass a different name right here just replace it with tutorial and then we also want to pass in the port which is 3306 now how do i know that the port is 3306 the first way i know is because almost every single database uses 3306 and the second way i know is because if you go to apache and if you look on the ports it was a three zero six under mysql and then the final thing and this is optional but the final thing i want to add is connect timeout equals five now this means that after checking for five seconds if the database works and it got no response it will error why did i change this to five is because on default i believe it is 30 so you have to wait 30 seconds before you might know if there's an issue and i want to know that quickly because if it doesn't work i just want to know it i don't want to wait half a minute i want to wait a few seconds that doesn't work we can fix the issue without waiting way too long now the next thing we're going to make is the class that we just had here it's the server class now in order to do so let's make a new class right in here so we're going to call this public class and then server you also see now that the error disappears and we have no issues whatsoever this is great now in a way to identify the server we want to use the server id that is the easiest way to identify a discord server and all discord ids are u-logs so use a public ulaan id and get set now again this means that we can get the value so we could find the value where server has this id but we can also modify the value and the next thing we want to store is the prefix because we want to allow servers to modify their prefixes now the prefix will be a string so we're saying public string and then prefix we also want to be able to get this prefix so we can check if the prefix is okay and if the command should be executed and we also want to be able to modify the value all right so now what we want to do is we want to push all of this to our database so we get a service table and in the service table we can store all kinds of information about our server including its id and the prefix now before doing this there's a little error that will prevent us from doing any kind of things like pushing that because we have an error in the version you might have this error too you might not have it but in order to solve this first we need to go to template the template project and right click it and then say edit project file now here it will say that all of these have version 3.1.7 you want to update that and change it to 3.1.8 so change it everywhere to 3.1.8 now you go to your package manager go to updates and let's clear the search now select all the packages because now they want to update to 3.1.8 update it and now we're done now we can actually make some nice changes what you want to do is go to tools go to your nuget package managers and click on the package manager console now what we want to do is we want to install another package that will help us push our changes to the database before doing that you want to make sure that the default project here is set to infrastructure like this if you've done that type in install package microsoft dot entity framework core not tools now all the commands that we're going to execute right here will be in the description down below so if you don't want to hesitate just go to the description copy and paste all those commands now the next thing you want to do is say add migration and this basically means that we are going to create a new version of our database what i mean version is for example this is the structure of our database right now but what if we decide to make a different structure later on but then find out that the structure isn't really nice it has a lot of errors and that kind of stuff now the nice thing of migrations is if you have a bad migration you can fall back to a different migration so a different version you can call it a version now if you do so we can just use a different structure that we made earlier and then update our database everything works again so migrations you can see them as versions versions of your own database and because this is our very first version we're just going to call this our initial version now it will build the migration and you will see an error it says the startup project template doesn't reference microsoft entity framework core dot design it's because we forgot something here we want to change our startup project to infrastructure now go back and try and creating the migration again this time it will succeed and it will open up a new file now this is basically every code that it will run in order to create it as you see here it will create a table service which we provided right here and then it will in the server have an id and a prefix now the next thing we want to do is you want to update this database because if you now were to go to apache were to go to your database you'll see that inside of the tutorials database there is nothing to find so now what we want to do is push our changes and to do this there's a very simple command simply type in update database now this will use the migration that we gave earlier and then push it to our own database so now if we go to our browser and refresh you'll see that it now has a table called servers and a table called ef migrations history if you look inside of the service table you'll find that it has as well an id as well as a prefix great everything works out now the next thing we want to do is we want to create our methods that allow us to modify the id and the prefix this is very simple create a new class inside of the infrastructure project so add and then a class and let's just call this servers and this will create for us a new class that we can use so in order to do this properly the first thing you want to do is we want to be able to use the tutorial context so in order to do so let's first make this class a public class and then here what we want to do is say private read only tutorial context and then underscore context now next we say public servers and then the tutorial context context and then brackets curly brackets and then say content underscore context equals context now what did we just do good question what we did here is called a dependency injection this means that we grab this class that we created right over here and then inject it into our file now in order to properly do this we need to go back to our program cs go all the way to here where it says services configure services and the first thing you want to do is say add db context and then the tutorial context now this will give us two errors the first one that it doesn't recognize at db context but in order to do so we can add reference to infrastructure and this allows us to use the tutorial context and the db context the next thing we want to do is say add singleton and then we inject the servers that we just made here this file will now be usable all the way in program and we are able to use the context right here now the only thing that's left is to make a function that is able to modify and get the prefix of our good now what we want to do here is create a function as able to modify the prefix by the guild in order to do this let's make a public stringiness task and call it modify gold prefix now here we want to have two parameters the id of the server and the new prefix that we're going to give the server now in here what you want to do is first search for the server with the id that was passed along here inside of our database so var server equals await underscore context and then servers because we want to use the table service that we have right here and then within the server we want to find a server that has the id of the provided parameter now again this will be red hover over it use the light bulb and say using system threading tasks now we have a server but what if we did not yet store the server inside of our database in order to do so we want to create a new record inside of our database now if the server is null which means that there is no database record of the server we want to create a new record so we say underscore context add and then a new server now it won't find this because i made a very stupid mistake excuse me so let's go here then get rid of this and put this outside of the public class tutorial context like this i am so sorry for the confusion now it will find it and what we can do now is say the id should be id that was passed on earlier and the prefix should be the prefix that was passed on then we're going to go view the curly brackets a bracket and a semicolon if this is not the case and the server is already on our side of our database we just want to modify the value now to do so the only thing we need to do is say server dot prefix equals prefix now the last thing we want to do is we want to save the changes so say context dot save changes asynchronously and this is pretty insane this is how easy it is just to update and find something inside of the server now the next thing we want to do is again make a task that is able to give us the prefix of the server so to do this we're going to make a new task and we're going to call this public async task get guild prefix and the only thing we want to pass in here is the id of the server now what we do is we say var server equals await underscore context servers and again dot find async and then the id but we don't want this just to be a task we want this to return our prefix so instead of just using task we want to use this task with a return type of string because it should return our prefix now the next thing we want to do is we want to get the prefix from this database and we could just use the server variable here but we can even make it more easy you see what you can do here is we can also say where and then x so a lambda expression and then the id should equal the id of our server this means that in the database it will search for all records where the id is our id but next we only want it to select the prefix so we say x lambda expression and then the prefix and last but not least we want to let it select the first or default result and we want to use that asynchronously just like this now what this will do quick going over this again also make sure that you press the light bulb and say using microsoft entity framework call like this now a quick recap is what this does is it goes through all servers but it only selects the servers that have the id that we have passed right here then it will select the prefix and then return the first or default record that it can find now the only thing that's left for us to do is say return await task.from result and then the prefix that we just found now we called this variable server but let's change it to prefix and let's call this one prefix 2. so now we can just use prefix now that's it this is insanely simple for c-sharp to just make um modifications to the database and get records from a database with just five lines here and just about eight lines here that's insane and it's really nice to be able to do this now we could check this but we could also instantly make some commands to see if this actually works and let's make that let's make some commands and see if it works the first thing we want to change is go to our command handler which is right over here and then what we want to do is instead of just always checking the prefix to be the prefix that we have in our config we want to check if the prefix matches the server now in order to do this the first thing we need to do is go all the way up and what we want to do here is we want to add a new private read-only and then servers and then underscore servers now also here what you want to do is add at the end servers servers and then again here underscore servers equals servers so what we just did is again we injected the servers into our class and it allows us to use the methods that we made right over here so now what we do we want to fetch the prefix so what we do here is we say far prefix equals await underscore servers that's get guild prefix and then the id of the server that the message comes from to do this we should use the socket guild channel that the message was sending so a message.channel as socket guild channel then the build and then the id now there is a chance that the server that we're trying to get the prefix from doesn't even have a prefix in the database or isn't even in the database and in that case what you want to do is we only want to use the standard prefix so use a null call scaling operator and then say exclamation mark now what this does is if the value on the left is nothing it will use the value on the right which is an extermination mark now the final thing that's left to do is replacing config prefix with our prefix just like this and now the system works now the last thing we want to do is we want to have a command which allows the server owner or admins to modify and view the prefix within the server now let's do this very simply let's go to the modules go inside of our example module and here let's make a new command and let's call the command prefix now we want users that only have administrative perms to be able to execute these commands we don't want any people that don't have administrative permissions to modify anything of the server so in order to do this you want to use a special attribute which is called require user permission and then two brackets and then say girls permission dot administrator now this means that if the user has administrator permissions then it will be able to execute this command if the person does not have this permission then he or she will not be able to execute this command now here we make a new task so we say public async task we call this prefix and here what we want to use is a string prefix but this should be optional so let's make this equals null this means that if you just use prefix i want to show the user the prefix and if there is a prefix then the prefix will be adjusted to the new prefix so let's start with the following let's say if the prefix equals null then we should first just give the prefix back to the user so var prefix equals and now you know how it works just the same way as in the command handler we first want to inject it so private read-only servers underscore servers then here we want to add servers servers and last but not least we want to replace this lambda expression with this and then inside we say servers equals servers so now here what we do is for our prefix equals await underscore servers and then get prefix by guild so gets good prefix and then here we say context guild id so this basically fetches in the prefix from the server and it will store that into a prefix but because we already used prefix as an argument name we're going to replace this with guild prefix now here also the chance exists that the server does not yet have any prefix stored inside of the database or that the entire server is not even stored in a database so in that case if nothing is being returned from the database we simply use an examination mark just like this so two question marks then a string and inside the string you put an exclamation mark now the only thing that's left to do is to say await context dot channel dot send message async this is what we have been using a lot but instead of using this you could even use reply async which is a lot shorter and it's also a lot easier now here what we do is we say a string and we put a dollar sign before the string because we want to use a parameter inside of the string and we say here the current prefix of this bot is and then we use kills prefix as our variable now a nice way to format the prefix is to use this character i have no idea what's the name of it but just use this it will be in the description down below and this allows us to make kind of a nice format around of the prefix you'll see in a minute what i mean the last thing we want to do is we won't just want to return because there's nothing else to do we fetch the prefix then showed it to the user and then we want to return now you could of course make a nice embed of this that's entirely up to you but for now i'm just going to make it a simple message because that's not the point the point is that everything works now if there is a prefix which means that the prefix is not null because if it is null it will return right here but if it isn't then we can do whatever we like here and again remember from the previous tutorial that means that we did an early return statement so we did an early return right here which allows us to just do whatever we like here if the prefix is not null in that case what we want to do this is so easy the only thing you want to do is say await underscore servers modify guild prefix then we want to provide the context guild id which is the id of the server and we want to provide the new prefix just like this then the last thing you want to do is say await reply async and then you want to say a dollar sign at the start and say the prefix has been adjusted to [Music] two of these and then prefix thought and and semicolon now that's it the only thing that is actually nice to do is to make sure that the prefix isn't too long a i think a one of the most used length of a prefix is around eight characters as a limit i believe i also use eight but this is also up to you you could set this higher for example 11 whatever you like but i'm gonna stick with eight i mean would like in prefix that's even longer than eight characters so in order to do so what you do is you say if the prefix dot length is greater than 8 you want to send an error so we say wait reply async we say the length of the new prefix is too long exclamation mark again use an early return statement and now what this means is if the prefix is longer than eight characters it will send the error and then return if there is a prefix and the length is shorter than 8 characters then it will do this now let's test it out now again here as you see we set the starter project as our infrastructure but now we want to change that to the template or whatever the name of the project is that you use for your discord bot i'll run the template head over to discord and let's see if it works so as you know if we execute exclamation mark prefix it will give us a message telling us that the current prefix of this bot equals exclamation mark but let's say we want to change it let's say we want to use the question mark as a prefix now it says the prefix has been adjusted to question mark now let's see what happens when we use an exclamation mark again so examination my prefix it will do nothing because the prefix is no longer the exclamation mark it is a question mark and there you go everything is working perfectly now if i want to change it again i'll just make it something different one two three it will become one two three if i want to change it again i can make it a dot it will be a dot if i change it again i can do this get the prefix i can change it again and that's how it works now let's say i try to make it way longer than i should the length of the new prefix is too long and if you use the prefix again it will not be adjusted now let's close our discord part and go back to visual studio i hope that your opinion based on databases and how advanced they might sound on how difficult things might be is now changed because i've shown you with just a few lines how simply you can manage to update delete modify data and that's amazing entity framework is amazing and i hope that i've shown you that you should be using it a lot if you have a project in c sharp that could use a database and it is working perfectly nice they use entity framework core it's amazing and its capabilities are endless now in the next episode we're going to work a bit more with databases i will be showing you how you for example you can use ranks how you can have auto rules so roles that will be added when a user joins your server and more for now i hope you've enjoyed this video every additional links such as the link to exam or anything that we've used in this tutorial including commands that we used earlier in the console will be in the description down below if you've enjoyed this episode don't forget to like and subscribe because this is definitely not the last episode have an amazing day and i'll see you later
Info
Channel: Coding with Efehan
Views: 4,950
Rating: undefined out of 5
Keywords: discord, discord.net, discord bot, tutorial
Id: dnvINSXHA7s
Channel Id: undefined
Length: 35min 0sec (2100 seconds)
Published: Sat Oct 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.