How to run a Discord.js bot on a VPS

Articles on: Dedicated Servers / VPS


How to run a Discord.js bot on a VPS (Linux)


  • Connect to your VPS via SSH

  • From this list, select the Node.js version that you would like to run the first 2 commands that are in their respective versions.

For Example, if you want to install Node.js v16. You would run the first two commands in the Terminal.

Note: Depending on what OS you are running, the commands may be different! In this case, we will be using Ubuntu!

  • Run the commands apt install npm -y and npm i pm2 -g. To install npm for installing node packages and pm2 to run the bots 24/7.

  • After installing the packages, run the following commands:

  • pm2 flush

  • pm2 install pm2-logrotate

  • pm2 set pm2-logrotate:max_size 10M

  • pm2 set pm2-logrotate:compress true

  • pm2 set pm2-logrotate:rotateInterval '0 */1 * * *'.

Running these commands will make it so that the pm2 logs stop creating very large log files in the long term.


Running the Discord.JS bot(s) using PM2


  • Connect to your VPS via SFTP. In this case, we will be using FileZilla

  • Then using SSH, use the “ls” command to see the content of the folders that you are currently on, and use the cd ` command to navigate through the folders. Also, to go up a folder, run the command ` cd ../.

For Example: If you want to get from the “root” folder to “Discord Bots > DjsBot”, just run the command


cd ‘Discord Bots’/DjsBot


.

  • Now to start the bot, run the command pm2 start ` or ` pm2 start --name= to give the process a name.


How to Restart the Bot using PM2


  • To restart the bot you, need to get the bot’s process ID or the process name that you’ve set when starting the bot.

Note: You can get the process ID by doing


pm2 ls


and find the id corresponding to the process name

  • Run the command ` pm2 restart ` and your bot will restart.


How to Stop the Bot using PM2


  • To stop the bot, you need to get the bot’s process ID or the process name that you’ve set when starting the bot.

Note: You can get the process ID by doing


pm2 ls


and find the id corresponding to the process name

  • Run the command ` pm2 stop ` and your bot will stop.


How to see the logs of your bot using PM2


  • To view the logs, you need to get the bot’s process ID or the process name that you’ve set when starting the bot.

Note: You can get the process ID by doing


pm2 ls


and find the id corresponding to the process name

  • Run the command pm2 logs ` or ` pm2 logs --lines= and pm2 will show the bot’s console logs.

If you require any further assistance, please create a ticket here.

Created By: Alecz R.


Updated on: 29/02/2024

Actualizado el: 31/07/2024

¿Este artículo te resultó útil?

Comparte tu opinión

Cancelar

¡Gracias!