As Blog posts age the content becomes a bit stale. The information here might not be accurate anymore.
If you're an avid discord user you've definitely seen a bot (or 10) in the server(s) you're a part of. If you're a programmer of some sorts (hobby, college student, or tired professional) you've probably thought "I could make this better! 🤓".
This article is great if:
- You have no access to a credit card and want to host your projects somewhere your friends (and potential employers) can access them 24/7.
- You have the coolest discord bot idea and you're able to code rock paper scissors in some programming language. I used JavaScript for this example but this concept can be applied to Ruby, Python, Java, etc.
- You know how package managers work for whatever programming language you'll try this with.
- You're a cheapo like me and want to run a discord bot or a hundred discord bots for FREE!
Turn back now if you need a bot with voice capabilities. We'll be using Repl.it to host our bot and it's a pain to install ffmpeg on it.
Step 1: Create a Bot User
a) Sign into your discord account on https://discordapp.com and create a new application here. Then add a bot user to that application:
b) Next save your bot's token for later use:
c) The last thing we need from this portal is your bot's invite URL. This determines what your bot has permission to do. At the very least it needs permission to send messages. Make sure to invite it to your server using that URL!
Step 2: Head to Repl.it
If you haven't heard of it Repl.it is an online IDE of sorts that lets you create and share small projects. It's pretty amazing and has constantly been adding features to help you do more online. We call these repls.
a) Create a new JavaScript repl.
b) First thing we need to make sure a webserver is running in our repl. Repl.it will kill a running repl when you close the browser tab unless it's serving web content. Then Repl will keep it alive for an hour even if you close the tab. Paste the following code in your repl and Repl.it will automatically install packages for you and start an express webserver.
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
If you look to the left, a file called package.json
should have appeared.
This file holds any packages you require in your repl. Plus any other scripts we might make. Just like a regular NodeJs project.
I chose express as my webserver. If you're working with Python you'd probably use Flask, Ruby would have Sinatra. For java however I would recommend investigating com.sun.net.httpserver.HttpServer
for a speedy start up time.
c) Next we need to instantiate our bot.
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
// ================= START BOT CODE ===================
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong!');
}
});
// You really don't want your token here since your repl's code
// is publically available. We'll take advantage of a Repl.it
// feature to hide the token we got earlier.
client.login(process.env.DISCORD_TOKEN);
ping pong
is sort of like the hello world for bots. Once we start this up you should see your bot online in your server. If you send the word ping in your server the bot should reply with pong.
After you've pasted that code snippet in, don't click restart yet!
d) Create a .env
file
On the left create a file called .env
, the content of the file should be:
DISCORD_TOKEN=your_token
This will help us hide your token from the rest of the world. Read more about it here in Repl.it's Docs.
Now that the proper credentials are in, you can click restart now. Your bot should be online!
Congrats! You can peruse the discord.js
documentation to implement all the cool bot features you can dream of!
Step 3: Keepin' our bot alive
As I mentioned before Repl.it puts repls to sleep. Here's a note from their docs about that:
Once deployed, the server will continue to run in the background, even after you close the browser tab. The server will stay awake and active until an hour after its last request, after which it will enter a sleeping stage. Sleeping repls will be woken up as soon as it receives another request; there is no need to re-run the repl. However, if you make changes to your server, you will need to restart the repl in order to see those changes reflected in the live version.
We're going to use Uptime Robot to keep our bot online.
a) Copy the url for your repl's webserver.
b) Create an account on Uptime Robot and create a monitor to ping your server every 5 minutes
That's it! We're done. Have fun with your 24/7 hosted bot. Sometimes the bot might be offline. But for 99% of the day it'll be online thanks to Uptime Robot.
Happy Coding Folks!
Oldest comments (46)
Does this depend on your computer to be online at all times?
It does not! Your bot is hosted on replit's servers so you can close the chrome tab even you're done setting it up
What will the repl instantiating code be for a java project?
I have a repo with a working java version. github.com/fizal619/MerlinBot
Feel free to fork it!
Thanks for this! I've used Repl.it before but never for hosting purposes
Yes of course.
can you do it on python?
Is there any limits on users? What if you host a music discord bot in your repl it will buffer?
I host 3 separate scripts (reps) for the same bot on RepL..they are a chat bot, a rank bot and a music bot...none of them lag...
Would this work if you close the Uptime Robots tab?
Hey,
I made mine using Python and unfortunately it didn't work. I'm guessing it's because I can't see the token because it is in .env and Uptime Monitor is not the owner. I even tried it without the .env, it still didn't work can you help me?
I used Node.js for this and it works. The guy in the tutorial did the same thing. So I recommend you try to use Node.js and see how it goes
I used this method mine is working but where can I add more features to it?
Well, there are two things you can do:
there can be some bugs with it, also you need to create a .env file to protect your bot token from hacking....
Unfortunately, your using Python, that is the problem. This is meant for discord.js. I would recommend going to here: dev.to/harshitojha/host-a-discord-...
It is a python version of this
Python isn't necessarily the language for back-end web apps. Either use Go or Node.js. Go is phenomenal for the job because it's incredibly fast, yet its size is bloated so not good for storage-sensitive applications. Luckily, servers aren't storage-sensitive which is where it's supposed to run :) On the other hand, Node.js is delightfully tiny, only as big as the text file you made to run it. The down side is that it's much slower than languages like Go.
Just followed you tutorial, it works nicely !
Hi, I have a repl team and it not working when I put the URL in to uptime robot. (not keeping online)
To do it with python I just made it http then added the end f it and it worked
Guys this only works on Node.js(as far as i know) and if uptimerobot.com is not working then check the link or pause and start again.
/home/runner/trying-trying-tryimg/index.js:8
const express = require('express');
^
SyntaxError: Identifier 'express' has already been declared
Hint: hit control+c anytime to enter REPL.

help pls
You have already set up express as a variable, and now can't change it because it's ALREADY a constant.
P.S: This is a sign you cant' understand the code you're writing. If you don't understand the code you are writing, then you NEED to go to the basics and learn JavaScript itself before delving into complicated topics like Node.js and APIs.
i'm good with that tho
dev-to-uploads.s3.amazonaws.com/up...
Thanks! Now I can keep my bot on 24/7.
How do I make it so then I can add more commands instead of just the ping command?
client.on('message', msg => {
if (msg.content === 'PERSONS MESSAGE') {
msg.reply('BOTS REPLY');
} else if (msg.content === 'PERSONS MESSAGE') {
msg.reply('BOTS REPLY')
}
});
// This is the full message command
It's good, i'm pretty sure you're using "Atom" as a coding platform. Visual Code & Atom is same. Almost every coding software is the same and can be used on as long as you have discord.js downloaded by downloading node.js
Some comments may only be visible to logged-in visitors. Sign in to view all comments.