Code a Discord Bot in Java with Discord JDA! (Episode 1: Commands & Embeds)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone and welcome back to another video No - damn be showing how to code your own discord bot in Java using discord jda I'll show you how to do it step-by-step straight from the start so you won't actually need to know Java for this tutorial whatsoever I'll teach you everything you need to know starting from the beginning at getting java on your computer so anyway let's get started so let's get started by going to our web browser and open a few links down below in the description I'll have all the links we need for this video down below so you can just click them download all the stuff you need but the first link we're gonna go to is this link over here and it's gonna take you to the download page for Java development kit so Java development kit is actually necessary to code in Java so we're well we're coding in Java so we need Java development kit so just get the latest version accept license and then you can you know get your Windows version or whatever fairly easy so after you don't with that just brush that off install Java fairly easy most you should probably have Java installed but this is Java development kit so it's necessary if you want to actually code in Java anyways the next thing you need is an IDE now an IDE is kind of like a program that you code in and it has all the stuff you need to code theoretically you could do all this in a text editor but I really recommend eclipse because it helps you actually see your errors that help you fix your errors a lot better than other text editors would normally do it also has a lot of features packed in so basically what I'm trying to say is Delanie clips cuz you need it so anyway go to this link I'll have down below in description click download for 64-bit or whatever bits you have hopefully you're not an 8 bit because we don't have a download for that and just click download over here and it'll download straight to your computer I don't actually need it because I have it so after that we're gonna need to get JTA now JTA is actually a wrapper for the discord API basically what that means is it's gonna help us interact with discord that's the simplest way I could put it so using JTA we can make our own discord bot that basically interacts with discord that's what discord lets do right so that's what we're gonna do we're gonna go to this link double in the description it's gonna be a github URL and you're gonna want to scroll down until you see this download thing right here click download then go over here to the latest version we'll always be at the top click on that and then click the normal jar over here now the last thing we have to do is create our discord ball in discord developer panel which basically makes discord give us a token or like a little string of text that we can use to connect our ball I'll show you in a second but basically go to discord Gigi or discord app comm link below in the description it sounds like I'm doing an advertisement but great-great up anyway go to developers over here and then go to developer portal and after you're here you'll see all the applications you probably won't have any if you're doing this video but I have a lot of weird ones but anyway we're gonna click create an application it should be the first or the very last and let's title or application now a great way to get a bot name is to go to a random noun generator and this one generated television so I'm like okay I'll use television so we're using television for the bot name for this test video it doesn't really matter just title it literally anything and we're gonna call it television and a thing that visions over telly and we're done with that so just click Save okay so now that we've got that we're gonna want to add a bot to our app so just go to your bot section right here and just click but and you'll see build a bot so that's what we want we want to add a bot to this yes do it and a wild bot has appeared apparently and so this is our bot we have the username of the bot and we also have a discriminator which is what this is called and so it's basically like a normal user except it's a bot user so it can go in infinite servers and not just a hundred like a normal user so yeah that's what it is so anyway we're gonna add an icon to this and I have a perfect icon prepared for this that I forgot they don't support gifts let's see if it still works okay it works cool yeah it's a very nice image there about the internet always crashes yeah now that we've prepared our bot with a nice icon and a good name hopefully a good name hopefully better than mine but anyway we're going to the OAuth tab over here on the side and we're gonna go and select a bot down here because we're making an invite link for a bot so we want to invite our bot to our server right because it's not invited so we're gonna want to do that and let's do that right now so basically we created a bot link and now we want to give permissions to our boss so this is similar to like permissions to a user in your discord server if you want it to only send messages and bam members you can do that and can't do anything else but just for development purposes it's always easy to put it as administrator and it'll give you full permission so you wouldn't want to do this after you've developed your button and you're about to like export it to a bunch of servers but for now administrators good and then just click copy up here on the link open it another tab in your web browser of choice and it'll open up a little selection here just select the server you want it to go in I called my test server toasty tester so now that we're done with that we can just close out of our web browser and that's it so now if we go into discord what you'll see is in our toasty tester server we'll see we have our but it's and I look at what this is greeting us anyway yeah we have a bot it's a bot doesn't really do anything right now so we got to do is we got a throw code at the bot to make it do something now you want to open up Eclipse to add your code in because if we're gonna code in Eclipse I'm gonna make a new workspace I'm just gonna call it television television and that's good so we shouldn't launch it up so this workspace will be just like what yours will look like when you log in so it'll be pretty easy to go from here but anyway when you open up eclipse it's gonna look like this it's very very white but anyway open it up click workbench over here and you'll see your workbench tada I would recommend closing some of these little tabs over here they're not really necessary for what we're gonna do so just minimize them minimize them and resize this package Explorer is kind of fat okay now we're well we're here so we're gonna want to go right click on our package Explorer click new and then Java project because we're creating a Java project now I'm gonna call it the Java project the name of my bots oh television is fine for me and click finish down at the bottom here and you'll create your own Java project now you can pat yourself on the back because you created a Java project but it doesn't have any codes so you can cry about that as well now we can create a new package inside of our project which your package is basically like a little folder that stores all types of code in it so you can have a package for like listeners or commands or something and they'll be in different packages just to organize things pretty much that's what they're for but for this package this is gonna be our main package so I'm just gonna put my name or my website backwards so if my website was like tech toolbox comm I put Vox but I don't really have a real domain but I'm gonna put my name like that and then the name of the bot would be preferable after that and we're done so when we created a package now you need to make a file inside of the package so I know it's a lot of nesting instead of like project package class but we got to make a class which is basically a file of Java code and we're gonna call it main since our is it's our main class you can also I guess name it something like television I think I'm actually gonna name it television so if you want to rename something in Eclipse just go to refactor right click refactor and then you can go to rename and you can name it something else so I'm just gonna call mine television it doesn't really matter you could call it bought java or something like that but that's how that works I'm just gonna make this code a little larger and now an important thing we need to do is we need to actually import our JTA jar files so remember we downloaded that earlier this is it on my desktop right here so what I'm gonna do is go to my television project right click click properties then we're gonna go into Java build path on the side here click the libraries tab and then click add external jars and then import that click apply and close and you're good to start coding your BOTS so fairly easy stuff like that one thing I'm also gonna do is I'm gonna quickly set a Jarque theme for this so your eyes aren't burning um so yeah give me two seconds ah that feels a lot better but anyway here it is so we're ready to start coding so now here's the fun part we actually get to start typing something so that's always great sometimes until it airs out but anyway so we're gonna start with a new line inside of these two brackets these two brackets basically contain all the code that we want to put inside of this file so we're just gonna do public static JD a JD a now JD a is a variable that we're gonna use to basically reference the actual JD a file we imported so that's gonna allow us to do things related to our BOTS so anyway you'll see that there's an underline right here with red but basically means it's wrong but don't worry it's not wrong you can actually fix it by importing it so you can hover over on JD or anything that's underlined and if it says import you can click on it and it will import it which basically means it'll find the file that it's related to and then just like link it to your file so what you want to do here in this case is you can click import but a great command I like to teach is control shift oh and it'll automatically import all the stuff for you so you don't even have to hover over just control shift o if something's broken BAM it fixes it so that's good anyway now we're gonna make a main method and this main method is something that will start when you start the bot like right when you start the bought this is the code that's gonna run so I'm gonna just title it main method here and what we're gonna do is we're just gonna do public static void main there go main method right and then we're gonna do string inside of here string two brackets and then we're gonna put args after it and that's about it that's the whole thing we need alright so here's the fun part we're gonna make our bought connect to the bought account we created so we're gonna do is we're typing JD a is equal to new JD a builder so this is gonna create ourselves or build ourselves I suppose a new bot which is pretty good and then we're gonna put a count type over here and that's gonna define the account type we need and we want to put bought a count site bot so it's gonna be a bought account you can also make a client account which is like connecting to for example your own discord account but don't do that because that's against Terms of Service and you're gonna probably get banned so don't do that okay always use BOTS okay good lesson learned alright so we're gonna do set token over here and the token is actually the little thing it's not actually little it's a pretty long amount of text and you put it inside of here and it basically connects to your bought now you do not want to share your token with anybody because if they get ahold of your token well let's just say they can probably delete every single channel in your discord server they can also maybe kick every user and you just scored server may be banned them so don't do that okay don't share your token otherwise bad things will happen to every server you're bought is in if somebody else gets ahold of it and it's in the wrong hands yeah anyway so we're gonna do dolt build async at the end after that amazing interruption and we want to import JD a builder and remember ctrl shift o on your keyboard is the best command shortcut ever and now everything looks good and water paste in our token so you're gonna want to go to your web browser again go back to the discord web site go under the developers developer portal does this feel familiar and then we're gonna go to television we're gonna go to our bot and we're gonna go to token now never share your token once again don't share and if you share it by accident just regenerate your token it'll give you anyone but here's my token I know I just said don't share but yeah whatever do whatever you want with it I'm probably gonna reset it anyway I'm probably gonna forget and you can probably delete my test Channel but it's just a test channel anyway so now if we want to run our bot let's just quickly do that we'll click run television up here at the top with the little green button I'm gonna move the console down here just by dragging it and you'll see that your bought is okay when it says finished loading and if we go back to discord tada bot is enabled it's also a Wumpus once again anyway yeah our bought is enabled as you can see it's online but it doesn't do anything right now so we want to add some commands to it but before we do that I actually want to do one thing and that's set a playing message so as you can see I have a playing message up here and it says I'm playing a game which is Eclipse so that's fun but yeah I want to do that as well on the bot and I also want to change the online status to may be either idle or do not disturb that would be fun so we're back in Eclipse so we're gonna click the stop button to terminate the process and I'm just gonna close the console once again and we're back in here we're back where are we were so we're gonna add a new line to actually set the bought as idle so we're gonna set it as idle pretending like you know it's not doing anything so she's waiting for a command so we're gonna do JD a Deltan get presents which is gonna get whatever that that little thing is called this is basically called the presence and so is this this is also called presence it gives information about a user and then we're just also gonna do set status which is the actual icon there so we can set it to idols so online status idol and Idol here there we go perfect and this one line will make it so it's idle but let's also add a game while we're at it we're gonna do JDM don't get presents once again and then we're gonna do doubt set game and then we're gonna do the game ja let's do mmm do you want to do streaming watching let's see television I guess television watches them thright or and guess it technically stream stuff over cable I don't know how television works anyway watching watching boring comedy shows I don't know why that came to my head but boring comedy shows it is so now if we start about once again you'll see that our new presence has been applied and it says watching a boring comedy shows that's great I don't know why it's still online that's not very reassuring but hopefully it does change so here we go I waited for a little bit I don't know why it took so long but here it is Idol is working so yeah it seems like the ball didn't stop last time I stopped it so it had to research itself but anyway you have Idol it's watching boring comedy shows that's all I could ask for so now that you got that we want to add some commands to our ball right so let's do that so we're gonna go back into eclipse oh I just realized I said so like 30,000 times but we're gonna go over to our package Explorer again you're gonna visit this place a lot when you're making new classes which is remember files of code so right-click on your package click new click class and we're gonna call this commands yeah so we're gonna put our commands in here this give me your commands class now every single commands class you want to put extends listener adapter now listeners are basically while things that listen for an event now an event for example is when you send a message so if we type F that would make an event of sending a message also for example adding a reaction of this one sounds good adding a reaction is also an event so you want to make sure that you capture the event in this case the event for adding a new message because that's how events right you send a message to to do a command right so we're gonna do that so to do a an event which is this is gonna be a similar format for everything but we're gonna do public static if I could type once again public avoid I'm actually not gonna do a static this time on guild mess oh geez message received event and might actually not put the event for it they're just you can you can change this to pretty much anything you want but I'm just gonna put it to that for now and also guild message and then here it is I'm just saving myself some time received amend and then we're gonna put event over here and there we go so basically what this does is it registers whatever every time an event basically happens so that every time this event occurs it runs code that's inside of here inside well this is where this cursor is so that's perfect because if we want every time a message is sent if you want to do something as great but we don't want to do it every time you want to check for like a prefix right in the beginning so for example maybe like like a tilde and then test right that would be our command this would be the prefix you don't want to send every time every time I message is sent you don't want to run a command so there you go to our television class and I'm gonna make a new string now a string is basically just characters like a normal character you can also include numbers in there but we're gonna do public static prefix actually public static string and then prefix and we're gonna set this this equal to whatever you want your prefix to be so in my case it's that and then put a semicolon at the end to end every single line that's how the Java works oh you have a tips let me call it pretty simple anyway the static also by the way makes it so any class any single class here can use this which is what we want because we want to reference it so anyway now we're gonna check to make sure there's actually a prefix in front of the message that's being sent right to actually activate a command so fairly simple here we're gonna actually split everything up into arguments here arguments are like little pieces so for example this would be arg one argument to argument three so these arguments are separated by spaces so every single arguments separated by spaces and we want to check if the first argument has one of these so what we're gonna do is we're just gonna make a little string list and we're gonna do args is equal to event dot and yes message and then we also want to get the raw message the raw message yet there we go get content draw and we want to also split up every single times there's a space right because every argument has the space in between so we want to split it up that's it now we've created our argost statement and now we can do an if statement on this statement is basically a conditional thing it checks if something is true that's as simplest way to explain it so you could do an if statement here and in between here we can do our use 0 so we're checking for the first argument Java counts from 0 up so you doesn't start at 1 the first the first thing here wouldn't be 1 or this first argue it wouldn't be 1 it be actually 0 the second will be 1 so just minus 1 whatever you think the argument is so if the first argument which we're checking for here and then we want to we want to actually get the message itself so equals ignore case that's basically gonna make make sure that no matter like how the command is written no matter if it's written like I don't know what's a good way to write it test like that with different cases it's still gonna run the same command even though it's written differently so we're gonna do that and now we want to check what the command is so we're gonna test command for now and then actually well let's do it let's do a different command let's do an info command it's gonna send information about the bots yeah sounds good okay cool mmm and then we're gonna go to main and then we're gonna add dodge prefix here and then add like that and I also just realized that we didn't call our main class the main class we called it television but that's fine I think I'm forgetting how to type television there it took like 10 tries but I did it so basically what this is gonna do it's gonna take the prefix from the beginning here which is this one and add it to the message which is gonna technically this is gonna make a tilde and then info so we'd have to run till the info in order to run whatever is in here so that's pretty simple so now we got to make the command actually do something so now let's send a message to the channel that the command is executed in so in this case we want to sign it in the same channel which is general so we're just gonna do this it's fairly easy event dot you get to channel the send message and then we could put the message you want to send hey there I'm alive like the bot is alive or something it's like some weird AI or something I don't know and at the end of everything like a message or something you want to put doc Hume and before I do that I want to make it look like it's typing in the in the chat so forever whenever I'm typing if I press F so you can see I'm typing up here and it also usually says it at the bottom here I want it to do that before it sends it so I'm just gonna replace this and then I'm gonna get the channel and then send typing don't send typing and it's gonna send a thing which basically says the bot is typing until it sends this message I think it's pretty cool anyway now we've got our command this is actually gonna be a fully working command and just these three lines well I guess if you count all these other lines and all the other classes but anyway just don't pay up don't pay attention to the minor details the three lines we have here is the command so we're gonna go over back into our class which is our television and we're gonna register this file as an event so all you want to do is basically go over here oh geez I already messed it up all right go back at the bottom just do JD a dot and then we're gonna add a listener event and we're gonna take new and then the name of the class so my case is just commands so new commands and then two parentheses at the end and now we've registered it so now all we have to do is run the bots so just click this run thing at the top make sure there's no errors here and once it says finish loading go into your discord hope it didn't break type the command info and there we go hey there I'm alive but I don't know fairly simple and what you saw there is if you pay attention and do it again at the bottom here you'll see that it sends a typing message there we go and it sent that so this time it seems to have glitched out the typing message a little glitchy well you what's usually supposed to do is send the message or send the typing and then the message after it would remove the typing but in this case it's it's broken but that's fine it just goes away after a few seconds as you saw there so that's about good so now I want to teach you embeds now n bits are really cool I'll have a picture on the screen right now sample a minute but yeah that's an EM bed and an M bed can be used to display information pretty nicely actually it's like a little card that you send in the channel so let's do that let's send some information about the server in a nice little EM bed so let's go back to our commands and we're gonna actually start by creating an embed here so we're gonna go ahead over here at the top and we're gonna create a nice little embed now to create an embed you need to create an embed build there similar to how you need to create a JD Builder to make a JD a but yeah very very simple oh also stop your bought that for you done yeah that's good idea anyway let's create an embed Builder so just literally type embed builder I think yeah I spelled that better correctly cool and let's title our embed so I'm just gonna call this info it's nice info ended so that's that and we're just gonna do new embed builder over here and that's that now what this let's actually quickly do until shadow first what this does over here we're putting new embed builder that's called instantiating something in Java or basically just making it work in this case yeah you'll learn about instantiation later I'll have a card on the screen as well talking about what it is but anyway let's also add some stuff to our embed so let's add a title to our embed and info is our M bit so you can just do info and then add tight or actually set title in this case so we're gonna set the title as what is it ball it's called television television in television bought information let's do that yeah sounds cool and if we're gonna do that semicolon at the end and now we have info dot set title television bought information that's gonna make the top of the M bet have a thing that says television bought information yeah let's let's actually see if I can add an emoji on Windows it's a Windows key period and let's do television there we go and [Music] let's just casually remove that and hopefully that this place properly alright so we're gonna do another one we're gonna add let's see what can we add we can add the crater right so let's create a thing called a field now a field basically just splits up information into two little lines and on one line it's the title of the field and on the bottom it's like the data of the field I suppose so pretty easy just do info dot add field and then you're gonna put two things in the field and put one two and then also I think null is that the actually no false at the end the last one in this case is whether it's inline or not we're gonna want to make it not inline so we're gonna save our file and also since I put any emoji wants us to save as utf-8 so if you do something like that just utf-8 like that in the first little set of quotes here you're gonna put the title of the M of the field so the field title I'd be putting like the Creator right now I put myself at the bottom as the actual value so check toolbox and then that's about good oh let's also do a description yeah let's add a description tor embed so we're gonna do set description and let's do something like this like use oh yeah completely useless information about about a useless pot yeah information about a useless botch called television a very passive-aggressive I know even though it's sending it itself and let's also add a color now you can add the color to the side of the EM bed as well so we're gonna do info dot set color and we're gonna do 0 X and then we're gonna do is open up your web browser and type HTML color picker hopefully you're in Google because Google has one of these and you can choose any color you want I'm feeling like a little bit of a red for some reason today so we're gonna do this color here copy paste everything but this little pound sign in front and then paste it right after the 0x and semicolon at the end and that's about it so now what we want to do is you want to send this instead of the ham here whatever a live message so we're gonna do info dot build that's gonna basically take our embed and build it nice suppose that's the easiest way to say it combine all this stuff into one little thing and send it and then after we sent it we want to clear the embed because that saves on some system resources some memory so embed clear and that's it so let's run this one more time let's click the little run television at the top let's go back into discord so we're just gonna do info and ok yeah let's do info again there we go there's the television completely use this information about a useless pot called television creator tech toolbox so this embed is cool and all but let's add another step of personalization and add the users icon and name at the bottom as a footer as a creator instead of instead of having this field over here we'll have it at the bottom okay so let's do that quick let's go back into here terminate our bots existence for a second and we're just gonna do another thing here at the bottom there's another thing that an embed has and it's called a footer and what you can do is you can put text and also an icon on so what we can do is we can do event dot get member which is the user that actually sent the command in the first place and we can get the use or get the yeah you want to get the user note set the user so we can set the user or I met and get the user I'm confusing myself pretty much get the user and then get the avatar URL which is gonna get the URL for their icon over here and then you also want to put some text here so created by tech toolbox or whatever and let's actually remove this field now because it's basically redundant to have our creator field be there as well and then television at the top so it's not information it's more television and let's see we have an error here oh yeah I didn't put a little semicolon at the end now that we do it all lights up in perfect colors we can click the Run button one more time go back into discord let's say for info command one more time and as you can see television completely useless information about a useless pot called television created by a tech toolbox that's how embeds work there very nice and simple and a really great way to display information you can even put images in the actual EM bed which is an awesome feature of them but anyways guys that's about it for this episode today it was a fairly simple episode just creating some commands but it was a very very fundamental step in creating your discord bot and also in your discord bought journey if you guys want to see more videos just like this leave a like on this one and I'll make another episode on how to code a discord bought also if you have some suggestions about what you want to see how to code I can do very complex things so just leave a comment down below in the comment section and just tell me what you want to see but anyways guys that's about it all this source code will be down below in the description so you can go download it and copy it I will remove the token even though it's kind of useless now isn't it I'll leave the token in who cares it is I'm up I'll see you in the next one [Music]
Info
Channel: techtoolbox
Views: 121,588
Rating: undefined out of 5
Keywords: techtoolbox, how to make a discord bot, discord jda, discord bot, discord bot tutorial, jda, jda tutorial, java discord bot, java, how to make a discord bot 2018, discord bot java
Id: jGrD8AZfTig
Channel Id: undefined
Length: 28min 12sec (1692 seconds)
Published: Fri Dec 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.