DEV Community

Cover image for Host a Discord Bot online 24/7 for FREE!
skidee
skidee

Posted on • Edited on

Host a Discord Bot online 24/7 for FREE!

Today I'll explain the 100% working method to host a Discord bot online 24/7 for free.

First we have to upload our bot in repl.it, then we'll add a bg task in the bot and then we'll setup the uptime robot in a special way.

1. uploading your project on repl.it

  • Create a new repl and select the language.

repl.it

  • Upload your project simply by dragging & dropping it from your PC or by importing it from github… here is an example:

example

  • Now we have to add a background task in the bot, have to keep it alive because repl.it auto sleeps after few minutes.

  • Now Im goin to tell you adding background tasks in two languages that are discord.js and discord.py

2. Adding a bg task and keeping the bot alive.

(1) Keeping the bot alive & Adding BG tasks in a discord js (v11) bot :

  • To keep our bot alive we have to add the following code on the head of our js file.

code

  • code :
var http = require('http');  

http.createServer(function (req, res) {   
  res.write("I'm alive");   
  res.end(); 
}).listen(8080);
Enter fullscreen mode Exit fullscreen mode
  • As a bg task we will be adding an auto changing bot status feature in our bot.

bot status

  • here is the sample code :
bot.on('ready', () => {

  console.log('Your Bot is now Online.')
  let activities = [`chill gang`, `with the gang`, `with the gang`   ],i = 0;

  setInterval(() => bot.user.setActivity(`${activities[i++ %  activities.length]}`,  {type:"STREAMING",url:"https://www.youtube.com/watch?v=DWcJFNfaw9c"  }), 5000)

)}
Enter fullscreen mode Exit fullscreen mode

2. Keeping the bot alive & Adding BG tasks in a discord.Py bot :

  • After uploading your project in repl.it don't forget to install the python packages. by goin to packages option on the left side and searching python.

  • To keep our bot alive we have to add the following code on the head of our py file.

bot alive

  • code :
from flask import Flask
from threading import Thread

app = Flask('')

@app.route('/')
def main():
  return "Your Bot Is Ready"

def run():
  app.run(host="0.0.0.0", port=8000)

def keep_alive():
  server = Thread(target=run)
  server.start()
Enter fullscreen mode Exit fullscreen mode
  • Adding a background task :

bg task

  • here is the sample code :
status = cycle(['with Python','JetHub'])

@bot.event
async def on_ready():
  change_status.start()
  print("Your bot is ready")

@tasks.loop(seconds=10)
async def change_status():
  await bot.change_presence(activity=discord.Game(next(status)))
Enter fullscreen mode Exit fullscreen mode

3. Setup the Uptime Robot :

  • First create an account on uptime robot for free.

uptimerobot

  • After creating an account, go to the dashboard and click on Add new monitor :

monitor

  • select monitor type Http(s) :

http

  • then go to to your project on repl.it and copy the url from the top of the console and paste it in url section of the monitor:

link

  • now set the monitoring interval to every 5 mins (so that it will ping the bot every 5 mins) and click on create monitor twice :

finish monitor

  • That's it…Now go to your project on repl.it and hit the Run button :

run button

So this was the tutorial on :

How to keep your discord bot online 24/7 for FREE.

This method worked for me and my bot is also working fine :D

Hope you guys liked it and You're bot is now online….peace.

Top comments (27)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
aryamaan08 profile image
Aryamaan08

Use this code:
from itertools import cycle

Collapse
 
skidee profile image
skidee

idk this method is working for several people
check ur code maybe it has some issues.

Collapse
 
pythoneralex profile image
Alex Jin

Hey I can't find the console URL, can you help me?

Collapse
 
skidee profile image
skidee

it's just above the console

Collapse
 
codpython profile image
Python-Cod • Edited

I can't find it...

Thread Thread
 
skidee profile image
skidee • Edited

just above the console..as shown by this red arrow in this img

Thread Thread
 
thekee04 profile image
thekee04

i dont think u understand

they removed that, instead look here: docs.replit.com/repls/web-hosting

Collapse
 
boyney profile image
Boyne

Is this against the repl.it ToS? Because otherwise... hehe...

Collapse
 
bsantosh909 profile image
Santosh Bhandari

Repl.it has approached a well known community to make guide about hosting discord bot on their platform.
It was seen when discord bots were no longer hostable on glitch.

Thus is completely within their TOS and supported as well.. who knows if it will end like glitch though

Collapse
 
cypherguy profile image
CypherGuy

Not against ToS to my knowledge :)

Collapse
 
skidee profile image
skidee

It's 100% safe. 🤗

Collapse
 
skidee profile image
skidee

It's 100% safe. 🤗

Collapse
 
cypherguy profile image
CypherGuy • Edited

I got this error below, after adding the threading and flask code:

Traceback (most recent call last):
File "main.py", line 1, in
import discord
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/init.py", line 25, in
from .client import Client
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 27, in
import asyncio
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncio/init.py", line 21, in
from .base_events import *
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/asyncio/base_events.py", line 296
future = tasks.async(future, loop=self)
^
SyntaxError: invalid syntax

Does anybody know what I should do?

Collapse
 
skidee profile image
skidee

it's due to an error in ur bot code I guess.

Collapse
 
necrodragon41 profile image
Necrodragon41

Really works! Thanks, thanks, thanks, thanks, thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! YOU'RE AWESOME!!!!!!!!! I NEVER THOUGHT THIS WOULD WORK!!!!!!!!

Collapse
 
skidee profile image
skidee

your comment made my day <3

Collapse
 
iac1di profile image
i-Ac1D-i

ILYSM!! TYSM for making this tutorial, really helped a lot!! TYYY <3333

Collapse
 
skidee profile image
skidee

ur welcome 🤗

Collapse
 
mrdino606 profile image
MrDino • Edited

for the discord.js bot, I'm getting this issue (linked image)
imgur.com/a/0Rw7nRP

Collapse
 
bsantosh909 profile image
Santosh Bhandari

seems like basic error. if you are new to node.js I would suggest learning more first.

Also it would simply be

- var http require('http');
+ var http = require('http');
Enter fullscreen mode Exit fullscreen mode
Collapse
 
skidee profile image
skidee

sorry but I have no idea about that 😅

Collapse
 
timelessnesses profile image
Rukchad Wongprayoon

It somehow don't work in python section is my bot script work fine but there's no url popup in console.
Help :3

Collapse
 
totoeevee111 profile image
totoeevee111 • Edited

um i cant find URL for the console i am using discord py
pls help

Collapse
 
thekee04 profile image
thekee04

repl.run is gone. i dont think this method is possible anymore.

however, repl.co is still available. docs.replit.com/repls/web-hosting
i have no idea if this works. let me know if it does!

Collapse
 
dontenvillemaz profile image
Maz dontenville

I get the error on the second piece of code :
)}
^

SyntaxError: Unexpected token ')'