# Mattermost Bot.
#  Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
#  published under MIT-License


import os
from inspect import cleandoc

# pylint: disable=wrong-import-position
from AbstractCommand import AbstractCommand
class CoreCommandHCF(AbstractCommand):
    TRIGGER = "hcf"
    CONFIG = {"display_name": "somebot-command", "auto_complete": False}
    CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PUBLIC+"Halt, but cleanup first."
    USEINFO = cleandoc("""
        ``/hcf`` - Halt and catch fire. Or actually halt but cleanup first.
        Used to wipe all this bot user's commands and then fail horribly.
        BOT_ADMIN only command.
        """)


    def on_POST(self, request, data):
        self._require_bot_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)

        request.respond_cmd_chan("### Halting and catching fire. Bye...\n# :fire::fire::fire::fire::fire:")

        # wipe all commands creted by this bot (not limited to this instance!)
        for team in self.bot.api.get_teams():
            for command in self.bot.api.list_custom_slash_commands_for_team(team["id"]):
                if command["display_name"] == "somebot-command":
                    self.bot.api.delete_slash_command(command["id"])

        # fail horribly.
        self.bot.on_shutdown()
        os._exit(42)
