Harrison Copp
Harrison Copp

Reputation: 97

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

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

Upvotes: 0

Views: 26326

Answers (2)

Macintosh Fan
Macintosh Fan

Reputation: 405

As of the latest version—Discord.py 1.3.3—try this:

# Close the bot
@client.command(aliases=["quit"])
@commands.has_permissions(administrator=True)
async def close(ctx):
    await client.close()
    print("Bot Closed")  # This is optional, but it is there to tell you.

Upvotes: 3

VoxelPrismatic
VoxelPrismatic

Reputation: 148

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

Upvotes: 3

Related Questions