This tutorial assumes that you are somewhat familiar with JavaScript. You already have an account with Discord and have created your own server. You know how to open a command window.
What is Discord?
Discord is an instant messaging application that is often used by gamers. Discord consists of servers and channels. Servers are a group of channels, which are individual chat rooms with a particular topic.
Servers can be public or private. When a server is public, anyone can join if they follow the link to that server, and links can be generated by anyone in that server. The links can be time-based, so they expire after a certain period so that links can’t be left on the web for anybody to follow. When a server is private, only users that the server owner or server admin invites can join.
Users can be given roles by the server admin or owner, and permissions for individual channels can be configured by role, so that some users can’t see certain channels. Roles can be granted some of the abilities of the server owner or admin, such as removing or banning users, removing comments, or creating or deleting channels.
Why might one want to create a bot?
Bots can fulfill various tasks. For example, you can create a bot to play a text-based game with users in the server, or have it translate text for users. It could play music in a channel or it could automatically moderate by removing comments with particular keywords. A common use for bots is to regurgitate text, like rules for the server or instructions on how to do tasks within the community. This is the sort of bot we will create in this tutorial, as the logic is very simple (receive a command, respond with set text). Once you have mastered this you can try making something more complex.
What is Node.JS?
Node.JS is an open-source JavaScript runtime environment for running web applications outside of the browser. It is used to write server-side applications. Being server-side, it can do tasks that are independent of the users using the application, and it can be used for data-intensive applications since it uses an asynchronous, event-driven model.
Create a Discord Application
First of all, go to the Discord Developer Portal: https://discord.com/developers/applications and click the button labelled ‘New Application’.

A screen will open prompting you to choose a name. Enter anything you like, but in this tutorial, we will assume you chose “catbot”. Tick the checkbox to agree to the terms and click ‘Create’.

Now you can customise the icon for the bot and the description, under ‘General Information’.

Under the ‘Bot’ menu item, scroll down and enable ‘MESSAGE CONTENT INTENT’. This will allow the bot to read messages in the server it’s in (otherwise it can only ready messages where it is tagged or in private messages with users). Then click ‘Add Bot’ to add the bot user to your application.

On the next screen, you will be given your bot’s secret token. We need this so that our Node.JS application will control this bot. Click ‘Reset Token’, click ‘Copy’, and paste it somewhere where you will be able to find it later.
On the ‘OAuth2’ menu item, we can obtain the client ID and client secret so the bot can be authenticated to the application. Click ‘Reset Secret’, then copy these and paste them somewhere safe for later.

Go to the ‘URL Generator’ menu item which is under ‘OAuth2’. Check off the ‘bot’ scope and then choose the permissions you wish to give your bot. To fulfill the requirements of this tutorial, check off ‘Send Messages’ (to be able to post in the server), ‘Create Public Threads’, ‘Create Private Threads’, ‘Send Messages In Threads’, and ‘Read Message History’. You may want to check off more when you finish this tutorial and want to try expanding the capabilities of your bot.

Copy the URL at the bottom of the page, this is used to authorise the application to a server. Visit it with your browser, and the following window will appear:

Select your server from the drop-down menu and click ‘Authorise’.
Build the bot with Node.JS
Now that you have created the bot and added it to your server, you can start actually programming it.
Step 1: Create directory for your code
Navigate to a folder where you want to keep your code, and create a new folder. The tutorial will assume you called it ‘discordbot’.
Create new files in this folder and call them discordbot.js and .env. Make sure that file extensions are visible. In the .env file, add:
CLIENT_TOKEN=the_client_token_saved_earlier
And save.
Step 2. Install Node.JS
If you already have Node.JS installed, you can skip this step. If you are already able to use npm (Node Package Manager) you likely have Node.JS. To test, type the following in a command window:
node -v
And
npm -v
If you get a version number after each of these commands, you have Node.JS already. If you get something along the lines of “This command is not recognised” you will need to install Node. Please have a look at the following link: https://nodejs.dev/en/learn/how-to-install-nodejs/ and return when you have installed Node.JS (and check as per above).
Step 3. Install the discord library from NPM
npm install discord.js dotenv
Run the above command. This will install the discord library into the newly created node_modules folder.
Step 4. Log in your bot
Add the following code to your discordbot.js file:
// Initialize dotenv require('dotenv').config(); const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] }); client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); }); // Log In our bot client.login(process.env.CLIENT_TOKEN);
Now you can try running discordbot.js in the command window. The bot will come online in the server, and you should get something like the following message:
Logged in as catbot#0000!
Step 5. Add functionality to your bot
Now we will get the bot to actually do something interactive. To have the bot respond to messages, you can use:
client.on('messageCreate', msg => { //logic and responses here }
We can signify commands that the bot should pay attention to with a character before the message. For example, ‘#’. So, for example, if a user types #help login, the bot can respond with login instructions (for whatever software or game the server is for). So we will tell the bot to look out for messages that start with this character. Add the following to your discordbot.js:
client.on('messageCreate', msg => { if(msg.content.charAt(0) == '#') { let words = msg.content.split(' '); //divide message into words switch(words[0]) { case '#help': switch(words[1]) { case 'login': msg.reply('To login, go to this link https://link.com and put in your username and password and click Submit'); break; case 'logout': msg.reply('To logout, click the logout button'); break; default: msg.reply('Sorry I don't understand what you need help with'); break; } case '#rules': msg.reply('The one rule is to be nice to others'); break; default: msg.reply('Sorry, I don't understand that command'); break; } } //if no # character used, ignore the message and do nothing });
Test out your bot. It should now give you different responses depending on which commands you give it. If you would like to add more functionality to your bot besides just responding with text, this documentation explains more ways to use the discord.js library: https://discordjs.guide/popular-topics/faq.html

If you would like to have your bot running all the time, not just on your local computer, see this guide to running Node.JS apps on Heroku (free): https://devcenter.heroku.com/articles/getting-started-with-nodej