Search code examples
discord.pyrestart

How to make a restart command for my discord.py bot


so far what I've got is:

def restart_program():
    python = sys.executable
    os.execl(python, python, * sys.argv)

@bot.command()
async def restart(ctx):
    message = await ctx.send("Restarting... Allow up to 5 seconds")
    restart_program()

This works, however I'm wondering how I can make it edit the "Restarting... Allow up to 5 seconds" message to say something like "Bot is back up" after it's restarted. Is this possible, and if so, how could I do this?


Solution

  • I would use Client.logout() and Client.login() to restart a bot instead of doing it with os.execl, but if you really need to do it this way, here is a how you can send a message every time it is started:

    @client.event
    async def on_ready():
        # First get the channel where the message should be sent
        channel = discord.utils.get(client.get_all_channels(), name='general')
        await channel.send("Bot is back up!")