<commit_before>from heufybot.moduleinterface import BotModule
from traceback import format_exc


class BotCommand(BotModule):
    name = "UnknownCommand"

    def actions(self):
        return [ ("botmessage", 1, self.handleCommand),
                 ("commandhelp", 1, self.displayHelp) ]

    def load(self):
        self.help = "This module does not provide a help text."
        self.commandHelp = {}

    def triggers(self):
        return []

    def handleCommand(self, data):
        if self._shouldExecute(data["server"], data["source"], data["user"], data["command"]):
            try:
                self.execute(data["server"], data["source"], data["command"].lower(), data["params"], data)
            except Exception as ex:
                error = "Python execution error while running command: {}: {}".format(type(ex).__name__, ex.message)
                self.bot.servers[data["server"]].outputHandler.cmdPRIVMSG(data["source"], error)
                self.bot.log.failure("Error while running command:\n {ex}", ex=format_exc())

    def displayHelp(self, server, request):
        if not self.bot.moduleHandler.useModuleOnServer(self.name, server):
            return None
        if request.lower() == self.name.lower():
            return self.help
        if request.lower() in [x.lower() for x in self.triggers()]:
            try:
                return self.commandHelp[request.lower()]
            except KeyError:
                return self.help
        return None

    def _shouldExecute(self, server, source, user, command):
        if not self.bot.moduleHandler.useModuleOnServer(self.name, server):
            return False
        if command.lower() not in [x.lower() for x in self.triggers()]:
            return False
        if not self.checkPermissions(server, source, user, command.lower()):
            return False<commit_msg>Make the code for displaying module help a bit nicer<commit_after>from heufybot.moduleinterface import BotModule
from traceback import format_exc


class BotCommand(BotModule):
    name = "UnknownCommand"

    def actions(self):
        return [ ("botmessage", 1, self.handleCommand),
                 ("commandhelp", 1, self.displayHelp) ]

    def load(self):
        self.help = "This module does not provide a help text."
        self.commandHelp = {}

    def triggers(self):
        return []

    def handleCommand(self, data):
        if self._shouldExecute(data["server"], data["source"], data["user"], data["command"]):
            try:
                self.execute(data["server"], data["source"], data["command"].lower(), data["params"], data)
            except Exception as ex:
                error = "Python execution error while running command: {}: {}".format(type(ex).__name__, ex.message)
                self.bot.servers[data["server"]].outputHandler.cmdPRIVMSG(data["source"], error)
                self.bot.log.failure("Error while running command:\n {ex}", ex=format_exc())

    def displayHelp(self, server, request):
        if not self.bot.moduleHandler.useModuleOnServer(self.name, server):
            return None
        request = request.lower()
        if request == self.name.lower():
            return self.help
        if request in [x.lower() for x in self.triggers()]:
            if request in self.commandHelp:
                return self.commandHelp[request]
            else:
                return self.help
        return None

    def _shouldExecute(self, server, source, user, command):
        if not self.bot.moduleHandler.useModuleOnServer(self.name, server):
            return False
        if command.lower() not in [x.lower() for x in self.triggers()]:
            return False
        if not self.checkPermissions(server, source, user, command.lower()):
            return False
{'fraction_non_alphanumeric': 0.12161095025006581, 'fraction_numerical': 0.0010529086601737299, 'mean_word_length': 2.4545454545454546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1867582', 'n_tokens_mistral': 1010, 'n_tokens_neox': 980, 'n_words': 280}