How To Create A Discord Bot Using Node.js

Discord is a popular messaging app primarily used for video game communities. It has grown and evolved into a platform where users can still chat, message each other, create servers and channels, and congregate, but also create full fledged apps! I am going to walk through how to make a Discord bot in this article, a popular type of app.

To make a Discord App, we are going to use Node.js. Node.js is a Javascript runtime driven by events. It has become very popular over the years, so any experience you have with it can help. To get this bot up and running, we will need to do the following:

  • Download and install Node.js on our local machines
  • Create a Discord account and create a server and a bot application
  • Write a Node.js file to define what the bot does
  • Run the bot in the command line!

Before we get into the steps, let me clarify that this bot will listen to a channel in Discord and respond to messages automatically as they are posted. Once we get the bot up and running, I will show you how you would wire it up to fire on many events, not just messages coming into a channel.

Step 1 – Install Node.js

Head over to download and install Node.js. Pick the installer for your operating system and install it as you would any application.

Step 2 – Create A Discord Account, Server, and Bot Application

Before we do any coding, let’s head over to Discord and get the infrastructure we need up and running. Head over to Discord and create an account if you don’t have one. You will also have to verify your email to be able to create apps and bots. Once you login, you will see a screen like this:

Discord Home Screen

Click on the ‘+’ button on the left sidebar to create a server:

Discord Create A Server

Select ‘Create A Server:’

Discord Create A Server Prompt

And I will call mine ‘Bot Test Server.’ Great! Now that we have a server and channel setup, head over to the Discord developer portal. Login with your Discord account and you will see the following screen:

Discord Developer Home

Create an application by clicking ‘New Application’ in the upper-right corner;

Discord Create New Application

Let’s call it ‘My First Bot.’ After you create it, you’ll see you have extra options on the left-hand sidebar:

Discord Application

Click on the ‘Bot’ tab and create a bot by clicking ‘Add Bot’:

Discord Bot Home

Accept the prompt:

Discord Bot Prompt

And you’ll have your first bot!

Discord Bot Home

Your bot will now need to be added to your channel. The way Discord allows this to happen is through the OAuth2 standard. You will pick what type of authorization you are seeking and what permissions you want to have access to. You’ll see an ‘OAuth2’ tab on the left-hand bar. Click on it to configure your bot:

Discord OAuth2 Permissions

There is a section called ‘scopes’ where you will select ‘bot.’ Under ‘Bot Permissions,’ select ‘Send Messages’ and ‘Read Message History.’ This generates a URL in the middle of the page that you will paste in your browser. It will then take you to a screen where you can add this bot to channels you have permissions to add bots to. In this case, select the ‘Bot Test Server’ channel and your bot will be added!

Discord Add Bot To Channel
Discord Bot In Channel

If done right, you will see your bot on the right-hand side of the channel, offline. To get the bot online, we will write our Node.js code!

Step 3 – Write Our Bot

To begin, pick a folder for your files to be stored in. Node.js applications are made up of a Javascript file, a package.json file, and whatever libraries are pulled in by the code. The Javascript file is what we will write and it acts as the main logic behind the bot. The package.json is a standard file for Node.js files that describes the architecture of the app. It will be created automatically in this tutorial.

Once you have picked a folder to work out of, navigate to that folder using the Command Prompt. You can get to the Command Prompt by searching in the search bar on Windows Machines:

Windows Command Prompt

My bot files will live in the folder shown below:

Windows Node.js File Directory

Open the Command Prompt and navigate to whatever folder you are using. Use ‘cd’ and then type in the file path to get to your folder:

Windows Command Prompt Navigation

Then, run the following command:

npm init

npm is the Node Package Manager. Since you previously installed Node.js, this command is available and it is a robust package initializer. You will be asked some questions, and you can enter answers by typing and pressing enter. When you get to specify the ‘entry point,’ call it whatever you want your Javascript file to be. In this case, I entered ‘discordbot.js.’

Node package.json

If you check your directory, you will see the package.json file:

Windows File Directory Node.js
Node.js Package.json Discord Bot

Now, create a file called ‘discordbot.js’ in the same folder using your preferred text editor (I prefer Sublime). Then, paste in the following code and save the file:

var Discord = require('');
var logger = require('winston');

logger.add(new logger.transports.Console, {
    colorize: true
logger.level = 'debug';

var bot = new Discord.Client({
   token: [YOUR BOT TOKEN],
   autorun: true

bot.on('ready', function (evt) {'Connected');'Logged in as: '); + ' - (' + + ')');
bot.on('message', function (user, userID, channelID, message, evt) {'msg');	
        to: channelID,
        message: 'I am a bot'

The above code will create a Discord object from the library. This will be the major library you will use to preform most of the bots functions. winston is a popular logger for Node.js, so we are using it here to debug out statements to help us figure out what is going on. The token here is the bot token that is on your bot’s page in Discord. Go back to it, copy that token, and paste it within double quotes. Finally, the two events are events that are defined in They fire when the bot comes online (which happens when the Javascript is run) and when a message appears in the channel.

Now, we are almost ready to run the bot. Before we do, we have to import the files we require at the top: and winston. To do this, open the Command Prompt and navigate to the folder our files reside in. Then, run the following commands:

npm install winston --save
npm install
Command Prompt Node.js Install Requirements

Now, if you look at your file directory, you will see the libraries are in a folder called node_modules:

Node.js File Directory Files Installed

Step 4 – Run The Bot!

Now, simply run the following command in the Command Prompt, while still navigated to the folder your files are in:

node discordbot.js

You should now see you bot come online in your channel:

Discord Node.js Run Bot File
Discord Bot In Channel Online

Now, if you type a message, you will see the bot respond and an event log in your console:

Discord Bot In Channel Working
Discord Bot Node.js Logging

And there you go! You can now read the documentation and see what else you can do. You are not limited to just triggering on messages. The bot can do much more. You can fire on all sorts of events and give the bot permissions to respond to events, create channels, and manage your discord server!

Leave a Reply

%d bloggers like this: