Building A Motivating Discord Bot In Node.js

by Chris Bongers

7 min read

Teams making it their own, gamers living there online, friends doing their online hangouts. Yes, we're talking about Discord, the communication tool with more than a quarter-billion users.

No wonder you see many people talking about it, making tools work with it, and notice integrations pop up everywhere.

And we're going to be a part of that because today, we'll learn how to make our very first Discord bot!

And not any bot, a motivating bot for when we feel down. Because developer happiness is very important ā¤ļø.

Prerequisites

To get started with this project, Node.js developers will need the following knowledge and tools.

  • Using the command line
  • Discord client setup and ready to go
  • Basic knowledge of Node.js applications

Don't worry, it's not too technical and explained step-by-step for you.

Setting Up a Discord Bot

Let's start with the configuration side. We'll be setting up a Discord bot, but let's make our test server for our new bot before we get there.

Open your Discord client and click the plus icon:

Adding new discord channel
Adding new Discord channel

Please fill out the details as to what server you'll be creating. For this article, it doesn't matter what you choose.

Once that is done, and your server is up and running. Then we can head over to the Discord Developer Portal.

Click on the New application button on the top right of the screen:

New Discord application
New Discord application

On the pop-up, fill out whatever your app should be named. I used discord-test-bot.

In the left menu, click on the Bot item, and click the Add Bot button:

Discord new bot
Discord new bot

You can then give this bot an image and name, making it friendly and personal!

Another critical step is to make sure we give the app and our bot the correct rights.
To manage the rights, head over to the Oauth2 section.

For scopes, turn on the Bot scope. Then scroll down and select Send Messages and Read Message History:

Bot access rights
Bot access rights

Then we need to add this bot to the server we just created. We can do this by copying the link under the scopes section and opening that in a separate window.

It will pop up a select, where you can choose what server you like to add the bot to:

Add bot to discord channel
Add bot to Discord channel

The last thing we need from the developer portal is the token. We can head back to the Bot menu item and click the copy button on the token part:

Copy Discord bot token
Copy Discord bot token

We'll use this token to give our bot access to do something.

Creating the Node.js Discord Script

Alright, now that this admin part is done, let's start on our Node.js script.

Open your favorite terminal and create a new project directory and initialize node.

mkdir discord-bot && cd discord-bot
npm init -y

Note: The -y parameter answers all questions of the CLI with yes (saves us some enters).

Next, we'll be using a couple of NPM packages, so let's install those using our terminal.

npm install axios discord.js dotenv

This will install the following packages, which we'll use for:

  • axios: Making http requests to the quotes API
  • discord.js: The Discord NPM package
  • dotenv: Enables environmental config files

With this in place, let's create our .env file first. This file will hold the Discord bot token we just copied.

TOKEN=YOUR_TOKEN

Note: Replace YOUR_TOKEN with the actual token you got from the bot setup.

Now, create an index.js file in your editor. The first thing we'll add is all our config.

require('dotenv').config();
const Discord = require('discord.js');
const axios = require('axios');
const TOKEN = process.env.TOKEN;
const bot = new Discord.Client();

As mentioned, we won't make an ordinary bot. Our bot will be responding to people who seem to be sad on Discord.

For this, we'll create an array of words that sound sad. You can, of course, enhance this list to your wish.

const targets = ['sad', 'angry', 'unhappy', 'miserable', 'down'];

The idea is that when a user uses any of those words, we lift them with a quote.

The next part will be to log in to the bot. This is something the Discord NPM package handles for us, so we have to call the following line of code.

In this line, we call the login function of the bot and pass our token to it.

bot.login(TOKEN);

Then we can also see if our bot is connected by executing the following script.

bot.on('ready', () => {
  console.info(`Logged in as ${bot.user.tag}`);
});

But more importantly, we want to listen to messages being sent on the Discord server and react to those.

To listen to messages, we use the following function.

bot.on('message', (msg) => {
    // Execute code
});

We want to add a check to see if the message was not by any bot. Else we might create an infinite loop šŸ‘€.

if (msg.author.bot) return;

That line will stop the function when a bot posts a message.

The next part will be to see if the user used any of our target words.

const foundWord = targets.find((target) => msg.content.includes(target));
if (foundWord) {
    console.log(`Found the word ${foundWord}`);
}

A quick recap for what's going on, we use the JavaScript array method ā€œfindā€œ to loop over each of our target words. When the message includes that word, it breaks the find method and will return that word.

Example:

  • User types message: "I'm angry and sad at the same time"
  • We'll loop over our targets
  • The first hit is sad
  • We found the word sad
  • Console logs: "Found the word sad"

That is enough for now, we could check the other way around, but this is the more efficient solution.

The next step is to query a quotes API and return a random quote to uplift the person.

axios.get('https://type.fit/api/quotes').then((response) => {
  // API response inside response object
});

This is an array, so let's try and get a random item from this array.

const randomQuote = response.data[Math.floor(Math.random() * response.data.length)];

Bear with me here. We define a new variable and call the response.data array, we then pass the number of the item we want to retrieve.

As the variable, we send the Math where we get a random number with a max of all items in the object.

The last thing we need to do is respond to this user with a nice message:

msg.channel.send(`Don't be ${foundWord}\n${randomQuote.author}once said:\n${randomQuote.text}`);

And now, let's run the script and test it out!

node index.js
Discord chatbot responding with uplifting messages
Discord chatbot responding with uplifting messages

And there we go! Our Discord bot is responding with an uplifting message. The message will change every time someone mentions one of our trigger words. If you're looking for the source code, I hosted the code on GitHub.

Interested in expanding your knowledge and keeping your skills up-to-date? Here's what you should read next:

FAQs

Q: How to deploy the Discord bot on a server for continuous operation?
Deploy the Discord bot on a server by using cloud services like Heroku, AWS, or DigitalOcean. Set up a Node.js environment, upload your bot's code, and use a process manager like PM2 to keep the bot running continuously. Ensure environmental variables are configured for sensitive information.
Q: How to integrate natural language processing (NLP) to better detect user emotions?
Integrate NLP into a Discord bot by using libraries like natural or platforms like Google's Cloud Natural Language API. These tools analyze text to detect user emotions, which can then be used to tailor the bot's responses. Implement the chosen NLP solution into your bot's codebase, ensuring it processes user messages to detect sentiments or emotions.
Q: Can the bot track user engagement or response to motivational messages over time?
Yes, the bot can track user engagement or responses to motivational messages over time by logging interactions and analyzing them for patterns or improvements in user sentiment. Use a database to store user responses and engagement metrics.
Chris Bongers
Chris Bongers
Senior Software Engineer

Chris is a Senior Software Engineer with over a decade of professional experience in the industry. His main focus are technologies in the PHP and JavaScript ecosystem.

Expertise
  • JavaScript
  • Vue.js
  • ReactJS
  • PHP
  • Laravel
  • +1

Ready to start?

Get in touch or schedule a call.