Skip to main content
The last step. Your bot is built; now it runs on a machine that does not turn off, as the production application, in every server that adds it. This page goes through what changes from dev, the steps on the machine, and what keeps the bot running.

What changes from dev

The production application

Remember the second application from Your Discord application? This is where it comes in. On the Bot page of the production application:
  1. Reset Token and copy it. It goes on the machine, never on your computer.
  2. Enable the same Privileged Gateway Intents as on the dev application: your files are the same, so they need the same ones. chapterjs start lists what is missing, but in production it stops instead of waiting.
To add the production bot to a server, build the invite link yourself, since chapterjs start runs in every server and has no reason to print one: in the Developer Portal, OAuth2 → URL Generator, tick bot and applications.commands, pick the permissions your bot needs, and share the link. Give it only what your files use: people trust a bot that asks for little.

Try it on your computer first

Before any machine, run production once locally, to see it work. In the .env of your project, put the production token in place of the dev one for a minute, build, and start:
The commands are registered for everyone, and private messages reach the bot: the where: 'dm' files you could not try in dev work now. Stop it with Ctrl+C, and put the dev token back in .env.
chapterjs start only needs BOT_TOKEN. When DEV_GUILD_ID is also there, production ignores that server, so that a dev bot and a production bot sharing one token never answer twice. With two applications you do not need that, and the machine only gets the token.

On the machine

A machine that stays on is a server you rent, a container, or a host made for Node applications. The steps are the same everywhere:
1

Put the project there

Clone your repository, or copy the folder without node_modules and .chapterjs (both are made on the machine). The machine needs Node.js 24 or newer and your package manager.
2

Install

The install command of your package manager, in the folder. It also writes the types of the project.
3

Give it the token

Set BOT_TOKEN the way the machine does it: an environment variable, the “secrets” of a host, or a .env file in the folder. The real environment wins over the file. This is the token of the production application.
4

Build, then start

The build script, then the start script. Many hosts ask for a “build command” and a “start command”: give them these two.
Never commit .env, and never put the token in your code: whoever has it controls your bot. If it leaks, reset it on the Bot page and give the new one to the machine.

Keeping it running

chapterjs start runs until it is stopped. Two things end it:
  • A stop asked by the machine (Ctrl+C, or the host stopping the process): the bot disconnects cleanly, prints ✓ Disconnected, and exits with code 0. Give it a few seconds before killing it.
  • Something to fix: a refused token, a privileged intent not enabled, no build. The message says what, and the command exits with code 1.
Use what the machine offers to start the bot again when it exits and when the machine reboots: a process manager, the “restart” setting of a host, a system service. But do not restart blindly in a tight loop: a bot that stops with code 1 has something to fix, and starting it again changes nothing. Discord limits how many times a day a bot may connect, and resets the token of one that goes over; ChapterJS stops before that limit rather than let it happen:
Inside chapterjs start, nothing needs your help: the connection is kept alive and reconnected when it drops, and a large bot’s processes are started again when one stops.

Updating the bot

Put the new files on the machine, build, start again. Until you build, the running bot keeps the previous build, and chapterjs start reminds you:
A host that takes the project from git does the first steps by itself on every push: then updating is pushing.

When the bot grows

Discord splits large bots into several connections, called shards, and ChapterJS opens them by itself. With many of them, it also spreads them over several processes, so the bot uses every CPU of the machine; you see it in the first line:
Nothing to configure, and a bot under a few thousand servers runs in one process. What it means for your code was said in Organising: each process has its own memory, so anything shared lives in a database.

Recap

  • Production is the second application: its token goes on the machine, its intents are enabled by hand, its invite link is made in the Developer Portal.
  • On the machine: install, set BOT_TOKEN, build, start; a host takes the two scripts as its build and start commands.
  • Exit code 0 is a clean stop, 1 is something to fix; let the machine restart the bot, never in a tight loop.
  • Updating is building and starting again.

What's next

A checklist, and where to go from here.

Guide: Deploy

The same steps, with every variable and message.