Host a discord.js bot, step by step
From a bot that runs on your PC to a bot that is online around the clock. This guide shows a small discord.js project that behaves the same everywhere: on your PC, on EStage and with any other provider that builds containers.
The project
A minimal bot with one slash command needs three files: package.json, index.js and a Dockerfile. First create the folder and install discord.js:
mkdir my-bot && cd my-bot
npm init -y
npm install discord.js
Add "type": "module" to package.json so you can use import.
The bot
// index.js
import { Client, Events, GatewayIntentBits } from 'discord.js';
const token = process.env.DISCORD_TOKEN;
if (!token) {
console.error('DISCORD_TOKEN is missing');
process.exit(1);
}
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once(Events.ClientReady, (c) => {
console.log(`Online as ${c.user.tag}`);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
try {
if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
} catch (err) {
console.error('Command failed:', err);
}
});
// Shut down cleanly on a restart or a new version.
for (const signal of ['SIGTERM', 'SIGINT']) {
process.on(signal, async () => {
await client.destroy();
process.exit(0);
});
}
client.login(token);
Three things make the difference between a bot that runs on your PC and one that runs anywhere:
- The token comes from the environment, not from the code. That way it never ends up in the repo. More in the guide Keep your bot token safe.
- Errors in commands are caught. Otherwise an error in one command can end the whole process.
- The bot shuts down cleanly when it receives a signal. That happens on every restart and every new version.
Only the intents you really need: Guilds is enough for slash commands. If you need the content of messages, add GuildMessages and MessageContent, and switch MessageContent on for your bot in the Discord Developer Portal.
Registering the command
You register slash commands with Discord once, not on every start. A small script for that:
// register.js, run once by hand: node register.js
import { REST, Routes, SlashCommandBuilder } from 'discord.js';
const commands = [new SlashCommandBuilder().setName('ping').setDescription('Replies with Pong')];
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
await rest.put(Routes.applicationCommands(process.env.DISCORD_CLIENT_ID), {
body: commands.map((c) => c.toJSON()),
});
console.log('Commands registered');
The Dockerfile
FROM node:24-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
USER node
CMD ["node", "index.js"]
And a .dockerignore, so local files do not end up in the image:
node_modules
.env
.git
The base image sets the Node.js version. Use the version your discord.js version requires, and raise it on purpose, not by accident.
Testing on your PC
DISCORD_TOKEN=your-token node index.js
Or with Docker, exactly as it will run on the server later:
docker build -t my-bot .
docker run --rm -e DISCORD_TOKEN=your-token my-bot
Bringing it online on EStage
- Connect the repo or push to our Git address.
- Create a secret:
DISCORD_TOKENwith your token. - Assign memory: 256 MB is plenty for a bot like this.
- Push. We build with your Dockerfile and start the bot.
Online as ...appears in the logs.
Every further push builds again and swaps the running bot. If a build fails, the old version keeps running.
Data that stays
To store points, warnings or settings, use your account’s PostgreSQL database (the credentials come as DATABASE_URL) or a file under /data. An example with a JSON file:
import { readFile, writeFile } from 'node:fs/promises';
const FILE = '/data/points.json';
const points = JSON.parse(await readFile(FILE, 'utf8').catch(() => '{}'));
export async function givePoints(userId, n) {
points[userId] = (points[userId] ?? 0) + n;
await writeFile(FILE, JSON.stringify(points));
}
For more than a few hundred entries, or several services writing at the same time, the database is the better choice.
Common questions
Which Node.js version do I need for discord.js?
The documentation of each discord.js version says so. For discord.js 14.28 it is Node.js 24.17 or newer. In the Dockerfile you set the version with the base image, for example node:24-slim.
Do I have to register the slash commands on every start?
No. Register them once, and then only when a command changes. The easiest way is a small script of its own that you run by hand.
Where does my bot store data that should stay?
On EStage under /data or in your account's PostgreSQL database. Everything else in the container is back to its initial state after a restart.