Reputation: 99
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!")```
Upvotes: 5
Views: 13768
Reputation: 1
@app_commands.command(name="restart", description="Restart the discord bot.")
@app_commands.checks.has_permissions(manage_guild=True)
async def restart(self, interaction):
if is_owner(interaction.user.id):
log.info(f"\n"
f"{interaction.user} has restarted the bot.\n"
f"{interaction.guild.id}\n")
await self.responses.success(self, message="Restarting Bot", interaction=interaction)
os.execv(sys.executable, ["python"] + sys.argv)
Upvotes: 0
Reputation: 1
Or you could just do ctx.bot.close()
which restarts your bot depending on your hosting, idk
Upvotes: 0
Reputation: 1
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")
Upvotes: -1
Reputation: 51
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.
Upvotes: 5
Reputation: 418
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
Upvotes: 2