VOOZH about

URL: https://www.digitalocean.com/community/tutorials/how-to-build-a-discord-bot-with-node-js?comment=170781

⇱ How To Build a Discord Bot with Node.js | DigitalOcean


👁 How To Build a Discord Bot with Node.js

The author selected the Free and Open Source Fund to receive a donation as part of the Write for DOnations program.

Introduction

Discord is a chat application that allows millions of users across the globe to message and voice chat online in communities called guilds or servers. Discord also provides an extensive API that developers can use to build powerful Discord bots. Bots can perform various actions such as sending messages to servers, DM-ing users, moderating servers, and playing audio in voice chats. This allows developers to craft powerful bots that include advanced, complex features like moderation tools or even games. For example, the utility bot Dyno serves millions of guilds and contains useful features such as spam protection, a music player, and other utility functions. Learning how to create Discord bots allows you to implement many possibilities, which thousands of people could interact with every day.

In this tutorial, you will build a Discord bot from scratch, using Node.js and the Discord.js library, which allows users to directly interact with the Discord API. You’ll set up a profile for a Discord bot, get authentication tokens for the bot, and program the bot with the ability to process commands with arguments from users.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Former Senior Technical Editor at DigitalOcean, with a strong focus on DevOps and System Administration content. Areas of expertise include Terraform, PyTorch, Python, and Django.

Still looking for an answer?

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Not recommended.

No command handler (all commands in the main file). No informations about intents (very important when the bot hit 100+ servers). Nothing about slash commands. So it’s not “How To Build a Discord Bot with Node.js” but “how to build a very useless basic discord bot with node.js”.

Don’t waste your time and take a look on YouTube.

Additional Settings

If you aren’t able to make the bot an administrator, then at least give it as many options possible under the “text permissions” column. When you follow the link to add the bot to the channel, make sure there is a green checkmark next to each of the items you selected. If you notice any denied permissions then your bot will not be able to do those activities. You may have to adjust your permissions on the server you are attempting to add the bot.

There are also new setting for a bot you may want to adjust in the https://discord.com/developers/applications/

  • You may want to uncheck Public Bot if you want to control what servers the bot goes to
  • Check the items under the privileged gateway intents (Presence intent, server members intent, message content intent)

Updated Script for discord.js v14

Here is a new code snippt using discord.js v14

// Require the necessary discord.js classes
const { Client, Events, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');

const client = new Client({
 intents: [
 GatewayIntentBits.Guilds,
 GatewayIntentBits.GuildMessages,
 GatewayIntentBits.MessageContent
 ]
});

client.on("messageCreate", (message) => {

 if (message.author.bot) return;
 if (!message.content.startsWith(prefix)) return;

 const commandBody = message.content.slice(prefix.length);
 const args = commandBody.split(' ');
 const command = args.shift().toLowerCase();

 if (command === "ping") {
 const timeTaken = Date.now() - message.createdTimestamp;
 message.reply("Pong! This message had a latency of " + timeTaken.ms + ".");
 }

 else if (command === "sum") {
 const numArgs = args.map(x => parseFloat(x));
 const sum = numArgs.reduce((counter, x) => counter += x);
 message.reply("The sum of all the arguments you provided is " + sum + "!");
 }
});

Run script in background forever

If you want your script to always stay running on your server in the background, look into the forever npm package: https://www.npmjs.com/package/forever

Once you install it you can run your script: forever index.js

If you want to see what scripts are running you can use forever list

If you want forever to watch your file for changes and automatically reload it, you can run it with the -w hook:

forever -w index.js

This comment has been deleted

If you want to node script to run forever in the background, checkout the forever npm package: https://www.npmjs.com/package/forever

If you want forever to watch your file for changes and automatically reload, run it with the -w flag:

forever -w index.js

👁 Creative Commons
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
  • Deploy on DigitalOcean

    Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and AI-native businesses

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

© 2026 DigitalOcean, LLC.Sitemap.
Dark mode is coming soon.