How to write your first discord bot using golang - tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey i got something for you do you want to know how to write your first discord bot using golang then you came to the right place stay tuned hello and welcome to my new video i'm going to write myself a friend well a discord bot which is actually quite the same i got this idea at work where we have this counting bot this sounds boring at first but it's actually quite fun um you see somebody else type in the number and then you have to increase it by one which which leads to a race where everybody is typing the number very fast and um well somebody always can you even say that somebody fat fingers the wrong number and um yeah you get the idea so then you have to start all over again so i decided to write myself something similar i call it what it does is very simple it lets you challenge other people and accept challenges by other people after that you will get into an epic battle and the winner is determined by pure luck so you never know who will win there will also be a leader board ranking each user in terms of total wins so you can always see who is the best fighter this video will have two parts in the first one i'm going to write the game engine and in the second part i'm actually writing the discord bot i've never done this so i'm really looking forward to do so now that we know what to do let's get coding first i define the fighter the username is used to make the connection between the fighter and the discord loser later on it also has a health value this is the amount of health that one fighter needs to chunk down before the other fighter dies i set it to a default of 100 hp just because i felt like 100 seems like a good number you can ignore the strength value for now i wanted to have a base damage and some sort of luck based bonus damage or so but then i realized that the base damage was already a random number so i decided to leave the bonus damage out and forgot to remove the strength value before recording as you can see the attack is basically a random number between 1 and 20. if a fighter attacks the other fighter the damage value will be randomly generated in a specific range i also wanted to see if the damage is actually random so i created the fighter in the main file and let him attack 5 times only to find out that the damage isn't random after all but after a quick search i found that the solution is very simple i just forgot to give the random number generator initial seat after setting it bam random damage now we need a way to evade an attack so if one fighter attacks there needs to be a chance that the other fighter does not get hit let's get these fighters evasive ninja style i added an evade function which is basically the same as the attack function later on i will call the attack function of fighter 1 and the evade function of fighter 2 and see which one is the higher number the higher number will determine if the attack was successful or not i also added a deal damage function and it is that function these are pretty self-explanatory to test my code i added two fighters in the main function i let fighter 1 attack and fighter 2 evade after running the code a few times you can see that the values are random and the damage was never high enough to let one fighter take lethal damage so the fighting functionality seems to work now we need to stitch it all together and let these fighters have an epic battle and see which one is going to be victorious and which one is going down in chain the fight code is pretty straightforward i let fighter a attack and fighter b evade then they switch turns and fighter b attacks while fighter ae waits after that i check if one of them is down and return who has won and who has lost after running the code a few times you can see that player 2 definitely has the upper hand finally time for the fun part i'm starting with a fresh discord account with exactly zero friends so let's change that and create a bot like that would even help with my zero friend problem but anyway if you want to create a bot too just follow these simple steps since the bot needs to live somewhere the first thing we need to do is to create a server simply go to the add server button then select create my account for me and my friends discord is even kind enough to suggest a name for our server click on create and click this away as you can see we are now connected to our new server which has a general text channel and a general voice channel nothing fancy but it gets the job done now let's create our bot to do so you first have to create an application you can do this by going to this url discord.com developer applications there you can see all the applications that you created just make sure to be logged in in the web browser with your discord account there you can press new application and enter a name since this is a test bot i'm going to name it golang testbot hit create and there you go you have created your first discord application now it's time to create the bot you can now create the bot by clicking on the bot tab then add bot and yes do it congratulations you created your bot to actually talk to your bot in your code you need two more things first you need the token this can be retrieved by clicking copy here make sure to store this token in your code you need it to be able to talk to it after that you need to set permissions for your bot just go to oauth2 under scopes select bot and under bot permissions select the ones that are relevant for you since i'm just testing out i think sent messages will be a good start for me now to actually invite your bot to your server go to the link up here and open it in a new tab then select the server you want to add your bot and then press continue it will show a summary of the selected permissions so this looks good press authorize to add your bot to your server yes i'm human there you go you added your bot to your server so if you go back to your discord client you see that the bot appeared and it is offline so we are good to go to actually talk to my bot i'm going to use this library if we scroll down a bit we can see in this usage section that the client creation is very straightforward we simply need to pass the auth token and we are good to go so let's give it a try i just browsed the examples folder and found a very simple ping pong example this shows how to connect to the bot as well as how to send messages from the server to the bot this is exactly what i'm looking for on the left side of the screen you can see that i've already downloaded the library and the out token from my bot i'm ready to start now and let's code this bad boy and see how it works okay i'm done now so let's give it a try the server is running now let's go back to the discord client and hope that the bot answers yes it does very nice let's go through the code a bit the first few lines are simply the client creation using the token we got during the bot creation process this gives us an instance called dg which we can use to talk to our bot next we add an event handler this is the part that listens to what we type in the chat box and responds accordingly this is the function we will later fill with all our fancy schmancy commands this line is needed for the initial handshake with the discord server to identify yourself you need to send some intents there are a lot of different intenses but to be honest i haven't quite looked into this yet so i just copy paste it from the example after that the connection is established and we close as soon as we get any kind of termination signal now it's time to actually implement the commands for our bot if you have a look at the comments you can see that i've prepared a list of the commands that the bot will listen to so for instance if you type in exclamation mark challenge bot the bot will listen to this command and respond with a fight each command will have a specific response first there will be the exclamation mark challenge bot command the next commands will be the challenge and the accept commands these are needed if you want to challenge other users and accept their challenges to see your open challenges from other users you can use the challenges command this will show a list of all your open challenges and of course we all want to know who is the king of the hill if you are typing exclamation mark leaderboard the bot will respond with the leaderboard who would have guessed last but not least i will provide a help command this will also be the default if someone types in some random command that the bot does not know so you get immediate response on how the bot works so let the fun begin i just finished the first command so now let's see if it works darn he won but nevertheless the code works this is good let's go with the challenges code i have a challenges truck containing a map of all open challenges for each user of course i also have a new challenges function which will create a new instance of challenges the add challenges function does exactly what it says it takes to users and adds a challenge for both users respectively the remove challengers function is needed to remove the challenge from the open challenges map after a fight has taken place then there is a helper function which checks if a challenge exists at the end i have a function showing all open challenges for a user if no challenges exist i return an empty slice in the discord go file you can see that i implemented the first few commands after figuring out i have no one else to test the challenges with i added a dummy challenge at the top of the connect to discord function you can see that i added a challenge between me and the random guy this is so i can test their challenges and accept commands now let's finally see it in action okay let's start the server and type in exclamation mark challenges okay as we can see i have an open challenge from random guy let's accept it and okay i won nice now that the challenges part is working the leaderboard is the next part let's get back to work let's quickly go over the statistics code that i used for the leaderboard command first i introduce the statistics struct which will contain all the wins and losses for each user then there is a statistics struct this is used as a return type for the get statistics function i will show in a second and as always a new statistics function to create a new instance same ultimate the first function is the add statistic function this increments the wins and losses for the winner and the loser by one irrespectively the get statistics function iterates over the wins and losses maps and fills a helper map the stats map with the statistics struct i was talking about then i stripped the keys from the map and sort the slice by total wins in the discord go file i added the statistic instance as a global variable the only thing left to do was to add a statistic after each fight this needed to be done here and here now it's time to get some action i guess you already saw on the right side that the leaderboard command is working but i wanted to show you how it looks like with an empty leaderboard let's type in exclamation mark leaderboard and as you can see it's empty but if we accept the challenge from randomguy and type in the leaderboard command again we see that he is actually the first place after beating me now the last thing we need to do is to implement the help command and set it as a default response the code for the help command is pretty much just the concatenation of strings explaining each command let's start the server and see if the help command works exclamation mark help show the help okay mark leaderboard still shows an empty board good and if i type in the random string i get a help response as default perfect this video was a real pleasure for me i always wanted to ride the discord board and i finally got the chance to do it and it was quite fun if you want to see more of this discord bot or the battle bot please let me know in the comments down below then i will make it publicly available for everybody so you can use it at work thank you all for watching if you like this video smash the like button subscribe to the channel ring the bell give me some love and until next time keep on coding
Info
Channel: Thomas Langhorst
Views: 330
Rating: undefined out of 5
Keywords: golang, discord bot, tutorial
Id: e2fFMAPzZs4
Channel Id: undefined
Length: 12min 16sec (736 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.