Back to Blog

How to Host a Discord Bot 24/7

How to Host a Discord Bot 24/7
Command of the Day
npm start

What it does: Executes the predefined start script configured inside a Node.js project's package.json manifest.

Hosting a Discord bot 24/7 ensures your community moderation commands, automated roles, ticket queues, and music systems are always operational.

In this tutorial, we walk through setting up a Node.js Discord bot from scratch on a RivinCloud Linux VPS, keeping it online continuously using PM2.


1. Prerequisites

Before starting:

  1. A RivinCloud Linux VPS (Ubuntu 22.04 or 24.04).
  2. A Discord Bot application created in the Discord Developer Portal.
  3. Your bot token and application Client ID.

2. Step 1: Install Node.js and NPM

Connect to your VPS via SSH and install the modern Node.js runtime:

# Add NodeSource Node.js 20.x repository
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs git build-essential

# Verify installation
node -v
npm -v

3. Step 2: Set Up Project Directory & Install Dependencies

Create a folder for your bot code:

mkdir -p ~/my-discord-bot
cd ~/my-discord-bot

# Initialize package.json
npm init -y

# Install discord.js and dotenv
npm install discord.js dotenv

4. Step 3: Configure Environment Variables

Create a .env file to store your private bot token securely:

nano .env

Add your credentials:

DISCORD_TOKEN=MTI0NTY3ODkwMTIzNDU2Nzg5MA.X_example_token_hash
CLIENT_ID=123456789012345678

Save and exit (Ctrl + O, Enter, Ctrl + X).

[!WARNING] Never commit your .env file to Git! Add .env to your .gitignore file to prevent accidental token exposure.


5. Step 4: Add Sample Bot Code (index.js)

Create your main script:

nano index.js

Paste the following discord.js v14 code:

require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');

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

client.once('ready', () => {
    console.log(`✅ Logged in successfully as ${client.user.tag}!`);
});

client.on('messageCreate', (message) => {
    if (message.author.bot) return;

    if (message.content === '!ping') {
        message.reply('🏓 Pong! Bot hosted 24/7 on RivinCloud.');
    }
});

client.login(process.env.DISCORD_TOKEN);

Test run your bot in terminal:

node index.js

(You should see Logged in successfully in terminal and your bot turn online in Discord. Press Ctrl + C to stop it for now).


6. Step 5: Keep Bot Online 24/7 with PM2

To ensure your bot stays online after you close SSH and automatically restarts if it crashes:

# Install PM2 globally
sudo npm install -g pm2

# Start bot with PM2
pm2 start index.js --name "my-discord-bot"

# Save PM2 process list
pm2 save

# Set PM2 to auto-start on server boot
pm2 startup

(Copy and paste the command generated by pm2 startup into your terminal).


Command of the Day: npm start

The npm start command executes the script designated under "scripts": { "start": "..." } in package.json.

Syntax

npm start

Example package.json

{
  "name": "my-discord-bot",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  }
}

Common Mistakes

  • Forgetting Gateway Intents: If your bot does not respond to messages, ensure Message Content Intent is enabled under the "Bot" tab in Discord Developer Portal.
  • Hardcoding tokens: Pushing code with raw API tokens will cause Discord to automatically revoke your token within seconds.

Troubleshooting

  • Check Live PM2 Logs:
    pm2 logs my-discord-bot
    

Best Practices

  • Use PM2 to manage logs and auto-restart policies.
  • Keep discord.js updated to the latest stable release.

Frequently Asked Questions

What happens if my VPS reboots?

Because we configured pm2 startup and pm2 save, PM2 will launch automatically upon VPS boot and restore your running Discord bot instantly!


Conclusion

Hosting your Discord bot on a RivinCloud VPS with PM2 guarantees 24/7 uptime, low API latency, and automated crash recovery. Launch your bot today!