Send your first Discord.js Bot Event

Lucas Minter
InstructorLucas Minter
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Now that we've set up a bot on our Discord server, let's write some code to get it do some fun stuff.

We're going to set up a new project and use the Discord.js library. While setting up this project you'll learn how to properly configure the client and log in to your bot by passing a Discord API token to client.login.

Once the project is set up we'll send our first event using the client.once and event, and set up some scripts so that we can start the bot using yarn.

Throughout the lesson, you will be introduced to important concepts and best practices for building Discord bots. By the end of the lesson, you will have a basic understanding of how to create a simple bot and be ready to continue building

Instructor: [0:00] Now that we've added our bot to our discord server and it's fully configured, we need to add some code to it to make it do some fun stuff. Let's go ahead and make our directory.

[0:12] We'll name it egghead discord bot course, CD into that. Now we're going to run it yarn init -y to go ahead and auto-answer all of the defaults.

[0:33] Go ahead and open it up in VS Code. Let's go ahead and open up our terminal again, and we're going to add in our dependencies. What we're going to add is discord.js.env for environmental variables and nodemon for our development testing.

[1:04] Perfect. Now, let's create a main file where we're going to do our initial stuff, main.js.

[1:15] In here, we're going to import a couple of things from discord.js, const Client, Events, and GatewayIntentBits. We're also going to require our .env. Perfect. Now that that's configured, let's create our client, const client = new Client, intents.

[2:03] This is where we're going to select what permissions we're going to have for our initial commands. First of all, we're going to do, GatewayIntentBits.Guilds.

[2:23] Now that that is set up, we're going to log in as a bot. That will be client.login, and then we're going to need our TOKEN. If we go back over to the developer portal, go over to the bot, hit Reset TOKEN. Yes, we want our TOKEN.

[2:46] Enter your two-factor authentication if you still have it, and that will display your TOKEN. We're going to copy our TOKEN, head back over here. We're going to open up .env file, and we're going to set this to TOKEN, paste in our TOKEN. Head back over to our main file, and we'll add that in here, Process.Env.TOKEN.

[3:19] With all that set up, let's go ahead and send our first event, client.once -- since we always send this once right when we log in, or startup our bot -- Events.ClientReady, c, console.log, Ready! Logged in as, c.user.tag. Let's head over to our package.json file. We're going to add in some scripts.

[4:04] First one is going to be our "start", "node main.js". The second one is going to be our dev. That is going to be using node one, main.js.

[4:20] With all that setup, let's go and start our bot, yarn dev. There we go. Ready! Logged in as the best book club bot.