Discord Bot 24/7 Without UpTimeRobot
That's not Possible ( ﾟдﾟ)つ Bye.
Just Kidding. Well with this Method you can make your Bot go Online
for as long as you want for at least 24 Hours but you will need to rerun it after a day. You don't need UptimeRobot or Anything for this. What you need is just this Great Site and a Python REPL for Deploying an http Server using flask. This method does indeed makes your Discord Bot run for 24 Hours but only for 24 hours only. If your Discord Bot is in Python you can follow this tutorial but if it is in JS then you will need to wait for us to make a tutorial for JS Discord Bots. You can just look at the REPL pinned if you are not a starter in Python Coding but if you don't know about either flask or threading then I advise you to take a look at the tutorial.
If your Python Discord Bot is already built then you can make do some changes in that Repl to make it run for 24 Hours or you can start from Scratch.
First make a Python REPL. Write the command
rm main.py in the shell and wait for
main.py to delete. Then make 2 new files named
bot.py and a
.replit file. You can copy your Bot code to
bot.py and in the
.replit write the following:
run = "python site.py" language = "python3"
We will write our code in
site.py. We will make a simple flask app for hosting the server. You can copy the following code for hosting the server.
import random from flask import Flask app = Flask(__name__) @app.route("/") def home(): return ("Bot is Connected to Discord") def site(): app.run( host="0.0.0.0", # The random port is for the reason that sometimes the default port 8080 is already in use port=random.randint(2000, 9000) ) site()
If you don't understand the flask code don't worry it is only for one time only and won't meddle with your Bot code. And if the code piqued your interest then you can just see any flask tutorial on
Google or Youtube REPL.
All Hail REPL
If you want to customize the Bot connectivity Page you can look at some flask tutorial. As much as I want to explain it this will make the post even longer that will make the readers umm a little bored. So Onwards.
Now that you have a flask app up and running we will proceed to the next step. After running this app the http server will set up and REPL will provide you a link to the server you just set up. You will need that link so please copy and paste it so that you can use it somewhere else.
Step 3 lets go（￣︶￣）↗
We will now import a new module or library or whatever you want to call it. That will be
urllib. You will need this whatever you call it to ping your website so that the REPL keeps running. Yes this is the additional thing that keeps the REPL running for a longer time than by following the stay_alive tutorial people published. You will now define a new improved function of stay_alive using this whatever you call it.
import time import urllib def stay_alive(): while True: start = time.time() while True: end = time.time() # This 15 is for the amount of minutes you change it and take it upto 30 minutes at most if ((end - start) >= (15 * 60)): urllib.requests.urlopen("Your flask server link that we told you to copy") break
Now if you know a bit of coding then you can tell what this function will do. If not then listen you
bakas idiots. We start a while True loop so that it does not stop and check the time repeatedly and if it crosses 15 Minutes, which you can change as mentioned above, it will ping the flask server and hence keeping the code running. You may be now wondering how will this True loop keep running with your Bot and the flask server then your confusion will end in the next step.
We will now fix your confusion is you have some if you don't then lets leave it that. Now the thread whatever you call it will come and fix your confusion. We will import Thread from threading like this:
from threading import Thread
And use this to run the modified stay_alive and the flask app simultaneously.
from threading import Thread site_thread = Thread(target=site) ping_thread = Thread(stay_alive)
Then we will start the threads for execution:
If you want to learn more about threading you can learn from here.
This will now run those as sub_threads and now comes the Bot in the next step.
This step will be short. You just need to define a function and then put the
bot.run() command inside it. Then import it to the flask file and run it at the bottom of the code like this:
# bot.py ... def run(): bot.run(os.getenv("TOKEN"))
# site.py from bot import run ... run()
site.py file will now look like this after all the alterations:
import time import random import urllib from bot import run from flask import Flask from threading import Thread app = Flask(__name__) @app.route("/") def home(): return ("Bot is Connected to Discord") def site(): app.run( host="0.0.0.0", port=random.randint(2000, 9000) ) def stay_alive(): while True: start = time.time() while True: end = time.time() # This 15 is for the amount of minutes you change it and take it upto 30 minutes at most if ((end - start) >= (15 * 60)): urllib.requests.urlopen("Your flask server link that we told you to copy") break site_thread = Thread(target=site) ping_thread = Thread(target=stay_alive) site_thread.start() ping_thread.start() run()
Finally the last step (/≧▽≦)/
You can now run your
site.py and voila your bot is now up and running. You can now expect your bot to run for at least the next 24 Hours.
The Advice is still there that you should rerun it after 24 Hours so that it doesn't crash automatically.
For People whom the Code didn't work
If you couldn't combine the code then you can fork the REPL below and follow some less difficult steps to achieve the same thing.
And for those who clicked the threading tutorial. LOL you got rickrolled. Try searching a tutorial on REPL.
All HAIL REPLLL!