Hosting discord.js bots on repl.it!! [ Works for both discord.js and Eris ]
Hosting discord.js bots on repl.it!
Just to ease things we'll be using the end product of this tutorial.
What we'll be doing?
Creating a web server using the
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
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
After that, create a new server! Which is as easy as
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: -
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
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,
.listen() is a function that starts the server and takes in multiple arguments.
- PORT : the port which listens for the request. (Default for node.js is
3000so we stick with it)
- 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.
And we're done!
- 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.
- 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)
And just before logging in to your bot use the
and we're done combining the two as well!
Step 3: Setting up UPTIMEROBOT
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 )
Click on the New Monitor button.
It will open up a form, fill it as specified below: -
Monitor Type : Set it to
Friendly Name : Set it to whatever you like. Although, I recommend you name it after your bot.
URL or IP : Copy your
server linkand 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
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!
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.
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?
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!
works for v12? and a problem i noticed is rolling out updates with live coding whats the most efficient way to do so
@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.
@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.
@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.
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