Build a Discord Bot with discord.js v14: Tutorial and Starter Template (2026)
Want to code your own Discord bot? With discord.js it's quicker than you'd think. In this tutorial you create a bot, write your first slash command and bring it online. At the end, we show how a starter template takes the tedious groundwork off your hands.
What you need
- Node.js (discord.js 14.27.0 declares Node 18 or newer on npm) and a code editor
- Basic JavaScript: variables, functions,
async/await - A Discord server where you can add bots (ideally your own test server)
Step 1: Create the bot in the Developer Portal
- Open the Discord Developer Portal and click Create App.
- Enter a name; that's what your bot will be called.
- On the Bot page, click Reset Token and copy the token. Discord won't show it again unless you regenerate it.
- Note the Application ID on the General Information page.
The token is your bot's password. Under Discord's Developer Terms (Section 2(d)), you must treat it as confidential, and it may not be embedded in open-source projects. It belongs in a .env file that never goes into Git.
Step 2: Set up the project
mkdir my-bot && cd my-bot
npm init -y
npm install discord.js dotenvCreate a .env file:
DISCORD_TOKEN=your-token
CLIENT_ID=your-application-id
GUILD_ID=your-test-server-idTo copy the server ID, enable developer mode in Discord, right-click the server title and choose "Copy ID". Add .env to your .gitignore right away; the discord.js guide warns not to commit it.
Step 3: Register your first slash command
Slash commands must be registered with Discord before they show up. The discord.js guide recommends deploying to a single server (guild) for development and testing. File deploy.js:
require('dotenv').config();
const { REST, Routes, SlashCommandBuilder } = require('discord.js');
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Replies with Pong!'),
].map((c) => c.toJSON());
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
rest
.put(Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID), { body: commands })
.then(() => console.log('Commands registered'))
.catch(console.error);Run it once with node deploy.js. When you're ready for all servers, swap the route for Routes.applicationCommands(CLIENT_ID).
Step 4: Write the bot
File index.js:
require('dotenv').config();
const { Client, Events, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once(Events.ClientReady, (readyClient) => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
});
client.login(process.env.DISCORD_TOKEN);The Guilds intent is what the guide's starter client uses. To receive messages and read their content (for a filter, say), you need Guilds and GuildMessages plus the privileged MessageContent intent, which you enable in the Developer Portal.
Step 5: Invite and try the bot
In the Developer Portal, open the Installation page. Under Default Install Settings, add the applications.commands and bot scopes for Guild Install and pick the permissions your bot needs (to start: send messages). Use the Discord Provided Link to add the bot to your test server. Now run node index.js and type /ping in the server.
From test bot to real project
One index.js holding everything gets messy fast. Real bots need:
- A command loader: each command in its own file under
commands/, loaded automatically. - Central error handling, so an error in one command doesn't crash the whole bot.
- Logging, ideally structured (JSON), so you can find problems.
- A deploy script for global commands and a test server.
- Graceful shutdown: stop cleanly instead of dying mid-reply.
- A Dockerfile, so the bot runs the same everywhere.
Building all of that yourself can take a weekend before you even touch your actual idea.
Shortcut: Discord Bot Starter for €19
The Botnexio Discord Bot Starter is exactly that foundation. Per the product page it includes:
- discord.js v14 with an automatic slash-command loader
- Example commands
/ping,/help(lists all commands) and/poll(reaction poll) - Central error handling and JSON logging
- A script to register commands, globally or on a test server
- Graceful shutdown and a Dockerfile
Deliberately not included: database and dashboard. Delivery: ZIP right after payment. Licence: Personal, non-commercial use only; reselling or redistributing the template is not allowed. Price: €19 incl. VAT.
Prefer a finished bot? The Anti-Scam Bot (€25 incl. VAT) runs on your own server too. For the buy-vs-build decision, see Buy or Commission a Discord Bot? (in German).
Keeping the bot online
While node index.js runs on your PC, the bot is online. Turn the PC off and it's gone. For 24/7 operation:
- A small VPS: the usual route. With Docker you start the bot there with one command.
- A home server or Raspberry Pi: cheap if it's running anyway.
- A process manager: restarts the bot after a crash (Docker can do this with a restart policy too).
Common errors
- Command doesn't show up: the bot was invited without
applications.commands, ordeploy.jswasn't run. [DisallowedIntents]error: you request a privileged intent that isn't enabled in the Developer Portal.- Interaction fails on slow commands: an interaction token is only valid for three seconds for the first reply. For longer tasks, call
interaction.deferReply()first. - Token leaked: regenerate it in the portal right away with Reset Token.
FAQ
Can I create a Discord bot without coding?
With ready-made bots, yes: you fill in a .env file and start it. For custom features you need basic JavaScript.
Is discord.js free?
Yes. discord.js is open source under the Apache-2.0 licence. Costs only come from hosting if you rent a server.
Why discord.js v14?
v14 is the current major version on npm (14.27.0 as of Oct 16, 2026), and the discord.js guide's examples are written for it.
Do I need a database?
Not for simple bots. Once you want to store data permanently (levels, settings), one is worth it. The Discord Bot Starter deliberately leaves it open.
Can I sell my bot later?
You can sell your own code. Follow Discord's Developer Terms and Developer Policy, and sell code, not your bot application: transferring an application needs Discord's prior written consent.
Less setup, more bot
Skip the groundwork. Build your idea.
→ Get the Discord Bot Starter for €19
Sources
- discord.js Guide: Project setup, Registering commands, Main file, Gateway Intents, Response methods
- Discord Developer Docs: Building your first Discord Bot
- Discord: Developer Terms of Service
All checked October 16, 2026.
---
Botnexio is a marketplace operated by Aramac GmbH and is not affiliated with or endorsed by Discord Inc. or the discord.js project. Discord is a trademark of Discord Inc.