Hosting discord.js bots on repl.it!! [ Works for both discord.js and Eris ]
TheDrone7 (548)

Hosting discord.js bots on repl.it!

This tutorial shows how you can host your discord bots on repl.it if they are built in node.js irrespective of whatever library you used. This tutorial is applicable for all discord.js and Eris bots.

Just to ease things we'll be using the end product of this tutorial.


What we'll be doing?

  1. Creating a web server using the express module.

  2. Using uptimerobot to ping our bot every 30 minutes.

    We do this because our repl goes offline after 1 hour of inactivity. So we keep pinging the repl every 30 minutes, to avoid it being idle for one hour.


Step 1: Setting up the server

Once you have your bot ready for hosting, use the packages tab of repl.it to install the express package.

Next, create a new file, let's name it server.js because it is a server and why not.
Open up this file and import the express package

const express = require('express');

After that, create a new server! Which is as easy as

const server = express();

And we now have a new server as well! Next step would be to create a route, as without a route your server might as well not exist. So here we go: -

server.all('/', (req, res)=>{
    res.send('Your bot is alive!')
})

This will create a new route at '/' which is basically the home page. .all() method creates a new route for handling all types of HTTP requests like GET/POST/PUT etc. The two parameters it takes are 1) the route and 2) The callback, which in turn takes 2 - 3 parameters ( We use the 2 parameter alternative ) i.e. the HTTP request and the HTTP response.

res.send('Your bot is alive') tells that the server has to return a string saying your bot is alive.

Now that we have our server ready, we'll create a function that starts the server,

function keepAlive(){
    server.listen(3000, ()=>{console.log("Server is Ready!")});
}

The server.listen() is a function that starts the server and takes in multiple arguments.

  1. PORT : the port which listens for the request. (Default for node.js is 3000 so we stick with it)
  2. CALLBACK : the function which takes place when the server is ready for handling requests.

And that's it! Now we will export the keepAlive() function since that's all we really need.

module.exports = keepAlive;

And we're done!

Run your bot, you should see something similar to this: -

  1. If you see this message then you've successfully created a server and the bot as well, if you don't see this, you must have messed something up, go back and make sure everything is right :P.
  2. This is the link which we will ping every 30 minutes to keep the repl alive.

Step 2: Combining the bot and server

Now that our server and bot are both ready, we need to combine the both to bring out the desired outcome. For that, open up your index.js file or whichever file has discordClient.login(TOKEN) method, and import the server.js file. ( I have kept the both in the same directory to keep things simple)

const keepAlive = require('./server');

And just before logging in to your bot use the keepAlive() function.

keepAlive();
// Login the bot
client.login("TOKEN GOES HERE")

and we're done combining the two as well!


Step 3: Setting up UPTIMEROBOT

  1. Visit this link and register a new account ( or login to your old one if you already have one ) just like any other website. ( It is free and safe of course )

  2. Click on the New Monitor button.

  3. It will open up a form, fill it as specified below: -
    Monitor Type : Set it to HTTP(s).
    Friendly Name : Set it to whatever you like. Although, I recommend you name it after your bot.
    URL or IP : Copy your server link and paste it here.
    Monitoring Interval : Anything between 5 minutes to 45 minutes is fine, 30 minutes is recommended
    "Alert Contacts To Notify" : Make sure to select yourself here. It will enable you to get notified whenever your bot goes offline via email.

    and finally click Create Monitor .


Now you're bot will stay online for as long as it can.

( Repl it being just another host, won't be able to host it 24/7 so it will have a downtime of a few minutes within every 24 hours.)

And you're done!

Please upvote if you found this useful!

Also, comment down below if there's something you would like to share or ask.

And finally, thanks for reading it!


You are viewing a single comment. View All
jamppunet (0)

@TheDrone7 thanks mate! It seems like I can't run the code while the server is up, since the port is already in use :D

Error: listen EADDRINUSE: address already in use :::3000

But I'll just pause the robot and wait for a while - it's totally ok. This method Rocks - creating a bot in Repl.it makes finally some sense!