Hosting discord.py bots with repl.it
TheDrone7 (519)

Hosting discord.py bots on repl.it

This tutorial shows you how you can host your discord.py bots on repl.it.

Before you start, you should already have a working bot. Also, this really doesn't matter if you're using discord.py async or discord.py rewrite. Works like a charm for both!

Just to ease things, we'll be using the end product of this tutorial.

What we'll be doing?

  1. Creating a web server.

  2. Using uptimerobot to ping our bot every 30 minutes.


Step 1: The requirements.txt file.

Once you have your bot ready for hosting create a new file named requirements.txt in the root directory of your python repl.

Open it up, the requirements.txt file holds all your project dependencies. Make sure to add the appropriate package names for the packages you're already using. A sample file for basic bots are given below: -


requirements.txt for a discord.py rewrite bot.

Now that you have the basic requirements ready, add one more requirement i.e. flask. Making your requirements.txt something like: -


Note : I am sticking to discord.py rewrite bot, but it really doesn't matter again.

flask is a python module for handling http requests.

Step 2: Setting up a server

Setting up a server using Flask is very easy. But to keep things simple and your bot's file clean, we'll make a server in a new file and name it keep_alive.py since that is what most people like to call the file that helps keep their bot alive.

Open up the keep_alive.py file and import Flask also import Thread you can learn more about Threads here.

from flask import Flask
from threading import Thread

We didn't have to add threading to our requirements since it comes with python.

Next, create the actual server.

app = Flask('')

Easy, isn't it? But it's not over just yet.

Next, we add a route. A route is basically what handles http requests. Without a route the existence of our server does nothing. So add the following code

def main():
    return "Your bot is alive!"

@app.route() is a decorator which defines a route. '/' is the default route or the homepage of our server. The main function is called whenever a user makes a request to the '/' route which basically returns whatever has to displayed on the webpage, here we simply send the string Your bot is alive to keep things simple.

Now that our server is ready, we need to make sure that it runs and http requests can be made to it.

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

You can basically put any 4 digit number in port which is not already in use. The defaults for python is 8080 and that's what we'll stick to.

Finally, we make a function that we can call to prepare our server and allow it to be pinged.

def keep_alive():
    server = Thread(target=run)

And we're basically done with our keep_alive.py file.

Step 3: Combining the server and bot.

Now that we're done making both the bot and the server, we need to combine the two. That is easy too, open up the main.py file ( or whatever file has the discord_client.run(bot_token) function in it ) and add the following line at the top: -

import keep_alive

This will import the keep_alive file in your bot's main file.

Now just before starting the bot, at the bottom of the same file write the following code: -


This will run the keep_alive() function that we defined in the keep_alive.py file earlier.

And now your server is ready too! Run the bot, you should see something like this: -

Notice that small window saying Your bot is alive! if you see it, you successfully created the server! Only the last step remains! Before you go into the last step, make sure to copy the repl.co link you see in that small window's address bar. (https://discordpy-rewrite.thedrone7.repl.co in the picture, it will be referred to as the server link in the rest of the tutorial)

And in case you don't see it, go through this tutorial again and make sure you didn't miss something, also if it retains, don't hesitate to comment and tell me.

Step 4: Setting up uptimerobot

First of all visit https://uptimerobot.com/ and register an account just like any other website. (It is free of course)

Next, Once, you're logged in,

  1. Click on the New Monitor button.

  2. Next, choose the HTTP(S) Monitor Type.
    Give it a friendly name of your choice ( naming it after your bot is recommended )
    Paste your server link in the URL/IP field.
    Set the Monitoring Interval to anything between 5 minutes to 45 minutes. ( 30 minutes is recommended)

    Optionally, Select yourself as one of the "Alert Contacts To Notify" so you are notified via e-mail whenever your bot goes offline.
    Finally making it look something like this: -

  3. Click on Create Monitor and you're good to go!



And you're done! The final product can be found here.

Please upvote if you found this useful!


And finally, thanks for reading it!

You are viewing a single comment. View All