Beispiel #1
0
 def dispatch(self,
              event: EventMessage,
              flag_list: List[str] = None) -> None:
     """
     Sends the function call out to whichever function, if one is found
     :param event: The message event which has triggered the function dispatch
     :param flag_list: List of flags to apply to function call
     """
     if flag_list is None:
         flag_list = []
     # Find the function name. Try joining each amount of words in the message until you find a valid function name
     function_message_split = event.command_text.split()
     if not function_message_split:
         function_message_split = [""]
     function_class_test = None
     function_name_test = ""
     function_args_test = ""
     for function_name_test in [
             " ".join(function_message_split[:x + 1])
             for x in range(len(function_message_split))[::-1]
     ]:
         function_class_test = self.get_function_by_name(function_name_test)
         function_args_test = " ".join(
             function_message_split)[len(function_name_test):].strip()
         if function_class_test is not None:
             break
     # If function isn't found, output a not found message
     if function_class_test is None:
         if EventMessage.FLAG_HIDE_ERRORS not in flag_list:
             event.reply(
                 event.create_response(
                     "Error, this is not a recognised function."))
             error = FunctionNotFoundError(self, event)
             logger.error(error.get_log_line())
         return
     function_class = function_class_test
     event.split_command_text(function_name_test, function_args_test)
     # Check function rights and permissions
     if not self.check_function_permissions(function_class, event.server,
                                            event.user, event.channel):
         event.reply(
             event.create_response(
                 "You do not have permission to use this function."))
         error = FunctionNotAllowedError(self, function_class, event)
         logger.error(error.get_log_line())
         return
     # If persistent, get the object, otherwise make one
     function_obj = self.get_function_object(function_class)
     # Try running the function, if it fails, return an error message
     try:
         response = function_obj.run(event)
         if response is not None:
             event.reply(response)
         else:
             event.reply(
                 event.create_response("The function returned no value."))
         return
     except Exception as e:
         error = FunctionError(e, self, function_obj, event)
         e_str = (str(e)[:250] + "..") if len(str(e)) > 250 else str(e)
         event.reply(
             event.create_response(
                 "Function failed with error message: {}".format(e_str)))
         logger.error(error.get_log_line())
         return