Daniel Tam
Daniel Tam

Reputation: 926

How do i make a discord.py bot run 24/7 but still be able to edit it?

I've been having trouble finding a way to deploy a bot and make it run 24/7 while still being able to edit the bot and save the changes. I have seen 0 videos that have taught me how to do that. Can someone here show me how?

Upvotes: 1

Views: 10006

Answers (2)

Redfer
Redfer

Reputation: 162

For hosting the bot you could use heroku and every time you change something you could push it like using a git repository. Reference for hosting on heroku: https://www.youtube.com/watch?v=BPvg9bndP1U

and as for the editing 24 hours you should take a look at using cogs.

import os

import discord
from discord.ext import commands
from discord.ext.commands import Context
from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())
BOT_TOKEN = os.getenv("BOT_TOKEN")

client = commands.Bot(command_prefix='.')


@client.command()
async def load(ctx: Context, extension):
    client.load_extension(f'cogs.{extension}')
    await ctx.send("Loaded Cog")


@client.command()
async def unload(ctx: Context, extension):
    client.unload_extension(f'cogs.{extension}')
    await ctx.send("Unloaded Cog")


@client.command()
async def reload(ctx: Context, extension):
    client.unload_extension(f'cogs.{extension}')
    client.load_extension(f'cogs.{extension}')
    await ctx.send("Reloaded Cog")

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[0:-3]}')

client.run(BOT_TOKEN)

This should ideally be your bot.py and make a folder called cogs. The load and unload command lets you remove the and add any files so suppose there is a major error you can unload it and then fix it and then load it so people can use it again. Remember you are only loading and unloading cogs. The reload is useful when you edit the cog and don't want to change it. So you can save and then reload the extension. So make a file called cogs and in the folder cogs as an example script:

import discord
from discord.ext import commands


class Test(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    async def ping(self, ctx):
        ctx.send("pong")


def setup(client):
    client.add_cog(Test(client))

Now if you initially run it all the cogs will be automatically loaded because there is a for loop in the first script that loads all the cogs. It called the setup which is what loads the cog.Now say you change the message all you have to do is change it and save. Next go to the server and type the command say the prefix is '.' .reload Test. If your using heroku make sure to push the changes. How i learnt cogs: https://www.youtube.com/watchv=vQw8cFfZPx0&list=PLW3GfRiBCHOhfVoiDZpSz8SM_HybXRPzZ&index=7

Upvotes: 4

ravexu
ravexu

Reputation: 665

You can use cogs, which will allow you to load, unload, and reload extensions. So then all you need to do is make changes in this cog, and then reload it, for the changes to take place.

https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?#cogs

Reload Extension

Load Extension

Unload Extension

Upvotes: 2

Related Questions