Part 1 - How to Create a Discord Activity Suggestion Bot in C# with Discord.Net

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay good day um today we're going to be creating a discord board and we're going to be using the discord discord.net library which is like a [Music] api wrapper for the discord uh apis um this is a very big library we're not going to touch everything inside the library we're just going to touch the surface of the of the library there's a lot you can do with it you can get the documentations uh the documentation from this url so i'm going to share it with other links and then the next link which you should have is this uh the discord developer for slash applications this is where it's the developer portal where you register your new applications and then the other link which you're going to be using is the discord api.com this is where this is a permissions calculator for this card um and we're going to be using cshop basically create a bot now the first thing which i want you to do is i want you to visit this url and create a new application call it um call this or suggestion correct solution board so that part is going to suggest activities which you can do when you're bored yeah um an image for that we don't have one we can get a bought image um i think it will be cool forget the headboard no let's get let's just say um so i'm going to download this save it there i'm sure they don't have any copyright thing is going on but good one which goes along with this code if possible but yeah that's fine okay now let's um let's add our picture um it's not there let's range turn to just download it we did it's called download it's inside downloads there it is okay and then let's save our changes the next thing which you have to do is go to the board tab and then add your port agree to that um everything here is fine and what i want to do is i want to give it administrator permissions take that and if you want your board to be um added by anyone to any server keep this on and the next thing i want you to do is i want you to go back to general information copy your application id and then go to the discord api permission and select administrator and paste the client id then this is going to generate this link and when you click on the link it's going to ask you to and they add your buttress so i'm going to add those bot to the session kit 7 i'm going to continue i'm going to authorize it as an administrator and continue um this is an empty server it's just for testing it's fine um then once it's authenticated as you can see it's joined my discord server and now that we have we've authenticated it it's time to program it i'm going to open up my console i'm going to navigate away from this project and i just did the video i'm doing it again i'm going to go to this folder i'm going to create a new.net application suggestion board and then wait call it suggestion but i'm going to output it into source suggestion but um so let's navigate into that and then in the source in the root fold of the project on to and then you'll get a file and then that's added and then once open visual studio code here so now this is our project looks so so sold out with the suggestion but and then inside our our suggestion but folder what i want is to let's make sure that my vs code knows i'm using c sharp and that ominous sphere is running as you can see it's running and then once that is running we'll need to install a few packages you need to have the then you get nugget gallery extension which i use to install packages and how do i use it um i just uh open it here and then it's on to list all the packages which i need or i can also search for them the first package which you want to need is the dependency injection extension so i'm just going to select the project i'm going to install it into and then install it i need you to download the latest version and then once that is installed we'll also need um rations so i want to install that as well okay that's installed and the next thing we also need is the binders i want to install them as well and finally this package wait not finally but one last important package the discord.net library and then we just want to install it to the project install the literature version i'm using 2.4.0 yeah and then i think we're good to go now i want to get that in there first so my program files empty uh first thing we need to do is set up the configurations on discord but i'm going to create a class called discord namespace for this [Music] let's create a class called it fingerling or configurations just called configuration um let's rename that as well what we need um just keep that and then i'm going to create the absolute links json file create a section called okay this one i'm going to start my talking and where will i get this token i'm going to go to browser and then you want to go to your board and from your bot you just copy the token i won't show you mine i'm just going to move this put my token there and close the file come back and then now that i've got the token i'm going to extract the token from the app settings file to do that we're going to have to set up the configuration build so we want to call this escort config right so i'm going to use configuration builder this is going to be in the extension we installed and i'm going to set base path to my directory current applications folder and then add a json file called and then i'm going to get the section of this card and then i'm going to extract the token between this let me format this my printer is not working currently um giving me the best developer experience so i'm going to import this from there um then now that we've got the configuration the next thing we're going to do is let's create a function facing don't call this tony so i'm going to say first thing we need to do here is we have to create the discord length um we have that now the next thing we have to do is add command service call this commands command service this class is found with the swap commands and then once we have those two things the next thing we have to do is set up our ioc container so continue for the services and then i'm going to create a new service collection this um it's from that namespace and then we're going to register our client as a single time and then we'll build service provider yeah um so we're going to get the commands from allah so now that we have this um our ioc container setup what i want us to do is want us to subscribe to the log log event of the client so to do we are going to obviously load it and since this is synchronous just have to tell our tasks that it's complete then oh the name space for this is inside this code space the next event we need to subscribe to is the message received event um this generate this method um actually let me let me just do some part here somewhere it did with i'll just make it async get rid of that and then let's push that get rid of that now what do we do with this message so basically what we're doing here is we subscribing to this event whenever our client receives a message from any of the channels which our bot has been registered in it's going to first check if the message is from a user and then if it is to check if it's not the bot and also if it's not a bot and then we'll have to check if the message is directed to the spot because in a channel anyone can send the message it may not be directed to the bot so we'll have to check for that and then we will have to reply if it's directed to the bot so now um what we're going to do is um let's check if the message is directed is not the bot i'll do this first we need to cast this as a what you call a socket user message and then we check if it's um it's on my bottom let's get started i'm going to call this h and then let's check if home message is not null that's very important and then if it's not we which i give the author of the message okay so if it's not or that has about just to return but don't do anything and then if it's not we're going to create a new context um a new sort of context so second command context we want to pass in our client and then also pass in the message and from here the starting position to read the message from zero and then you want to check if the message has a string prefix of this this is going to be a flag which tells the bot that this message is for but it can be anything in this case we just want to use the exclamation mark it's very popular and take our out position if it has a prefix of this then what do we do we're going to reply to it um call this reply await and tell our commands any command which is called which is applying to response age personal services and then if our reply is not successful what do we do first we want to just console.log it down um just going to console the reply error and then if command condition what do we do uh okay um so now we're done with that uh the next thing which we just have to configure now is what to log in and then also once it's logged in we're going to start the bot and then we register all the commands into our ioc container so let's start with telling our client to login asynchronously and the token type it's bought and so the token which we want to get from our configuration is that and the next thing we tell them what to do is just them let's add more things i think from our current assembly so let's import uh some different reflections and then we want to get the entry assembly now let's um delay let's end this um we're done with the configurations setting up but now the next thing which is the cool part is just starting to add modules so i'm going to create a board call it board suggestion module suggestions and then we want to create a command called suggests uh hmm uh this module basis from this column command this is going to be also get common context and of this command so just something um so our reply zone to a task let's adjust async um to check if this is working i'm just going to send out that apply to do something yeah i'm going to build a project let's see if we did everything right it's it'll build now let's run it let's head over to this card um it hasn't started running yet so there it is it's connecting connected so now it's logged in and it's ready so let's open this card i'm going to call the button to suggest something and yeah it's it's working so it says go to something so yeah how bought is working um we could stop uh the video here uh the next part of this video is we are going to get actual suggestions from from an api so but whenever you tell the bot to suggest something it's going to fetch the suggestions from in [Music] from an actual api and then it gives you back the feedback so for that i'm going to just pause the video here and then start that on second on the second part of the video
Info
Channel: C Sharp Kid
Views: 114
Rating: undefined out of 5
Keywords:
Id: Oz65AF4gRyA
Channel Id: undefined
Length: 28min 36sec (1716 seconds)
Published: Sun Oct 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.