Discord. Py Restart Command

So, i was making a bot and i was wondering if there is a way to restart it using a command like: p!restart i did like a command: p!shutdown but cant figure out how to restart, for those who came here looking for a shutdown cmd:

async def shutdown(ctx):
    id = str(ctx.author.id)
    if id == 'your_id_here':
        await ctx.send('Shutting down the bot!')
        await ctx.bot.logout()
    else:
        await ctx.send("You dont have sufficient permmisions to perform this action!")

2 Answers

ctx.bot.logout() only logs your bot out.

If you want to fully restart your program, this is how I accomplished it:

import sys
import os
from discord.ext import commands

bot = commands.Bot

def restart_bot(): 
  os.execv(sys.executable, ['python'] + sys.argv)

@bot.command(name= 'restart')
async def restart(ctx):
  await ctx.send("Restarting bot...")
  restart_bot()

bot.run(os.getenv('TOKEN'))

In order to prevent potential abuse of this command, just add an "if" statement to your command that checks ctx.author.id to see if the user has permission to execute the command. It looks like you did that though.

Client.logout()

This will simply log you out and then you can log in again by using Client.login()

This is simply what it needs to restart the bot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest