Home > OS >  How do I make my discord.py bot 24/7 which I have coded from VS code
How do I make my discord.py bot 24/7 which I have coded from VS code

Time:06-04

I have coded a discord bot from VS code. How do I keep it 24/7 active

CodePudding user response:

There's multiple ways to achieve this, but without seeing the actual source code I am going to make some inferences.

Option 1 - Quick Dirty Solution

Place your code in an infinite loop and include a sleep call so you can run your bot in intervals. This depends on your need, it can run every 5 minutes, every dew seconds, etc. You can then execute the python script locally and it will run until you close the application or an error crashes the application.

while True:
    sleep(5)

Option 2 - A Little Better Solution

The same solution stated above, but run the python script within a container. This will make your solution more portable.

Option 3 - Take It To The Cloud

A robust solution would be to deploy your bot into the cloud. One of the easiest clouds to deploy to is heroku, which will make your application more fault tolerant.

CodePudding user response:

The best option is to use replit

It is a free cloud hosting service and there is a roundabout way to kind of make it work 24/7

STEP 1:Make a replit account

STEP 2:Copy paste your entire code in #main.py

STEP 3:make another file named keep_alive.py and put in the following code

from flask import Flask
from threading import Thread

app=Flask('')

@app.route('/')
def home():
   return "Good to be alive"

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

def keep_alive():
   t = Thread(target=run)
   t.start()

STEP 4:Add from keep_alive import keep_alive in your #main.py code

STEP 5:Add keep_alive() right before you use client.run(TOKEN)/bot.run(TOKEN)

STEP 6:Setup an uptimerobot linked to the https link on the top right area of replit

EXAMPLE CODE :https://replit.com/@PushkarKulkarni/ogdogebotadmin2#main.py

IF U HAVE ANY DOUBTS REGARDING IT DM ME OR SEND ME A FRIEND REQUEST ON DISCORD Blackcoffee#9911

  • Related