5

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!")```
MysteriousK
  • 89
  • 1
  • 3
  • 9

4 Answers4

5

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.

2

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

Ahmed Khaled
  • 308
  • 3
  • 14
0
import discord
from discord.ext import commands
import os

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

@client.event
async def on_ready():
    print("Log : "+str(client.user))

@client.command()
async def rs(ctx):
    await ctx.send("Restarting...")
    os.startfile(__file__)
    os._exit(1)

client.run("token")
0

Or you could just do ctx.bot.close() which restarts your bot depending on your hosting, idk

Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42