Discord Bot with Rust ft Prince Wilson - Colbyashi Maru

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right hey team welcome to kobayashi maru the show where devs all over the world boldly face off with the most logical tools on the web i'm colby fayok your space jelly commander and your host of today's challenge today we have prince wilson prince is a web app developer at new zela you can catch him on his stream on twitch right over here and also on twitter and let's bring him in all right hey prince uh do you want to give yourself a quick intro and anything you'd like to add there yeah uh hello everyone uh as you've already mentioned i'm prince wilson i work at a company called new zela i twitch stream every tuesday at 6 30 pm eastern standard so about three and a half hours from now but on tuesdays and yeah i think that's really it uh i'm a person who likes to learn about a bunch of different tools i love teaching people how to do tools so i felt like this was a great opportunity absolutely i'm excited too because i haven't actually used rust before so it should be it should be interesting but uh prince i hope you feel special this is officially the 10th kobayashi meru stream yay thanks everybody so far who's been able to hang out and hang out with us but uh yeah if you're new to kobayashi maru our guest will have one hour to complete their project challenge through that time prince will try to talk out loud as he's working through the challenge and also be asking questions along the way so if you have a question make sure to send it over through chat so today prince will be facing off against building a discord bot in rust if prince doesn't finish the challenge all is not lost we'll see if we have enough time to finish it up or we'll just chat about what the next steps would be so prince before we get started i'm going to bring in your screen and do you want to talk a little bit about the tools that you're going to use just to kind of give an idea before we dive in yeah so like we said uh we already started is we're going to use a language called rust it's very different from what we might be doing normally on kobayashi my room but uh i would say like for the most part i'm going to walk through everything that's going on we're going to be using types if you've never heard of a type we're going to be talking about that and then also we're going to be using a service called discord uh i don't know if you have a discord server colby i do i do perfect so it's a small but humble discord server i i'm here for it but so we're going to be using a service called discord and we're going to be building a bot on top of that one of the things is rust is not the only language of discord supports for making bots if you are like more of a fan of writing in javascript totally down for that too so just something awesome awesome well so if you're ready to go i think we can get started does that sound good yeah it does let us do it so normal i'm gonna switch the screen and i'm going to run the start command and let's hope that my web hook actually starts it oh my gosh this is so exciting we're getting ready to try all these new things and it didn't okay let me refresh the page click to see if uh it's totally fine still not doing it okay let me give one more wait it is doing it oh it is doing it it is doing it oh my god we're we're watching it live here colby nice okay well you're on the clock now i guess my my one screen just wasn't updated so we're good okay we love it we're here for this so like i mentioned before we're gonna be rioting with rust um if you haven't used rust they have a bunch of kind of like what are the things to use it for such as command line tools webassembly networking abandoned uh with installing rust we use a tool called rest up which basically does all the hard work of actually installing everything for us i've already done this step but if you're like learning or more interested in doing this i highly recommend going through the rest book later on but to not waste any more time i'm just gonna dive right into everything that sounds good let's do it i already have a nice little folder for us we're gonna be using a tool called cargo similar to like uh node and mpm on yarn cargo is our package manager inside of rest so whenever we want to start a new project we'll just say like cargo new and the name of our project we might call this like the test spot i think that's like a fair name and once you've done that you'll build your binary project to not get too deep in it there is like the difference between like a binary application and a library similar to like you know we have like react is a library where it's by binary you might actually execute and run something um this is kind of the differences between those two but now when we look inside of our folder here we'll have a new folder called testbot and so let's go ahead and open up testbots another screen one thing to note oh it should it should be fine uh one thing to note is i use a tool called rust analyzer it's kind of like um here's like how the types work inside of this language it kind of gives me hints about when something's going wrong it's kind of nice hi there kind of like type scripts like the hints that typescript i think similar because it's using the language server so like typescript kind of gives you kind of good insights because there's like an intellisense from uh vs code that basically says like here you're trying to do this thing and you can't actually do it and that's like a new working project inside of russ that's making it so much easier for folks interesting hello hello all right so main project that we got going on here we're going to do some changes inside of here the first thing to do actually let me even also let's start from the beginning we have serenity serenity is the library we're going to be using specifically helps us to do all of the management with regards to like discord and rust it has a it has a bunch of examples also in here we're going to go through one of the examples which is like the basic implementation of your rest bot and then we're going to swap it over to what is known as the command framework just because it makes it a little bit easier so to in order to use serenity we're going to install it so we're going to go into our handy dandy cargo tommel cargo tommel is similar to like your package json except that instead of it being json it's tamil format and so we'll have serenity is equal to 0.10 oh interesting are there any specific like benefits to using the tamil format or is it really just personal preference i think there's no if you know that's a great question i actually don't have an answer that i i'll be i would be speaking like out of pocket being like ah yeah this is like definitely it i think as long as there's a structure it doesn't matter which one you use um some of the nice cities is that you're more flexible with what you can do so like sometimes you might not be able to do certain structures that you could do in json or something you could do in tamil but you wouldn't be able to swap them makes sense that's a good question something i'll think about because i have never actually considered like uh huh what would what would happen if you did this instead um what we also need to do is i think we might need some other features but we're going to test we're going to see what happens so in order to get our little terminal going we're just going to do cargo build cargo build just like runs a project it basically makes sure that we get all of our dependencies and make sure that it compiles that's the difference between something like javascript and something like rust is that there's actually a compilation stage ah cool so there's some error love when it starts off with the error it normally doesn't have a good sign it doesn't have like a big chunky error like this uh usually this happens depending on the version i'm using so maybe i'm going to try one more thing i noticed that the version inside of here and below it's a 10 but right here the latest is 10.4 and it's actually done 28 days later definitely a gag to the game this is the video game another video game what is it the movie um i was wondering if that was a reference yeah so i noticed it's still failing i'm gonna try one more thing so one thing that's helpful sometimes you you know you're installing something you're like okay well you have these examples they should be working out of the box so what i typically do is if i see any examples most of them always will have like a cargo.com and i actually use the cargo tunnel to kind of give me insights like what are the things i might be missing so like the difference is here is that in serenity they mentioned like the path as well as they don't install all of the things out of serenity they just install a few things so this default feature says don't install all of the things instead install these things and so what i'm going to do is i'm going to actually take this part of the code i'm going to swap that out and that might be an exercise for other people if they were like interested into like doing some open source you'd be like hey i think this might be wrong uh yeah easy i love these little bits though because it like it helps other people debug like the same kind of thing so this is great exactly so now we're going to be doing the dependency building again let's see if that goes through ah thank you you caught my version already and so i'm gonna wait for this to do its thing it might install it might still continue to install a bunch of other stuff that doesn't work i have one more thing i literally just did this so i was like oh you know this should be fine but of course clearly when you try to put on the clock you're not ready for it there's something about that clock if nothing else i have i have alternatives all right last test so tokyo is like a framework that we use specifically for like handling async stuff all right one is breaking let's see ring is typically like a process we shouldn't do it we put that timer on and all of a sudden everything that everything wanted to work and then stopped working so strange let's see hmm let me try one more thing what could it be i made sure that i have all the dependencies correct ring is a dependency that's being misdone i have the right version i have the the correct defaults let's see try it one other step i wonder if serenity version default features features this yeah nothing's popping out like obvious yeah it's very strange i haven't done any new updates to my language i haven't done any new updates to this to my installations i in fact made sure to run the installation like made sure everything in a clean build would have worked so now we are we are triaging ringing while you're thinking about oh yeah okay you saw the chat yeah yes so ring is a is like a tool you'd use for cryptography and so maybe maybe what i'll do cargo ring uh rust let's see if i can find that yeah so like ring is typically used for uh cryptography and you're using that typically in your libraries for anything that's due dealing with like requests and so that's one of the dependencies in there and that's what i'm thinking about is maybe let's just delete the lock and see like what happens so maybe like it got out of sync and we it's actually pulling the right dependencies yeah if that's the case i guess i was just going to say no language is prone to lock file issues exactly like nothing nothing is nothing is safe hmm it says architecture 786 i wonder if i'm not targeting the right architecture do you have to be on like a certain rest version or something hmm the i don't think i do so i in fact uh did this same rust version before okay and that's why i'm perplexed because i'm also haven't changed this like hmm one thing we could do is we could go into our let's try another shell oh we're ready i don't think yeah i wouldn't think that would have fixed it but that would have been very strange if for some reason like two different terminals were right were not working this is a big old error seriously failed like linking with cc arabic interesting interesting okay so this well this is an issue back in 2015 hmm hmm i wonder if i did something to my cargo but i literally haven't done anything different so the second link on the google search there's an answer in there i don't know if that's worth looking at um there's one that has a bunch of emojis on it trying to have emojis interesting have you have you read the last i don't know if that's related at all so that's similar to what i was saying also before is that and this is what i was thinking is like for some reason what i used to compile so like rust has a compilation stage and it can compile the different targets in different places and for some reason the thing that targets normally my mac os machine is no longer the default target i think that's what's the same hmm very strange very very very strange that has a bunch of things i wonder if someone is you normally build like this oh my gosh you need to add the linker the simplest situation yeah i said this would change like so i was hoping we wouldn't have to do this at any point but it's at least worth noting what's happening so inside of rest you can tell like config you can tell it like what is the actual configurations you want to have so cargo slash config basically says like here you can set up these particular configurations to happen so when we're targeting so when we are targeting and i just missed it we go when we're targeting apple basically that's what the architecture says in there do these particular settings that's what it should do let's fingers crossed that's looking great seems like it's yes nice github to the rescue shout out to github issues you know like it's really coming through i don't know why that happened i cannot explain to you because i had literally done this before we started or even the other project i'm using right now uses the same exact setup i did it from a clean build and all of a sudden just didn't want to work that's so weird and has this ever happened before have you guys so funnily enough i was messing with my linker a while back but this has not happened any other rust project that i've done so that's why it was very peculiar but before i have these like nano leaf lights and i put basically a controller for a raspberry pi to allow me to do all my configurations so like on my stream when you like subscribe or you use your channel points it actually changes the lights behind me that's awesome yes and showing me up on my stream i don't have these lights that can change well tomorrow hopefully uh i will be working on learn with jason and we'll be doing some event sub with event sub which is like a new api that they have and maybe we'll do some light stuff directly that's awesome we'll see if it works one more thing that could just potentially go on fire perfect so notice like we did all of our compilation step we did also like it says finish the dev so like whenever you build something inside of rest you'll have a new folder called target and depending on what type of build that you did whether it was like a um like a development or if it's a production you'll have different folders inside of here so in our case all we did was we had a development one so that's gonna be our debug and there's actually like this like test slash bot this file is actually the executable file that we created which is different from something that you would do inside of like um in javascript you wouldn't have like an executable file you have like your node.js that you invoke this is actually the file we can take anywhere right now at least on a mac os and you can actually have that on any other operating system or i should say any other computer with mac os and then you'll be able to run it that's awesome yes perfect perfect that was scary for a second we got past it though nice yes so what we're going to do is we're going to take a step to just using serenity um there's a lot of little small things that it's doing and so i'm going to walk through each step step along the way that it does did you hear that sound i did hear that sound so that means it's 45 minutes left oh okay we got this we got this that's the first time i had sound effects on this show so hopefully it wasn't too loud for everybody tuning in it was a nice little i could i could hear in the background i grabbed it all from a star trek sound board so [Laughter] i love it um so i was mentioning before one of the tools that we're using for this is gonna be serenity which is the actual library another one is tokyo which handles a lot of the async nature that we're trying to do rust when like serenity first started up didn't have all of its async tools kind of built in inside of the language and so tokyo was the thing that kind of helped people kind of figure out what is asynchronous kind of communications working like now that's kind of since changed but we're still in this kind of middle area where we got two things going on in order to use um in order to use our tokyo we've already installed it and we're gonna use something called a macro so macros are things like this particular word has like a pound sign in front of it and we're just going to denote that the this main function is actually going to be where everything starts for asynchronous bit now we can like call this main function as an asynchronous thing the the next few things i'm going to do are i'm going to write this in like code code pseudocode is this what i was looking for we're going to need our token grab our disk or token start up the serenity client and then we're going to need to just basically communicate that it's caught any errors or anything like that along the way and those are kind of the primary steps nice so what we'll do is we are going to grab our token let token equal and you'll notice this is something i mentioned before is like you'll notice this like parenthesis that's actually being written by rust analyzer it goes and kind of tells me the type along the way of what am i using and how is it going to get it discord token and if we don't have our token we'll say something like please add your discord token to the environment so that way i'm not committing my my discord token i'm gonna show my discord uh token here i don't mind that it's being shown i can regenerate it um nice okay it'll be fine i don't i don't foresee anything going wrong but i want to make sure everyone sees the ui because i think adding a you're creating your own discord bot is probably the hardest part not even the code part is the hard part unfortunately or so maybe so yeah sorry go ahead i was just gonna say it's amazing how some of the like dev portals just make it so difficult just to simply get it yes 100 agreed we're going to do mute client here so you'll notice i'm saying like let token is equal to this let is like our keyword our signifier that we're going to be writing a variable inside of by default inside of rust things are not mutable i cannot change the value along the way i cannot like say uh token is now equal to this until that's like a constant in javascript [Music] yes yes it's it's um some people are very pedantic about like this isn't you can't quit equate it this way and i i want to say it is similar in the sense i cannot change it but there's also a cons inside of rust and those are two different things and that's why i want to be careful saying yes you'll notice here i'm writing like client colon colon build so client capital c here is gonna basically take is like you kind of think of it like an uh a class i'm gonna basically have like a class i'm gonna call a method inside of that class called builder and it's going to take in that token it's going to let me build something specifically our client here we're going to pass in like how to handle any events that we receive events are anything that's happening inside of our library as well as anything that happens in discord so that can be anything from we're starting up the server we're ready something's gone wrong or i've received a message and i don't know how to handle it those are sort of the things that you can think of it being used for oh wait and then we're going to expect so let's say if nothing goes right uh we will say there was an error creating the client i would i just caught that so like the expect that's interesting because it's almost like uh like a test assertion right exactly that's one of the beautiful things about like uh rust is the concept of like something can potentially be correct something can succeed and that will be fine but we need to be able to actually handle what are the error cases and so by by nature of rust we think about how do we handle the error cases organically while we're doing something um you'll notice like here in this like var and var i'm doing something that potentially will succeed or something that will potentially error and we have a specific type in rus that basically says like it succeeded or there was an error and that's what the expect helps us do is it says if we have the error type here's what you should display to the user interesting yeah the await is going back to like the whole asynchronous that definitely looks and feels a lot similar to what you're doing inside of javascript we have like async awaits uh you're in an async function i'm doing some awaiting like those those things still feel this very similar which is why you know some people really like running a rust after writing some javascript and let's see let's try so i have the client being pulled in i have the handler being pulled in but i need to actually include a bunch of stuff so what i'm to do is i'm going to import some stuff so use is like how you can kind of think of import inside of rust we're going to import from the standard library and and that's how we will be able to use the env var in addition we're going to say use serenity and let's say you want to import a bunch of things but you want to like specifically name them off you can do the same thing where you're using like curly braces to do structure that kind of works similarly in how to read so we're gonna use async trait which i will explain in a bit we're gonna have some stuff for our model and models are like for serenity like um how to describe the data type that we're receiving inside of our rust server so we're gonna have message and we're gonna have gateway and then prelude is a bunch of beginning steps for us so like let's say we need to do a bunch of setup prelude is going to have all that for us so that's all of our imports if i like save this and i try to do like cargo run it's oh cargo run it's gonna break still everything's still breaking we're still having a good time i shouldn't need to install anything but ideally what i'm expecting to see is one it's gonna fail here first because i don't have my discord token and then even once i do have my discord token it's probably gonna fail again because of i haven't declared what the handler is yet but i want to kind of show what do errors look like so it's not like prince just like magically wrote all this code and all of a sudden it just doesn't work we got a comment here that rest is the future rust is the future i i want to i do want to take like a moment to say like there's a lot of nice things that rest affords i don't think everything has to be rewritten in russ i will also say that which is probably a hot take somebody wasn't ready for um but i really do appreciate what it affords us i'm gonna get rid of this for a second and then so things that i wasn't expecting to fill so i expected to see the handler we didn't get to use all these which is i what i expected build is something i wasn't expecting to fail yet so that caught me off guard but for the most part we're pretty good nice um let oh i know what it is i think i spelled this incorrectly i think it's builder perfect so you'll notice like once again i have a bunch of types afterwards once again this is like in vs code there is a tool you can have as an extension called rust analyzer uh spelled like rust analyzer and this kind of gives you the types that you're working with in real time so as i'm like updating stuff it's actually going to tell me what are the types along the way yes so in most of what you're doing inside of rest you're going to be maybe declaring the types ahead of time but it's good to see like what are the types over the course of me changing stuff so you'll see here like the client builder i'm doing a method on the client builder and i keep doing that and it actually turns into a result which is what we talked about before where it can either turn into a client or it can turn into an error and seeing that in front of you as opposed to like having it all in your head where you're doing that in javascript this is kind of one of those things that's like really helpful to have i'm gonna move some stuff around in my other view here perfect so that way i can keep track with all y'all um so we have our builder we have this we did that now we're going to add that handy dandy handler that i mentioned before and so the handler is all is all going to handle the messages that were being sent in specifics what we're going to do is we're implement the event handler for handler and handler is going to be a struct structs are like um trying to think of like what the best name of those things gonna be called structs are like declaring your own types so let's say you wanna have your own custom type struct is kind of how you would do that and maybe you want specific operations to happen for that handler or that uh that type what then you'll do is you'll implement specific features for that object in this case we're saying this particular event handler needs to be able to have this particular thing available to it and this is how you can say it's available i'm i'm trying not to overload with jargon because i think it can be like more confusing than it actually is so if there are people out there going to be like how like that's not how i describe it i'll be like it's okay you and i were trying to build this project and making sure we were all along the ride well i think i'm a good audience ever since i haven't ever used uh rust before so i'm i'm bearing with you so far perfect so what we're going to do is we're going to implement two methods for our handler one is going to be message and then the other is going to be ready so ready kind of like indicates like okay here we've received the event ready we meet meaning that it's time to run we're connected to disk or we can do the thing the other one message is going to be whenever we receive or hear a message that goes on inside of discord this is what we're going to do or this is what we're going to hear and so that's kind of like why you would want to do that um i don't actually know if discord.js has a similar kind of hooks inside of it but that's kind of what i'm thinking about so we'll do async async function this is the first function we've written kind of really uh we're going to write ready and you can actually see here ready ready pops up and so if i oh that's nice press enter it actually will auto complete for all the things and that's one feature that is part of rust analyzer you can kind of know how to auto complete some of the stuff for you uh we have a question is the uh are you gonna have the source code is the source code available we can throw up later absolutely i definitely will have that available for you um in addition like if you're interested you could also go through serenity rs oh and then inside of serenity they have a bunch of examples as well where they kind of talk through some of this stuff and so if you're like i want to see more things i can do with serenity they'll also have that so don't feel like you have to only use what i have shown you here too so i only trust you oh thank you so sweet um so what we're gonna do is we're going to have our thing here i think what we can do come on i appreciate that your chat is so nice i love them they just make they just like warm my heart i definitely agree though you're doing a great job so you'll notice here like print ln print new line um i actually don't know why it's oh i guess print line right that's what it says and the exclamation mark that's actually very key inside of uh inside of rust i almost said javascript but inside of rust this is another type of kind of like function another macro where it does some work for us it makes it easier it's called a functional macro or yeah i might be wrong with that it might be called a procedural macro whatever semantics again but this specifically what it does it says like here if you write this with the exclamation mark we're gonna do a bunch of work for you when we are ready to execute don't worry about it just do these two parts here and then it just works similar to just like a message so i can say this person is connected and so i'll say um let's change this to like ready ready.user.name hope i hear it there we go 30 minutes re-waiting 30 minutes we're doing great i i'm already feeling comfortable we had some issues with that starting off but i'm already i'm getting close we're moving good inside of rust we have to do a bunch of not fun stuff like adding semicolons which is not fun but you gotta do you don't like semis not not no not really not really i'm a semi guy it's all right i think once i moved to moved away from my first language or my second language i guess is java and that was all semicolon and c which was my first language it also had semicolons and so i've just accepted the fact that anytime i'm doing any like low-level programming there's always going to be something i call lens um i didn't realize you knew all those languages i can't write any java for you anymore i can still write c very fine uh but don't ask me to ever write you some java code that's never happening you'll notice uh before i had some red again this is from russ analyzer it tells me when something's broken um it's basically saying like cool you're trying to do this function async and i can't actually do that i can't i can't use the async function how do i do that you have to tell me and so that's one of the features out of serenity is this adding this async trait macro and it's going to be like cool like here's how to handle asynchronous stuff and so now you can see all that right it's gone nice okay i was wondering go ahead and i was just wondering with the the big red lines yes uh russian is very good about telling you like hey you're doing something that you might not want to be able to do um for instance like here it's giving me like a yellow line it's going to say like oh you're giving you're doing something but it's not actually used are you sure you want to do this whereas the red line's like you're going to break if you try to do this and most of the time when i like run cargo run it's going to tell me those same errors but it's kind of nice to be able to have those before i even ran any of my code yeah and you'll see here i try to run our code right now and it's actually saying that errors please add your discord token to the environment not present so it's very good at catching those things ahead of time yes so let's let's take a step and pause here from writing code this code part we haven't finished everything but i want to take a second because i think that the next part the actual add the discord bot to your server is complicated and i want to explain that part shouldn't be but it is so discord has its own developer portal in your developer portal um you'll be able to add applications you'll see i already added an application called kobayashi bot um very very absolutely named if you would like to name your own something else you can do that but i'm just going to show building an application real quick uh let's think of another bot we got going on here i don't know i usually use my my catch phrase of like when i don't know how something works or like when i just want to name something silly i'll use the word beef um that's my go-to okay partially because it's just easier it's like it makes it seem silly so no one's like oh my gosh it needs to be named this way but yeah so that's making the application the next step then is to actually go into the oauth 2 um and inside of here we're gonna add a scope and that specifics that we're gonna add is the bot scope and so what this is gonna say is like this particular application can make a bot in discord you can do a bunch of different things it can do anything from handling email to doing guilds if you want to do anything fancy like that you can do stuff like in there but specifically all we're going to do is do bot stuff you'll have to read through the discord api to basically understand what all these other scopes are but i couldn't figure it out and so i want to make sure everyone else can figure it out i feel like there's so many features just discord that i still don't even like i don't even know what a guild is like is that so that's what discord servers are they are guilds um which but like like the mind-blowingness of like wait that's what they're called right i just feel like i mean it makes sense yeah but i had no idea i wouldn't have known to life i've struggled through this enough um so here we have like the scope bot when i don't have this clicked in you'll actually notice that there's like no other things here so if i say like identity i can do something like that but like when i do bot there's actually extra permissions i can have so if i want to have like the ability for this bot to be an administrator or maybe it can view some audit logs any of the things that a user can do i have to actually manually say like which one of these particular permissions would allow you to have typically you may not want to give them administrative permissions this means because let's say somebody uses your bot you do something malicious um they have direct access to everything of the server um including deleting the server which you may not want them to have access to so you might want to like limit the amount of permissions that they have such as like managing the server and doing other things so i know there's a there's a saying about that like the the least the least i don't know yes you know what i'm trying to say i think the the idea is always like the least amount of privilege given to like a resource partially for this mistake uh if you've ever used something like aws this is like why people always recommend like don't use your administrative account yeah because you'll create an account with aws and it's like has direct access to do literally everything but if you use that account to do all your operations if for whatever reason somebody has access to that account they'll break everything and we don't want we don't want that we don't we don't need that so our bot won't have all these permissions um so you might choose like manage server maybe manage roles manage channels maybe not even kick members or band members depending on what you want it to be able to do you could probably do manage emojis few channels maybe send messages at least your bot might want to respond to people um maybe manage things you can do whatever you'd like um this the choice is yours as they say and at the end you're you're gonna have a url here and it's actually gonna be like here authorize this particular client with these permissions discord uses like a number system in order to be able to say what permissions you have access to and this is like how they are able to say like this is what your set of permissions are i take this link and i just throw that in to the browser and this oh doesn't have a bot i haven't created it probably oh wait wait wait a second authorized access oh us application does not have a bot maybe oh maybe i see goes back to the whole prince didn't do a step um i think what i'm missing here is even though i've done this like application has these settings i also need to add a bot i still want to add a bot a bot is not just an application uh it could be a part of an application so our bot is going to have the same name as our application unfortunately but let's try this one more time perfect that's what i wanted to do it's doing it but this is the way for you to be able to like determine like how do you want your application to be able to have like uh whatever image if you want to have a special image for your bot maybe you want the space jelly to be the image icon you could do all that here you can even rename it if you'd like we can we can call it uh oh space jelly when we come to it later we're gonna save that i'm gonna refresh it just to see what it does so it's still gonna say beef bot boss gonna connect and we can actually select the server that we'd like to connect it to so i'm gonna connect it to a server that i started um and we're gonna just do that we're gonna see and you can check once again before you finish you know like saying this bot is attitude server you can actually check and uncheck like what permissions they have access to so with the oauth you're giving that rule the ability to to select into those but when you give that access you still need to include those as part of that access exactly and it's going to assume that you want to do these things by default anyway um but for instance like if you decide like typically from like a i'm a discord admin maybe i don't want them to have these particular permissions when i give it when i'm accessing someone else's bot but me the user who's creating this bot might not care so it's you have like that kind of granularity of control and you can see boop and then we say i am a human and then it's been authorized and i go over to my discord server which is on the other screen that i have over here but boom we can see space jelly has landed we have it now a bot exists as a user the next thing is to then make sure the bot actually is running so right now we've added the bot into the server so there's always like add bot then bo has to run when we created our bot we needed to be able to have that discord token remember how it says like i don't see your discord token this is the discord token so in the bot section you're gonna have this token inside of here we wanna want that token we're gonna stick that into our environment variables typically when i do this i use like a like an emv file but for like for ease of use i'm going to just run it as an additional thing to my run command so i'm going to say um discord underscore token is equal to all of this then cargo run so every time i'm running right now i'm going to have to have that extra line in front of it oh beautiful well it ran this time it didn't fail but i didn't do anything after that so you'll see here it ran the application it actually finished the application but it's not doing anything afterward with it and that's no longer yelling about the environment variable exactly and so what i think i need now is one last thing which is to start the client that's what i forgot to do so i'm gonna walk through the steps one more time to make sure i got it here we got our discord tokens taken here we started our client we made sure that our client has all of these things being built and the last thing that we want to make sure to do is not just catch errors along the way but specifically catch errors along the way when we've started the client so we'll say if let's this is something to be some weird like uh i don't know what you'd say client uh weird um syntax in terms of like what you've seen in javascript uh but i'll write it all out and then we can go through what it does i like the key word of why exactly it's it's going to be good clients error and this is specifically from like i always do this i see this in the examples quite a bit it's just very simplistic like why um so nikki actually says if led is kind of like a fancy if statement so let's say on this part here you'll see client.start that means like the client will do its thing it's gonna make sure it's working and then it's gonna like await anything else let's say something goes wrong right like we want this part to happen but let's say something goes wrong if something goes wrong we're actually going to do an assignment we're going to create a variable if something goes wrong it's going to match against this particular thing it's going to say like if we went wrong here is the error state that's going to look like so it's kind of like a it's like an if case only if something happens otherwise it's going to run its thing it's going to keep going but if something goes wrong we're going to say store the value of the error inside of this variable called y and then print out the error if nothing goes wrong it's not going to even worry about all this inside and now when we try this card run what i expected to see before was that it just continues to invoke it just keeps running but one thing i noticed is it wasn't running afterwards so like it just stopped at this part and so now we should see this exactly this where it says space jelly is now connected this is what i was expecting to see before is it actually saying like here we're now running perfect now this is all great like this now says we're connected to discord we're doing good but we haven't actually done the actual bot here right stuff things so i'm gonna make like a simple like message thing and then we're gonna go from there i mentioned it before we're going to write a function called message so anytime i see the message event here's what i need to be doing and in our case what we're going to do is i don't know like we'll make it something simple like let's say we receive like a specific message and let's say if a message dot content is equal to this particular thing i always like to do this this is the example that they use um ping so like if i see the command ping then we're gonna respond with pong similar i love it it's really simplistic but it does show a lot of steps inside of it which i didn't appreciate beforehand um you'll see that if let again so let's say i do an action and i want to keep track of the error this is what i'm going to be able to do and i'm going to say oop i forgot if i say like message so that's referring to our variable message up here if i see this message then go to the channel oh 15 minutes we'll get there i got you i got you we'll use the say command and so the say command says inside of the specific channel that you did this ping go and repeat or send some text along the way so in our case this http is like telling serenity like here's the client to be using here's how you send your request and what our request is going to be it's going to actually be inside of this ctx which is short for context it's going to be context dot http so like a lot of little rust things so just to kind of go over this is what i'm saying here is go to the object context that i'm giving you pull out the particular client that i am using this http and this this specific thing here this ampersand very important oh made it too big made it too big because importance enhance this ampersand is saying like don't consume this value only give it a reference of this value that's like a critical part inside of rust comparatively to other languages um this basically says like rust is paying attention to how memory is being used and we want to make sure that we're not consuming memory before it's uh before it is unavailable again so this ampersand is basically saying like here make sure that this value is available for later i'm not actually taking this value and saying like free it up when it's done that that is a big like concept inside of rest it's it's specifically about like uh the difference between the mutability and can i take the value with me later on yeah so it seems like it requires a little bit more granularity with memory management yes perfect that's exactly exactly the way to phrase it is russ generally is like that yes exactly that's what makes rust very unique compared to other languages that also let you do memory management is that you are you have much more control over how long some value has to last um specifically it tries to free up as soon as it can and what we're doing is we're basically saying like we're not consuming this value so when it ever falls out of scope you can just free it we're just saying give me a reference to this value so that way i can have it for later on that's like the synopsis of this this whole thing i could go into a long rant into that but we only have 12 minutes um for another day exactly so we're going to do our pong message here and let's say again if something goes wrong let's just print out a message like says error sending the message and i'm gonna do some special syntax here this says like debugging syntax give me the reason why cool so we have here an issue why existential question why is it causing an issue is that also a rest thing or is it just what like the example calls the error variable the error value just the example is calls it y okay yeah i can call this once again see it can call it beef and but for somebody reading it they'd be like i don't know what that means but that's that's why i go for it um let's see if i have the let error is equal to this we do this ah i know why because even though i am saying say i want to have the await here i believe yep and so message.say the say method results in a result and we want to make sure we're just waiting for that result to come back chat's better be careful here i'm going to start singing what will you sing others say tell me why oh i just did starzing [Music] i need to do a new one kobayashi karaoke oh i love that so now our space shuttle is connected and then when i go to my discord if i talk into the thing let's say i say ping we still see pong we're out here we have our message we can do it again beautiful it's letting us happen it is doing it so let's say like you know you have this message it's going well it's happening we have 10 minutes i think i still have enough time to do the command framework one of the things i was thinking about when i was doing this spicy sounds i was like i wasn't i wasn't ready for it i don't i don't know i don't know if you were able to tell but it was like so the 10 minutes and surprised for the five minutes like i tried to use different like not just the alert it's like you know and they're called what are they called like in shows where it's like like the sound effects that are more like a trumpet and it's just making it very dramatic ah yes yes we're like at the triumph we're at the classic sting yeah oh yes yes that is what it's called um so we have our message we have all this i am going to convert all of this what i'll do before i do that is i'll commit it as it is clear status get out get status get add git commit uh finish ping command before command framework clear perfect 10 minutes thinking what are you going to do with those 10 minutes i think what we're going to do is we're going to swap them we could do one of two things we could easily make another command that directly just works with whatever we have it saying or we can do it with command framework the command framework does have some more setup stuff inside of it specifically what we will need to do is make sure that i have added some more uh things i think i'm just gonna go for it we're just gonna do it uh command framework it requires handy dandy framework features by default we didn't have this installed but we're gonna make it happen in addition we're gonna do our standard framework here so like one we're gonna use the the framework method and then also we're gonna use the standard framework there's some other frameworks if you're interested um but those are the ones that we're gonna be using now when i go and build it it should still all succeed we can keep both kind of things available for us but the command framework takes advantage of macros it makes it easier for you to write code so you don't have to be out here writing a bunch of if cases like you'll notice like i wrote like if the content equal equals ping then it does a thing but that's not like really scalable right like i can't write if this every time if i have like 10 commands that'll be a little bit more difficult so the command framework makes it so it's easier to say like this is the particular command that i'd like to be able to run um when you're like thinking of like building out a big old butt you might do something like this where you're like how do i structure everything together right and that's kind of where it comes from so we might do something i'm thinking i think if i can just do like the handy dandy like okay i'm trying to think thinking loud thinking fast i'm getting kind of nervous um basically what we want to do is have a group of commands together we'll add that into another struct we'll call that general we'll have a group macro that references the general group and we'll say like the commands that are in the group and we'll call this one ping inside of here i need a function called ping i need a command because basically you'll have two steps you'll have the method that you are connecting to for your command and then you'll also have like whatever data that you're passing along so let's just say uh ctx uh i think it's just context here and then i don't think i need anything else maybe the message itself so i can reference the message oh no args i'm making mistakes for a second i thought that was a new rust character i'm just throwing i'm throwing all the new new things in here command results beautiful and then i basically can just do all of what i was doing here before but now actually in the thing [Music] is that the five minutes there we go yep i love it this is so exciting i'm here for it i for some reason that in my brain it brought back up the um i've just watched or i just started uh the king kong or godzilla versus king kong movie spoilers i gotta watch that okay and then i will we're done with that conversation it's not even happening i'm excited to watch it though um let's see what else do i need i need i have the context i need the message no i need the args i need i need rx do i really need arcs though typically i need this like arguments thing but it'll be fine i think it's all right so i'm thinking about like what are the things i need to import so inside of the framework uh package inside of standard i'm gonna need to install a few things i'm gonna say args command command is it options command options command group and what else i need oh i need all these i did that i have all these options i also need the macros for those which is command then group open my comma so we've imported all the things you should be fine why are you why are you whining group should be fine what are you whining about we're just gonna we're gonna run it oh no let's go for it beautiful oh i know in addition to all of those things so i have my group i have my function i need them all to be async that gives me the command result i'm going to say okay here because command result is just a fancy smancy like okay message i don't have command result as a thing result now i have command results is that not in the import no that isn't the import what am i thinking i think that's fine maybe comma that's what i was missing there um so now i have the command framework i need to add a few things inside of the client which are the different ways of connecting the commands we're not gonna make it i'm already nervous i'm already nervous how close are we are we close we're like pretty close i i have a feeling that i will not i will not pass the uh the the actual thing but kurt's here and he believes in you y'all are y'all are too kind to me oh and i dropped something um no time leave it behind uh oh um what am i thinking what am i thinking framework okay um framework i need the framework but then i need to figure out where to declare framework i think that was let's framework here standard nope that's not the one oh no that's too many things standard framework new beautiful love that um and then we're going to configure we have our like configuration object i might call that like config bad names sorry and we'll do the framework i'm trying to think um oh no i think i messed up trying to think um it is salvageable it's like it's like the small details this is like the details of like how the framework works so like right now we have the framework we're setting it up we're configuring the object we have the configurations themselves we might do things like like what are the prefixes it's like you'd have like prefix inside of here being like this i need to do that didn't get it i can hear you it's yeah that's tense so maybe i need to change so i have it so it goes for 10 seconds it just does the final thing but if you hear it that's good music right we made it work once but i love that music exactly yeah but oh i had the cargo run fail let's see if i can make a run let's see if it even does work it may it may work it may surprise us maybe maybe we'll be good i'm going to call it a win if it does grass didn't didn't get it messed up it trying to do just a tiny too much i i know whatever you got you got the original bot though i would call that a win we're we were going for the bonus points i was going exactly whole 100 here exactly do you uh how long do you think do you want to try to do that or do you want to just call it i think we're all right i think what i can do is i can i can make that his own commit which is fortunate like you doing doing it and it makes it nice to be able to see like here's the differences between the two um i think what i need to do is just like add the commands so like right now the framework basically saying like here is the framework we're using and i just need to add the commands group to it i think it's just like group and then that would be it but it's fine linking them together that's awesome well this was this has been an awesome walk through like it's i feel like this is a great example for getting familiar with rust because like the different pieces for like between like connecting to discord itself and like all that stuff and it's it's been great i am big fan of like finding projects that end up being easier to learn um i i uh am a big fan of the rust programming language book rest programming language book um i do i do in the long form recommend this particular book if you're a big reader partially because it goes through from beginning to end of the language you can once you install rust in fact i believe by default it's actually able and available to have it offline so you can actually have it directly there i think it's just like cargo stocks book that's intense is i've never seen a language ship with a book before that's pretty great but yeah uh did you wanna just like really quickly walk through what you did uh what you did today yes so what we started with was we used rust to build a discord bot before when we did this i had used the kind of like implementation way of saying like the handler handles all of our messages from like ready to message and then we try to convert it over to what is known as the framework method or the command framework where we use macros to do that all that so we can power a bot that can say ping and pong we also um came across a small technical detail oh no colby can you hear me we can hear you now that is hilarious was it just me or was it i was still here oh no i was still here i was confused i was like wait did did i because you were you were close on my screen so i was like oh no that's hilarious well we're back i don't know if it's still actually recording because i still see the oh i just need to switch to zoom oh it didn't rejoin wait are we not live or does it mean that they can't hear us or can they hear us oh we're still i don't understand how that crashed like that oh my heart i'm gonna stop sharing how's that yeah um and i i can't even seem to oh wait it's because it's on the second screen so i'll at least get i love can you actually share your screen because now it's like inside of the oh yeah for sure i just don't have it set up to i have a very specific you're totally fine okay well just to get the nice little intro or outro rather um thank you for everybody who raided that was amazing and the worst possible timing ever but i do sincerely appreciate it but um prince thank you so much for coming on today this has been an awesome walk through rust is really cool and i appreciate you walking through the awesome demo of it um yeah everybody make sure to check out prince's twitter his stream uh maxell and next week we're gonna have melissa mcgregor who will be facing off against building a full stack jam stack app with redwood js so prince before we go any last words my last words is be ready for anything you truly truly cannot predict what's going to happen but we're here for it all yep yep well i think that's definitely fair and did i no it's still here okay well uh this is uh colby signing off and uh thanks everybody for stopping by bye y'all you
Info
Channel: Colby Fayock
Views: 515
Rating: undefined out of 5
Keywords: discord bots, discord bot tutorial, discord bot commands, discord bot client, discord bot coding, discord bots to add to your server, discord bot rust, rust, rust tutorial 2021, rust tutorial beginner, rust discord servers, rust discord, rust discord bot, discord bot in rust, rust serenity, rust serenity tutorial, colbyashi maru, rust discord client, rust tokio, rust tokio tutorial, discord bot setup, discord bot setup tutorial, discord bot set permissions
Id: 03sHMLdQQXk
Channel Id: undefined
Length: 68min 58sec (4138 seconds)
Published: Wed Mar 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.