Your Discord bot works on your computer, but stops as soon as you switch it off. To keep it online all the time, the simplest solution is a small VPS and PM2, a process manager for Node.js.
What you need
- a Linux VPS (Ubuntu or Debian) you can reach over SSH;
- your bot's code, ideally in a private Git repository;
- the bot's token, which must never appear in the code.
1. Install Node.js
Connect to the server, then install a recent version of Node.js. The versions shipped by default with some distributions are old: use the nvm version manager or the official NodeSource repositories instead. Then check the installation:
node --version
npm --version
2. Get the code and its dependencies
git clone https://github.com/you/my-bot.git
cd my-bot
npm ci
npm ci installs exactly the versions in the package-lock.json file: the bot behaves just as it does on your machine.
3. Protect the token
Create a .env file at the root of the project with the token and other secrets, and add it to .gitignore. The code reads these values at startup (for example with the dotenv package). A token published on GitHub is detected and revoked by Discord within minutes.
4. Run the bot with PM2
npm install -g pm2
pm2 start index.js --name my-bot
PM2 automatically restarts the bot if it crashes. To have it start again after the server reboots too:
pm2 save
pm2 startup
The pm2 startup command prints a line to copy and paste: run it, then run pm2 save again.
5. Monitor and troubleshoot
pm2 status
pm2 logs my-bot
pm2 monit
pm2 logs shows errors live: it's the first thing to check when the bot stops responding.
6. Update the bot
git pull
npm ci
pm2 restart my-bot
The downtime lasts only a few seconds. To go further, automate these three lines in a deployment script.
Would you rather we handled everything, from development to production? See the Discord bot service.