Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update bot without closing it discord python

I am creating a Discord Bot in Python which posts a random daily fact (or just any fact, really) when the user says "!fact".

my question is that, say I want to add another command like "!foodfact", in which case the bot would give you a strictly food related fact. or maybe something like "!help" so that the bot gives you the list of commands available.

assuming that I have set up the bot with a script, and the bot is active in a terminal, and I have only defined one command(long, I know, but stay with me)

how do I add new commands in the code without having to

a)exit the terminal instance, and

b)edit code and start it again ?

any solution is welcomed.

P.S: this is a personal bot to post facts in a channel in a server

I would be glad to post the source code, but I don't wanna go open source with this just yet...

like image 793
Esvin Joshua Avatar asked Mar 12 '26 18:03

Esvin Joshua


1 Answers

You could use Cogs. They help you to load/unload/reload the bot.

Put only essential commands that you don't update often in the main bot.py file and include a reload {specified file} command, this allows you to reload your bot's modules without actually restarting the bot itself.

@bot.command()
@commands.is_owner()
async def reload(ctx, extension):
    bot.reload_extension(f"cogs.{extension}")
    embed = discord.Embed(title='Reload', description=f'{extension} successfully reloaded', color=0xff00c8)
    await ctx.send(embed=embed)

or

@commands.command(name='reload', hidden=True)
@checks.is_owner()
async def _reload(self, *, module : str):
    """Reloads a module."""
    try:
        self.bot.unload_extension(module)
        self.bot.load_extension(module)
    except Exception as e:
        await self.bot.say('\N{PISTOL}')
        await self.bot.say('{}: {}'.format(type(e).__name__, e))
    else:
        await self.bot.say('\N{OK HAND SIGN}')

(From cogs/admin.py)

like image 78
krmogi Avatar answered Mar 15 '26 07:03

krmogi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!