 Hallå där och välkomna till den här kursen. Den här kursen är mer än en hår av videokontent och det är beslutat att läsa dig hur att utveckla och finna programmet på Discordbot idag. Hallå där och välkomna till den här kursen introduktionen. I den här kursen kommer vi att utveckla en Discordbot med den Node.js programmet. Den här botet kommer att respondera till en specifikt kommande och vi kommer att skriva funktionalitet för att göra det. Så de städer i den här kursen är att vi går ut ur installationen av software, som är Discord, Node.js och Visual Studio Code. Och då skriver vi en Discordbot i en Discord-developerportal. Och vi skriver en Discord-server där vi ska testa botet och få den upp och ut. Och vi kommer att få en invitation-link till vår bot så att botet kan joina i Discord-serveret. Efter botet har varit med till serveret, vi behöver ta fram en NPM-installation, skriva en package.json-files och då kan vi starta att utveckla Discordbot, skriva den basic koden och progriva en bot som responderar till en kommande. Efter end kan jag skriva kommentar i koden för att öka vad koden gör och jag kommer att ta fram hur att göra det och varför det är viktigt att skriva kommentar. Den här kursen är mer än en timme av videokontent, så det är designat. Man kan skriva den här kursen idag och utveckla en Discordbot idag. Min namn är Erik Pedersson och jag är 25 år sen från Sverige. Jag har många års-experienter i C-sharp, Node.js, JavaScript, fullstack webbdevelopment, webbdesign och jag har lite erfarenhets- och gamelöpning. Och Arduino, de är basically my core skills och jag har utvecklat många olika Discordbots och jag har några års-experienter när det kommer till teaching online on different platforms. And I do use Node.js a lot when I develop web applications or browser games or Discordbots that is basically Discords using Node.js to develop a Discordbot. This lecture is going to be about the software, programming languages and the editor that we are going to use within this course. So to begin with, well you need Discord because we are going to develop a Discordbot, then you need the Discord software, which is essentially where well you have like social gatherings, you write to people, you talk to people, you can have like screen share, you can live stream, you can host events, etc. There is a lot you can do with Discord. And this is kind of requirement, you need to have Discord because if you are taking this course I believe that you have a Discord account just like I have. And you simply just need to download it and create an account, it's super easy but I will not show you how to do it. Okay, but that's pretty much it about Discord, it is the social software where we communicate. The programming language that you need for this course is Node.js. This is Node.js and it is essentially JavaScript used as a server-side language. And when you download, select the recommended for most users, open when done, sometimes it is a bit slow, so let's see. All right, so let's install Node.js. You have to agree here, next. That location is fine, next. You can press next here. This thing, uncheck this, it will install things that you don't need. Next, install. And it should be quite easy to install it because it is simply a programming language and you can run a Node service on your computer. That is a nice feature, you can easily host your Discord bots on your computer if you have Node.js. I already have Node.js installed so that's why it says like removing files, etc. So it might be taking a little bit longer for me and Node.js is installed. However, you might need to restart your computer when you have installed Node.js. So after we have installed Visual Studio Code, I want you to restart your computer because I think sometimes you actually need to restart for Node.js to work on your computer. For example, start in the node server. Sometimes it just doesn't let you and you have to restart your computer. But Node.js isn't like something you have to start, it is simply something that is on your computer now. And for Visual Studio Code, this is the link. And you can see that you have different downloads here, Mac, Windows, Linux. And download for Windows is what I have. And it is going to be downloaded. In the previous lecture I supplied you with the links. I will not supply you with the software but I will send you the links. Okay, agree, accept the agreement. And I don't actually want any of these installed. And they should be quite fast as well because it is a very lightweight editor. This isn't like Visual Studio where it takes a real long time to actually open up and start up the editor. This opens really fast. And these are the files that we are going to develop within this course. All right, so that is pretty much everything. Now we have Discord, Node.js, Visual Studio Code, and we can begin developing the bot. Hello there and welcome to this lecture. In this lecture we need to head over to the Discord developer portal to create a Discord bot. Because we need to create an application that is essentially going to have details that you can log in with. And I'm going to show you how it works. I've already googled. Discord developer portal. And I clicked this link. Now you need to be logged in and I am logged in. And I go to applications. If you get stuck or anything you can see my URL up here and you can simply type it. But here we are. These are my bots that I have created. And we can ignore them because we need to focus on this button up here. New application. Because we want to create a new application. And I would like to call it Funny Bot. And here you can change the name of your bot if you wish to. And that is something I am going to do. And I am going to say Responding Test Bot. Because that is basically what it is. But you can name your bot any name at all. However this application is created. But it is not a Discord bot right now. You need to head over to this tab called oof2. And click this add bot. Yes do it. And now this is a Discord bot. Okay. We have created a Discord bot. That is basically what it looks like if you go back to application. So you can see the bot down here. This is the bot that I created. Now I would like to go to Discord. And I have a server that we are going to use where we are going to test the bot. And you need to create a server and that is quite easy. I am not going to create one but I will show you how to create one. If you press this button scroll down here all the servers you are in. You can select whatever you want. These are templates but we don't really need that. But if you want a template you can go for it. I want create my own. Is it for you and your friends or a club or community. It is for me and my friends. And you can name it anything. Name here. Add any press create. And what happens when you press create. Is that a server like this is created. The difference is that you only have one text channel and one voice channel. Both are named general. I have a bot and a user added to my server. But we can ignore them because we don't really need to focus on them. Once you have your Discord server open. We need to do. We need to head over to desktop. I am currently on my desktop. And we need to create folder. This folder. We are going to store the code inside this folder and the project files etc. And I would like to call this responding bot. And what you need to do now. We need to open visual studio code. And it is going to open up some random file for me. So I need to click open file. Or actually open folder. We have created a folder. And my folder is on desktop. So I click desktop. And the folder is called responding bot. Select that. And now it is open. This folder is empty. To create a file. If you see what is highlighted in blue here. If you right click here. And select new file. You can create a new file. And I would like to call my file app.js. .js stands for JavaScript. And we are creating a JavaScript file. So that is actually needed. It needs to be .js. For example you have files called text.txt. And that is a text file. Txt. The name should be app. Because when we create a project. It is going to have app.js as entry point. But you can give it any name at all. You can give it like. Home about bot.js. You can give it any name at all. But inside this file. Before we do any installation. We need to gather three different things. We need to gather the token. Add a white link for bot. Okay. How do we get this? If you head over to your discord.develop portal. And select your application. The bot you have just created. You go to oof2. If you go to bot here. You have token. And if you press reset token. You see this is my token. And I copy that. But if I refresh. It is no longer shown. But I have the token here. That is my password. And if I regenerate it. It is going to be gone. So we need to take the new one. Okay. It is essentially like. Updating your password. Or changing your password. And this thing public bot. It is checked. So uncheck it. When it comes to privilege gateway intents. We need to access the message.content. And this is going to be changed. In about the month. So what you need to do. You need to check this. Okay. So now we are going to have access to the message.content. Because that is how the bot is going to act. It is going to act under message.content. Basically the text they use as written. Okay. Save changes. What we need to gather now. Is the invite link to our bot. That is found at oof2. We got to. URL generator. The scope is. Bot. You can use click that. And this opens up bot permissions. When you have bot permissions open. Fill out the send messages. And suddenly. You have an invite link down here. So copy that. And then take this link. Taste it. And suddenly. We have the responding test bot. And we can add it to our server. My server is Eric Peterson server. But your server is going to have a different name. Click continue. Send messages. I am human. The bot should be in my server. Is it? Yes it is. This is the bot that we have created. And it is now in my server. And you can see this as this icon. It is a bot. But we are not really done. We need to paste the invite link here. And that is pretty much it. We have created a disco server. We have created a disco bot. And we have added permissions. We have added gateway intents. And. The bot is now in my server. We have invited a bot basically. And in the next lecture. We will continue developing the bot. In this lecture. We are going to do the NPM installation. And the NPM installation is essentially adding a project.json file. And with config data. That is going to be very important. Ok. So to open up. I have highlighted this section in blue. Right click. And open in integrated terminal. And here we can use the NPM command. To install things. But we need to begin with NPM init. Right in there. We initialize the project.json. Basically. It is actually called package.json. And it is dependency. It has dependencies inside the file. The package name. You can write anything here. Or you can simply leave it blank and press enter. And it is going to select the default value. That is inside the panthesis. The version. You can keep it at. The default. Description. A. Thought that responds to a message. This is what I talked about in the previous lecture. About the entry point. Make sure that it says app.js. But if it doesn't. Simply write app.js. And press enter. And it is going to be set as entry point. The test command. We don't really need any. So press enter. Git repository. I have none. I press enter. Keywords. We don't really need that. Author. Write your name. Or your nickname. Or your tag name. Or anything that you want. License. I press enter. Is this okay? And then I press yes. And package.json is created. And this looks a bit. Empty. There is nothing important here that. Like has dependencies or anything specific. That is something that is going to be added. Whenever. We install. Discord.js. And we are going to do that immediately. We need to write npm. Follow by install. Follow by the name. Of the module which is discord.js. And then you can add a dash dash save at the end. It's going to save it as dependency. And press enter. We wait a couple of seconds. Okay let's read what it says. Added 24 packages. It said found zero vulnerabilities. And we had no error. So that is great. And suddenly. It has added a property called dependencies. And added discord.js. This version. As a dependency. So now for this project. We know even if we remove all of the files. We know that this. Requires discord.js. If you were to remove all the folders. And you keep the app.js file and the package.json. And you write npm install. It is going to automatically look at the dependencies. And install them automatically. Okay. Now we have discord added. In the next lecture. We are going to start writing some code. And we will get the bot online. Up and running. But it will not be able to respond to messages. Because that is something we are going to do later on. In this course. So I will see you in the next lecture. And we will start writing some code. Basically doing programming. Hello there and welcome to this lecture. This lecture is going to be about coding. Basically programming. At the start of creating this bot. Well, what do we need? You remember we installed. Discord.js. So we need to access that. Inside node modules. Discord.js. Because discord.js contains. Functions. And methods. And APIs. And things that we need. To create the discord bot. It is very complex. So it is not something I am going to cover. I am going to teach you how to use it. So create a variable called const. You can use the var keyword. Like var. Like that. Or let keyword. But this variable. Will be changed during runtime. For the name const. Prevents that from happening. Because after giving. A value to const variable. You can never change the value of it. It goes to return an error. And your application is going to crash. Basically it won't even start. When you save the code. And you try to run it. That is the purpose. To never change the value. Of this variable. And variables need to have meaningful names. I am going to give it a name. Discord. That is equal to. What we have installed. And we need to. Use the require method. And this is going to require something. And it is automatically. Going to find. That we have discord.js installed. So that is what you simply write. Const discord. Equal to require discord.js. And now. We have stored. Everything about discord.js. Inside this variable. Okay. So what exactly do we want to have. From discord. Because it is quite large. And we don't really want everything. We want some specific parts. What we need. Is a client. That we can log in. So we need to create a new client. This variable. Has to be constant as well. Because when you have declared a client. You do not want to change the value of the variable. This is like discord. It is going to be named client. And this. What this is going to be equal to. Well we did store. Discord inside a variable. And we need to access something inside discord. So we got to make use of the discord variable. By writing new. Discord. Client. This is going to create a new client. And we could simply use to write like this. Client. So if you write client. We are accessing the bot. Application basically. That is what we are creating. And I can. Add functions. For example add listeners. Fetch skill preview. Fetch invite. There is a lot of things you could do here. But we need to access the. Function called login. Here. You need to paste your password. Which is this password. The token. We paste that there. Now we have all we need. To login the bot. Except for one thing. But let's try to login the bot. And see what the console is going to say. Determinal. So here. You need to write. Node. Is the command. Follow by the name of the file. Which is app.js. And if you press enter. It tries to run. The app.js file. And it says. Client missing intents. Because we need to add intents to the spot. In order to. Have it up and running. Because there are certain things that we need to access. Here we have two links. We have gateway intents. This is the structure. Intents. This is what we are going to write. Open up the client again. Here. Add the brackets. And write intents. With array brackets afterwards. And then here. These are the intents that we need to add. However. What do we want to add? That is something we need to figure out. If I go back to gateway intents. I go to gateway. Discore develop portal. The documentation. And here we need to scroll down to gateway intents. List of intents. And intent. Is used to access parts of. The. Properties. The objects. The events etc. So if you would like to access all the guild members. For example. Where the guild member joins the server. You need to use the guild members intent. What we are going to do. Is that we are going to write a message. A specific command. And a bot. Is good to respond to your message. So what intents do we need? Well we do need to access guilds. Because that is the discord server. In its entirety. So guilds intent. Let's begin with writing guilds. Let's redo that. And simply write guilds. That is the first intent. But we are not done yet. Guild members. Guildbands. Guildmojus. Guild integrations. Well none of these have anything to do with messages. Except this one. Guild messages. And this. Is the event. That we need to keep track of. The message create. In order to receive. That event. In order to receive that event. We need to have guild messages. Guild. Messages. And there is one more that we need. If you want to write to the bot. In direct messages. And that is. Direct message. Direct messages. We have the same event here. So they are. On the same function. Like the return function. When you listen for an event. It is going to be fired. If it is written in the server. Or in dms. It is going to be the same code. But you separate them with if statements. Before we end this lecture. Let's add direct messages. Here. We add direct messages. Ok. Now. Let's run the code again. Just to see if the bot is going to be online. And you can see it looks like it's frozen. It's just stuck there. Well. If you go to your discord server. You can see that the bot is online. The bot is up and running. But it cannot really do anything. Because we have nothing. We have nothing here basically. We are simply just created. A new client. In this lecture. We will be adding partials. And you can see this warning text. Make sure. You enable all partials you need for your case. If you miss one. The event does not get emitted. So without partials. The event will not be emitted. And we need to add. Those that. Have any impact on. Messages. In dm. In server. Etcetera. We need to look at the list. Because we can find the list here. If I separate that with a comma. And I write partials. And let's move that to a new line. Like that. And if I write quotes. You see we automatically find out. Which ones exist. These are the partials that exist. These are the partials that exist. And we are going to add. Message. And we are also going to add channel. Those are required. However. I would suggest. To add all of them. Except for. Let's see. I made a mistake there. I would add all of them apart from guild scheduled event. And the last one is going to be reaction. And the last one is going to be reaction. This makes sure that whenever you continue developing the bot. This makes sure that whenever you continue developing the bot. You are going to have all the action. You are going to have all the action. You are going to have all the events emitted. Because you have the correct intents. And you have the correct partials. All right. Now we need to access an event. That is going to display a nice message. Whenever the bot comes online. So if I run the code. You can see we have no message or anything. When the bot is actually. Online. When you start the application. I'm going to press control C. I'm going to press control C. To stop the execution. To stop the server from running. And now I would like to access an event. And how do that? Well we already have defined a client. And this client has events. That it is listening on. Or listening for. If you write client. If you write client. Start on. And it listens for event. And it listens for event. And it listens for event. And these are some of the events. Emoji create. Yield. Band add. Whenever someone gets banned. We are going to find a message. A message is. I believe deprecated. It's obsolete. It's not used anymore. A message create. Is the event we are going to look at. In a little bit. The event we are looking for. Is called ready. This is fired. Whenever your bot comes online. And then I. I simply write the arrow function. And I accidentally remove the panthesis. Just like that. Now I created an arrow function. I was a bit clumsy there. But this is what the arrow function looks like. And here I would like to do. I created an arrow function. I was a bit clumsy there. And here I would like to display a nice message. That the bot is online. The bot. Is online. Now if I run the node app.js code. And I start the server. It prints the bot is online. That's nice. We now know that the bot is online. But I would like. A nicer message. To say. The name of the bot. And. Adding a message to say. This bot is now online. And to access the name of your bot. Well the client is your bot. So the client contains information. About the name, tag. Etcetera. Client. User. Tag. Bus. Is now online. What is this going to print? Take a guess. It is going to print. Name. Hashtag and discriminator. Which is the four numbers. By running the code. It says. Responding to a spot. Is the name. Hashtag. 1301. Is now online. So this bot is now online. All right. I the next lecture. We are going to use the message create event. Whenever someone writes a message. And we will. Look for a specific command. So it doesn't respond to every. Single message. Hello there and welcome back. Within this lecture. The bot is going to respond. To a specific command. So let's get there. Let's do some programming. This is going to be very similar to the red event. Exakt. We are looking for the message create event. Let's see. So you simply write client.on. Once again. And you write message create. Notice that it is message. In a low case. And create. As a capital C. That is because Node.js. Is case sensitive. And this is called camel casing. Okay. The event. Is going to return a message. That is the object. That is returned. And then you add the arrow function. In here. The code inside. The code inside this event. Is only going to be. Fired. Whenever a message is sent. That the bot can actually read. Basically. If you have a. Next channel. Let's say call admin. And the bot has access to the channel. The bot is not going to detect the event. Because no message is created. That the bot can read. And it is quite easy to respond to a message. You simply write message.reply. Either. And let's start the server. By running node.js. And if I write. Hello. The bot is going to tag me. And say hi there. But notice that it is. It keeps spamming. The bot started spamming. So I stopped the server. And the reason for this. Is quite simple. I write a message. The bot is responding. To my message. Tagging me. But then. The bot notices that. A new message. Notices that a new message is created. But the bot is actually. Tagging. Itself. Writing hi there. And this is going to be. Resulting in an endless loop. Because for every message sent. It is going to reply. And once it has replied. It's going to see a new message is created. And it replies to that new message. So it is going to. Continue like that forever. And to stop that from happening. We can simply. Just tell the bot to not. Reply. To bots. If the user. Is a bot application. We are not going to write anything. That is easy. To write if. Message. This message. Objekt. The message object. We need to find out what that is. And we have a good documentation. For. Discord.js. That is what we are using. When we created discord bot. Discord.js. That is what we installed. Previously. I go to this link. Discord.js.org. And head over. To the documentation. This message object. Is a class. Or actually it has a class. And that class is called. Message. This is the list of classes. And the class we are looking for is called. Message. Because we would like to find out. If the owner. Or the author of the message. Is a bot or not. And message. Has a property called author. An author. Is a type of user. So that is the class. User. I can scroll up here. And click. And scroll down. And I click user. This is what we get. And in the code. Basically we are here. Message. Author. Is this class. And this class has a property called bot. That is a boolean. It returns true or false. By writing. Message. Author. We access a property that is true or false. And this need to be false. What this is saying. If the author of the message. Is a bot. This code is not going to be run. So if you are a normal user. This code is going to be fired. And we will reply to that message. Okay. Objects might be a bit tricky. But what we are doing. Is that we have an object. And that object has properties. And the property might have a property. In itself. So it can go very deep. This object. And I can print. And I can print that. Console.log. Message. Okay. Let's write. No depth.js. And start a bot. And I write the message. The bot responds. But it doesn't. Keep responding like it did previously. That is because we have added. This failsafe. That we only. Reply to the user. If the user. Is a normal application. It cannot be a discord bot. And then the line after that. We did print. The message object. Because I want to show you. That this class. Message. Contains. All the data that we simply looked at. That message. Contains all the data that we looked at. That we looked at. Which is the message.author.bot. Let's find that. Let's begin. Here starts the object. And you can see that it says message. That is because. This object is a message. Class. And then we have author. So this is message.author. And you can see that the author. Is of class. User. That is the class we looked at. And this is the property. That we accessed. Bot. And you can see that this is false. Because. I am not a bot. So if it is true. The user is a bot. If it is false it is not a bot. So it is a regular user. And you can even see my username. Eric Peterson. But. I would like the bot to only reply. To a specific command. I don't want the bot to respond. To every single. Message. I would like to have like. Exclamation mark hello. And the bot is going to respond. And that is quite easy as well. Inside this if statement. I would like to add a second condition. That has to be met. In order to run this code. In order to run this code. To add a second condition. I write. Double and. Follow by. Message.content. Is equal to. Exclamation mark hello. Now let's do the same thing again. With the message object. We accessed something called. Content. That is a property. If we look at data here. Message.content. Is this. And you can see. There is a message that is sent. The message content is hey. Which is essentially. This message here. Okay. Now let's try and run the code again. I restart the bot. And I can write anything. It doesn't work. But if I write hello. The bot suddenly says. Hi there. All right. Well done on creating this bot. The bot is basically complete. We have done what we were supposed to do. Create this code bot. That responds to a command. That is exclamation mark hello. From now on. You can continue on developing this bot. And do whatever you want. And I hope that you learned something. New and useful. In the next lecture. I am going to add comments. And teach you about comments. But it's going to be a quite short. Short lecture. Biggest comments. Aren't really that difficult. But we will go through them in the next lecture. Hello there. And welcome to this lecture. In this lecture. I will teach you about comments. In programming. Specifically for Node.js. JavaScript. Because comments for Node.js and JavaScript are written in the same way. And they work exactly the same. And this is the last lecture in this course. Okay. So what is a comment? We have already written comments down. Up here. The token. And the invite link. However. That is a single line comment. And you can have multiple lines comment. But it is not really necessary for this project. Single line comments are okay. A comment is essentially explaining or describing what the code is doing. For example. Let's create a comment to explain this variable. Basically. This variable is the access to this code. API. This variable is doing inside this program. And then. I'm going to create another comment. And then I will talk short about it. This client variable. Well. What's the explanation for this variable? Well. Creating. A new client. From. Word. API. Intents. And partials. When I execute this file. And I have the node server running. These green lines that are comments. They are ignored by the compiler. So they are only usable for you who are reading this code. When it comes to software development in general. Based off the projects. I believe that 60% of the importance is the coding. The programming part. Is writing comments that explains the code. Because trying to understand a program without any comments that guide you. And explain what the code is doing. Well you're going to have a headache and you will waste a lot of hours. But with proper written comments. It is much easier to understand what the program is doing. And it's going to save you from headaches and a lot of wasted hours. You can just imagine on a professional level a large company. A large program. No comments. Well. If some new employees need to figure out what the code is doing to change something. It's going to be very costly for the company. On the other hand if the program had well written comments. Maybe. Instead of spending let's say 100 or 200 hours. You're going to have that easily. That is how powerful comments are. At the further write comments we can simply use a print bot name when ready event is fired. That is basically what happens because the ready event is going to be fired once. And then we print the name of the bot. Message create event. Reply to user user is not a bot and writes the hello command. That is basically what it does. It is the message event. Message create event and we reply to a user. And here it simply says log in your bot with password slash token. This is a very basic program so that's why we have basic comments. If the function were dependent on other functions etc it will require more comments to explain further in detail how the program works. But this is the end of this course and I hope that you learned something new and that you liked this course. And I hope that you also understand how important comments are within programming even though this was a pretty short lecture. I would like to say thank you for watching this course and now you have a discord bot that can answer a command and you can further develop this bot to do whatever you want.