Skip to Content

Getting Started

Discord bot user requires websocket connection, therefore modern serverless approach won’t apply here. You will need a virtual machine to self host your bot.

Requirements

Software

  • Node.js >= 16
  • npm >= 9
  • pm2 (optional)

Hardware

  • CPU >= 1GHz
  • RAM >= 512MB

Installation

Clone the repository

git clone https://github.com/xuc323/discord-bot

Install dependencies

cd discord-bot npm install

Configurations

Create a .env file in the root directory of the project and fill in the following fields:

# .env DISCORD_TOKEN=YOUR_TOKEN DATABASE_URL=POSTGRES_DATABASE_URL PREFIX=!

Format for DATABASE_URL. Here we are using postgresql as our database:

postgres://{user}:{password}@{host}:{port}/{database}

Run the bot

npm run build npm run start

Cleanup

npm run clean

pm2 (optional)

Bot will be terminated when you close the terminal. To keep the bot running in the background, you can use a node runtime such as pm2 to manage the process.

pm2 start npm --name "discord-bot" -- run start

For easier startup, you can also create a ecosystem.config.js file in the root directory of the project and fill in the following fields:

module.exports = { apps: [ { name: "discord-bot", script: "npm", args: "run start", time: true, }, ], };

Then you can start the bot with:

pm2 start ecosystem.config.js

Or if you have a few apps within the same file:

pm2 start ecosystem.config.js --only discord-bot