How to make a Discord bot using Discord.JS v13 - #1

discord.js v13 Aug 21, 2021

How to make a Discord bot

In this article we will be creating our Discord bot and getting it up and running. In future tutorials we will cover more advanced topics. For now you can check out existing articles and YouTube videos.

The corresponding YouTube video for this blog post can be found here.

Prerequisites:

  • An understanding of JavaScript basics.
    • If you are new to JavaScript then check out my complete JavaScript course. The first hour is on YouTube for free here.

Need help?

If you have any questions you can ask in our Discord community. Be sure to read the "how-to-ask-for-help" channel near the top of the server.


Table of Contents

  1. Getting the right tools
  2. Creating our Discord bot
  3. Creating our test Discord server
  4. Setting up our project
  5. Writing the code
  6. Complete Source Code

Step 1) Getting the right tools

Before we start developing our own bot we need to have the proper tools for the job. The first tool we will need is Node.JS version 16.6 or higher, you can find a link to that download here.

Next we need somewhere to actually write our code. I use Microsoft's Visual Studio code which is free and can be found here.

Both of these have standard installers and all of the default installation settings should be fine to use.


Step 2) Creating our Discord bot

To create our bot we need to login to the official Discord developer portal. You will then see a "New application" button at the top right like this:

From there you will be asked to provide a name for your application. I'll name mine "WOK Tutorial" but you can name yours whatever you want.

You will be redirected to a page with different options for your application. We are wanting to turn our application into an actual Discord bot. To do that we can go to the "Bot" tab on the left:

Click on the "Add bot" button the right side of your screen. It will create a confirmation popup like this one, simply click on "Yes, do it!"

From there you have some important options for your bot. You can change the profile picture, name, and access the bot token.

IMPORTANT: Your bot's token is basically it's password. Never share your token with anyone. If you believe your token has been compromised click "Regenerate".

Directly below you will see a checkbox for "Public bot". Make sure to uncheck this if you don't want anyone else inviting your bot to their servers. This is ideal for testing or bots intended for your server only.


Step 3) Creating our test Discord server

We now need a place to test our bot, it is recommended that you create your own private test server. To do that you can go to the bottom left of the Discord app and click on the plus icon:

You can then click on "Create my own", followed by "For me and my friends", and then give your server a name. I'll name mine "WOK Tutorial".

Now that we have our own private Discord server, we can invite our bot to it. Go to the "OAuth2" section of the Discord developer portal:

You now want to scroll down to "SCOPES" and select "bot" and "applications.commands". This will make your Discord app into a bot that has access to the new slash commands feature which we will cover in a future tutorial.

Below "SCOPES" you will see "BOT PERMISSIONS" where you can select what permissions your bot will need. When publishing a real bot is it important to only require the necessary permissions. This way server owners don't have to worry about what your bot could do to their server with too many permissions. However while learning we can select the "Administrator" permission to keep things simple. This will give us access to everything we could need.

Once you have selected these three checkboxes your screen should look like this:

At the bottom of the "SCOPES" section you will see a URL with a copy button. This is the link you can give to server owners in order to invite your bot. Simply open this link in a new tab and select your new testing server.


Step 4) Setting up our project

We need to setup a new Node.JS project for our bot. First create a new folder in a convenient location and then open Visual Studio Code (VSCode). From there you can drag and drop your folder into your workspace, highlighted here:

Doing so will make VSCode act like it refreshes, you should seen see 4 icons like so:

VSCode will now show all the files and folders within your newly created workspace folder. Because we just made this folder there are no files listed, but as we add files we will see them here.

To create our project we need to open our terminal. You can do this by clicking "Terminal" then "New Terminal" on the top of VSCode like so:

This will open a terminal/console near the bottom of your screen. From here we can use different commands to help create our project. If you have never used a console before don't worry, what we need to make our project is fairly simple.

Start by initializing our Node project with:

node init -y

This with initialize our node project. The -y flag will accept all the default questions it will ask you. If everything worked correctly this should create a "package.json" file in your explorer. Don't worry about what's inside of this file as long as it was created successfully.

We can now install the 3rd party dependencies we need such as Discord.JS:

npm install discord.js dotenv

This tutorial series will use Typescript which is a superset of Javascript. This means it extends the functionality of Javascript and tries to make our lives easier. If you are new to Typescript or Javascript don't worry, we will only be using very basic Typescript in this tutorial series. To install it you can use the following command in your console:

npm install -g typescript ts-node

Note: If you run into permission errors on Windows you may need to run VSCode as an administrator.

Because we want to access Typescript anywhere on our computer, we use the -g flag to install it globally. The "ts-node" package will allow us to run our Typescript files while developing. This makes our lives easier for now, but once we actually deploy our bot (in future tutorials) we will be using a slightly different workflow.

We can now initialize a Typescript project with this command:

tsc -init

This will create a "tsconfig.json" file in our project.

Next we need a place to write our own code. At the top left you can click on the new file icon, name the new file "index.ts" like so:

Note: If you are not using Typescript then create a file named "index.js" instead.

We also need a place to store our bot's token in a secure way. This is why we installed the "dotenv" package earlier. You never want to store passwords, tokens, etc in your source code files. You always want to import those from some external file when possible. This way if you need to share your source code you can simply exclude the file that has your token.

Make a new file called ".env" like so:

Inside you can add in the following text. Be sure to replace "YOUR_TOKEN" with your bot's actual token from the Discord developer portal.

TOKEN=YOUR_TOKEN

Never share your token with anyone OR push your .env file to version control.

Now that our project is setup it's time to write the code!


Step 5)  Writing the code (finally)

We are finally ready to start writing some code inside of our index.ts file.

We need to start by importing the 3rd party dependencies we installed earlier:

import DiscordJS, { Intents } from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()
 
This will give us access to the libraries so we can use them. The dotenv.config() will give us access to our token within our ".env" file.
 
Next we need to make a new client which represents our bot:
 
const client = new DiscordJS.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES
  ],
})
 
Intents are used to tell Discord what information our bot needs. This way we don't have unnecessary information being sent to our bot which slows it down and eats up our bandwidth. A full list of possible intents can be found here, but it is important to only use the ones you need.
 
Next we can listen for when our bot starts up, as well as listen for when someone sends a message. To keep things simple we can listen for the message "ping" and reply with "pong" like so:
 
// Listen for when the bot is ready
client.on('ready', () => {
  console.log('The bot is ready!')
})
// Listen for new messages
client.on('messageCreate', (msg=> {
  // Was the message "ping"?
  if (msg.content === 'ping') {
    // Reply with "pong"
    msg.reply({
      content: 'pong',
    })
  }
})
Finally we have to login to our bot using our token:
 
client.login(process.env.TOKEN)
 
We can finally run out bot by using this command:
 
ts-node index.ts
 
If you are not using Typescript then you can run your bot with:
 
node index.js
 
Your bot should be online in your testing server. If you type "ping" in a channel it should reply with "pong" like so:
 
 
This is obviously a very basic example, but we now have a Discord bot online and listening to messages.

Complete Source Code

Save time by accessing all of our Discord.JS tutorial source code by becoming a YouTube member for as little as $2.99. Learn about all of the perks we offer here.

Already a member? Download the source code here.

 

Close

50% Complete

Two Step

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.