Learn to Code via Tutorials on Repl.it!

← Back to all posts
Hosting discord.js bots on repl.it!! [ Works for both discord.js and Eris ]
TheDrone7 (1824)

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.

// 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!

kemosaf5 (14)

Yes, thats a very nice way to keep your bot 24/7, but your repl is public, not only people cant steal your code, they can also access your token, so make a file named .env, and inside of it just put TOKEN=your-token-here, then go back to your main file, and do process.env.TOKEN instead of putting your token between " ", repl doesnt make .env files public.

PineapplePraise (1)

@kemosaf5 Problem... The env file is hidden from public. Including the link that uptime robot is pinging, which gives the error of invalid token when using the repl run link.

kemosaf5 (14)

@PineapplePraise No problem at all! uptime robot doesnt need to see your token, its just going to your projects website to keep it awake, as for running it, it shouldnt give an error if you have access to the env file

PineapplePraise (1)

@kemosaf5 yeah i just realized I didn't do something right

Jannis2021 (0)

@kemosaf5 idk if you still looking at this but I get an ERROR every time I try to run the bot. Do I have to put the token in "" or ''`or jus tnothing?

ZafirHasanHasan (0)

@Jannis2021 you have to put a token to login in bot, like

client.login("mysupersecrettoken") or your bot appear online

MrTechy11 (0)

@Jannis2021 in your .env file, write this code:


do NOT put quotations around it, just paste the token.

ike_fite (12)

My bot was doing fine and is now not working at all.

P025 (1)

@ike_fite did you find some other way to do it or no?

novemberlc (1)

The webserver doesn't start.

gameietweg (1)

where have packages tab of repl?
i can't find it

code913 (16)

You explained the server link a little badly so some people are confused with what the heck it actually is. The simple explanation is just that <repl name>.<user name>.repl.co is the server link for the repl.


Making embeds in Discord.js now requires node.js version 12 or higher. Repl.it has node version 10.x.x.
Anybody have ways of making embeds without node 12?

MakerofMyths (0)

Doesnt work for me, it keeps saying "RichEmbed is not a constructor"
Any help?

Embedded77 (2)

@MakerofMyths make sure u have discord.js v11

MakerofMyths (0)

@UsernameWitheld What is the thing to check/update it?

Embedded77 (2)

@MakerofMyths npm install or just the packages button

MakerofMyths (0)

Thanks! I checked it and it said it was version 12.2.0, Is there any way for me to make it v11?

simpleIdiot (1)

@MakerofMyths change it to "^11.6.4" or something along those lines.

rblxploit (1)

@MakerofMyths Why would you make it v11? v12 is the newer one.

blaster202 (3)

Actually, repl.it uses node V12 now. @Helixable

TorCenta (0)

does this mean that I have to have repl open 24/7?

Gamerul552 (0)

I don't see the link to my site to put it in Uptime

ramin248 (0)

i mistakely pressed close button of the server url now i cant get the url anymore how can i get it

hightowernolan (0)

it isn't showing the server preview and the bot goes offline soon after I close the repl tab, even with uptime robot on. Please help

KiritoBlack (0)

It works........but when i tried with another bot on uptimerobot it says The monitor URL/IP is invalid, please try with another URL/IP.

Aflix90 (0)

If there is a bug and you're doing everything right but it wont show you have to delete the files "package.json" and "package-lock.json" and then run the code again. Repl wants the express file instead of just "express" it wants it installed with discord.js so "discord.js-express." After that it should appear!

ClaraLila (0)

How can I add embeds? It keeps saying: "SyntaxError: Unexpected token ':'"

pro260 (0)

what is server link is the repl it url

code913 (16)

@pro260 it is <repl name>.<user name>.repl.co

RT1005 (0)

Where should i create the file in step1?

TheDrone7 (1824)

@RT1005 wherever your bot file is.

Gooba0069 (0)

@RT1005 add me at discord i would help you Gooba#0069

Aspekttt (0)

really helpful thank you so much

GeekyAbhi2020 (0)

works for v12? and a problem i noticed is rolling out updates with live coding whats the most efficient way to do so

MeowcaTheoRange (7)

@GeekyAbhi2020 Works for any type of bot project (twitter, reddit, discord, etc, etc, etc) because all UTR is doing is visiting your website to make REPL load it. You can even do this with non-bot projects using Express if it's important.

P025 (1)

i did all of this and it still gets down help

DaLiteralPanda (11)

what is a server link?

lumaduda (0)

never worked for me

TheDrone7 (1824)

@lumaduda it's still working for me till date.
https://repl.it/@starboard/starboard is a bot I've been hosting permanently on repl.it using the same technique as here except for some changes to how I do the same thing.

rblxploit (1)

@lumaduda you probably did something wrong with express

NoelBryan1 (0)

My bot goes does at around 11:45 pm and never comes back up. Could anybody help?

MrMinimax (140)

How do you acually put this into a discord server though

targetstudios (0)

@MrMinimax in the discord developer portal go to the bot's application, then click oauth2, then click bot, then scroll down and click the permissions the bot needs, then click the copy button next to the link and paste it in your browser, then select what server u want to add the bot to and then prove u are not a robot. then the bot is in your server.

tpunn19 (7)

Do you have a code example like your python one?

TheDrone7 (1824)

@tpunn19 The same link as provided at the end of my d.js bot tutorial.

tpunn19 (7)

@TheDrone7 I've gotten it to work for both the Flask and Express servers. But the Python one seems to stay online longer than the Node one. Python - 24 hours, and Node - 7 hours. Do I have to restart the repl manually, or will it come back online itself? Because it doesn't seem to do it itself.

TheDrone7 (1824)

@tpunn19 there can be only 2 reasons if there bot does not come back online on its own.
1. The bot may have bugs.
2. Uptimerobot may be under maintenance.
Since the python one does stay up, my guess is there might be a bug in your bot.

tpunn19 (7)

@TheDrone7 It seems to be staying online now. thanks.

tpunn19 (7)

@TheDrone7 The issue happened again, but I think I know what is wrong. Whenever the server is refreshed, it asks for the token in the console (for security purposes). Is this why?

I think for JS I can fix this by enmapping the token, but not sure how to do this for Python.

TheDrone7 (1824)

@tpunn19 what exactly do you mean by "security purposes"?

tpunn19 (7)

@TheDrone7 I don't want people injecting scripts into my bots... and Repl.it allows it to be public (since I'm not in paid version)

TheDrone7 (1824)

@tpunn19 You can hide some stuff like your bot token using a .env file in which you basically have stuff in the following format: -


Repl.it hides your .env from others and doesn't let others modify your repl without having to fork it which in turn would result in loss of the .env file for the person who has forked the repl, thus making it safe even for free users.

tpunn19 (7)

@TheDrone7 Oh ok. Thanks. Will this prevent the bots from staying down?

TheDrone7 (1824)

That is what this tutorial is for @tpunn19


if you would like to stop the bot instance you may add client.destroy() line to your code under your message section for example stop so that if you message the bot with stop it would logout (you have to wait till it shows). make sure you put condition like when id = your id otherwise everybody will be able to logout the bot

Nurutomo (0)

Id act like a password :)

jamppunet (0)

Great tutorial and it seems to be working too :D But what about changing the bot. Do I have control somewhere in repl.it to stop the server, so I could get my new version live?

TheDrone7 (1824)

@jamppunet there's not a way to stop the server currently, you can always restart the server. A quick way to kill the process would be to deliberately write a line of wrong code and then restart which will end up not running because of the error. You can also stop uptimerobot and wait for 1 hour.

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!

TheDrone7 (1824)

@jamppunet try modifying the server.listen line to this

server.listen(); with nothing between the parantheses.

jamppunet (0)

@TheDrone7 You're the man! This way I can develop the bot, even when it's running. It doubles everything up in Discord thou, but that's expected, since there are several instances running at the moment - one on repl.it browser, another one on the server and there is a mysterious third one - probably the server version on repl.it UI.

I'll just close the browser and leave some time for it and it will reset the extras. But thanks for the tips!

I think rest of you using this do the following when developing your bot:
1) pause uptimerobot monitor
2) comment out the server.listen line in the server.js by adding // in front of the line, like this
//server.listen(3000, ()=>{console.log("Server is Ready!")});

and you can still run your code. In Discord you will be getting several replies until the old server version that uptime robot was keeping up dies, but other than that, this is perfect!

When you are ready to go online and keep it up, just remove the // from the server.listen and run it + activate the uptimerobot

TheDrone7 (1824)

@jamppunet although what you're facing is a repl.it bug. @amasad might wanna look into it. So you should only use the same port to avoid creating multiple instances. But till it's fixed you can do what you're currently doing.

jamppunet (0)

@TheDrone7 I haven't changed the ports, it's been 3000 all the time. But there is probably some kind of bug, since it replicates the bot instance...

TheDrone7 (1824)

@jamppunet using server.listen() without specifying the port creates a new server on whichever port is available. So if you use that then it'll create multiple instances. It's an issue with bring unable to properly kill processes.

jamppunet (0)

@TheDrone7 Oh damn ;) Well luckily I just commented the line totally out, so it doesn't listen to anything :D

TheDrone7 (1824)

@jamppunet ok so basically you're not creating a server for keeping your but up and just running the bot temporarily. It would still create multiple instances because when you click restart it is not killing the earlier process and starting a new one. Although repls automatically die after one hour of inactivity. It should kill the process instantly.

jamppunet (0)

Ok @TheDrone7 ,
TBH I'm not running it temporarily, but I was doing a test for the keepAlive feature. I need to add some logic to the bot so I need to edit it and that's why I sent the question in the first place.

Thanks to you, that's sorted out now - so when I have my code ready I can set it on as permanent bot!

I started thinking... If I comment out the keep alive call from the main index, then it doesn't create the server at all - even thou the server is included into the project?

noah427 (3)

where is the tutorial for building the actual bot?

TheDrone7 (1824)

@noah427 the link is there in the introduction the text of this tutorial is the link.

rubiksking124 (0)

@TheDrone7 i dont have any front end?