-2

So I was trying to make a shutdown command for my discord bot. I want to do this in case the token is released (I'm very careless) and the bot is hijacked by someone else and I am unavailable. The code I have written is below:

@commands.has_permissions(view_audit_log = True)
async def shutdown(ctx):
  print('Bot is shutting down')
  await ctx.send('@PercyJackson and @Berekyah the bot is being shut down')
  exit()

I initially tried quit() but then tried exit(). The bot didn't work. Everything up to the last line is executed but the last line isn't.

The Thonnu
  • 3,578
  • 2
  • 8
  • 30
  • [Welcome to StackOverflow.](/tour) Please see [ask] and [help/on-topic]. Please [edit] your question to [format the code](/editing-help). Do you get any errors? If you do, include them in the question. `import os` and [try replacing `exit()` with `os._exit(0)`](https://stackoverflow.com/questions/9591350/what-is-difference-between-sys-exit0-and-os-exit0). And I think you're being overcautious about your bot. Your bot's 'token' is meant to be secretive and known only to you. If the token is kept a secret and not shared, no one can "hijack" your bot. – The Amateur Coder Jul 31 '22 at 05:33
  • Also, you can 'regenerate' the bot's token in [your Discord Developer Portal](https://discord.com/developers/applications). That way, the bot won't run when someone tries to run it with the previous token. The bot will run only with the newly-regenerated token. I think you can regenerate it how many ever times you want. "Shutting down the bot" won't prevent other people from running your bot as long as they have your bot's token. The only way to prevent it is [changing/regenerating the token in your Dev Portal](https://www.writebots.com/discord-bot-token/). Change the token in your code too. – The Amateur Coder Jul 31 '22 at 05:43

1 Answers1

1

the way you tried it, you just terminate the Python script, which then causes the bot to time out on Discord. It's better if you do it with the integrated function:

@commands.has_permissions(view_audit_log = True)
async def shutdown(ctx):
  print('Bot is shutting down')
  await ctx.send('@PercyJackson and @Berekyah the bot is being shut down')
  await ctx.bot.logout()

Also, you can easily regenerate the token in the Discord Developer Portal. Then you add it back into your code and nothing more can be done with the published token.