How would I create a command to shutdown my Discord.py Bot?

Harrison Copp picture Harrison Copp · Feb 6, 2019 · Viewed 13.6k times · Source

I have rewritten my existing Discord Bot so that commands work via @client.command.

Here is an example of the clear command so you can see how the language works.

@client.command()
async def echo(*args):
  output = ""
  for word in args:
    output += word
    output += " "
  await client.say(output)

I would like to create 2 commands. One which will -shutdown the bot, taking it offline, and unresponsive. And the other which will -restart the bot, meaning that if I update the code, I run the restart command, and the bot will go offline, reboot, and then come back.

How would I go about doing this?

As I want the commands to only work for me, I have left by Discord User ID Below so you can include that in the code. 432234718860148749.

Thanks in advance, H

Answer

VoxelPrismatic picture VoxelPrismatic · May 15, 2019

https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot.logout has your answer

@client.command()
@commands.is_owner()
async def shutdown(ctx):
    await ctx.bot.logout()

However, I do not yet know how to restart a bot via a command