<commit_before>package listeners

import annotation.Command
import data.CommandData
import net.dv8tion.jda.core.entities.Message
import net.dv8tion.jda.core.events.message.MessageReceivedEvent
import net.dv8tion.jda.core.hooks.ListenerAdapter
import java.io.File

class MessageListener(val commandPrefix: String) : ListenerAdapter() {
  val commands = mutableMapOf<String, CommandData>()

  init {
    val extensionsDirectory = File(ClassLoader.getSystemResource("extensions/").toURI())
    extensionsDirectory.list()
        .filter { it.endsWith(".class") }
        .forEach {
          val className = "extensions." + it.substring(0, it.length - 6)
          val extension = Class.forName(className)
          val instance = extension.newInstance()
          extension.methods
              .filter { it.isAnnotationPresent(Command::class.java) }
              .forEach {
                val annotation = it.getAnnotation(Command::class.java)
                val commandName = when (annotation.name) {
                  "" -> it.name
                  else -> annotation.name
                }
                commands.put(commandName, CommandData(instance, it))
              }
        }
  }

  override fun onMessageReceived(evt: MessageReceivedEvent) {
    val author = evt.author
    val message = evt.message
    if (!author.isBot) {
      if (message.content.startsWith(commandPrefix)) {
        val commandKey = message.content.split(" ")[0].substring(commandPrefix.length)
        if (commandKey in commands) {
          val command = commands[commandKey]
          if (command != null) {
            val method = command.method
            val argsList = getArgs(message, command)
            method.invoke(command.instance, evt, *argsList.toTypedArray())
          }
        }
      }
    }
  }

  fun getArgs(message: Message, command: CommandData): MutableList<Any> {
    val delimitFinalParameter = method.getAnnotation(Command::class.java).delimitFinalParameter
    val numArgs = command.method.parameterCount
    val splitArgs = message.content.split(" ").toMutableList<Any>()
    splitArgs.removeAt(0)
    if (delimitFinalParameter) {
      return splitArgs
    } else {
      val args = mutableListOf<Any>()
      for (i in (0..numArgs - 3)) {
        args.add(splitArgs[i])
        splitArgs.removeAt(0)
      }
      args.add(splitArgs.joinToString(" "))
      return args
    }
  }
}<commit_msg>Reorganized getArgs to getParameters and changed parameter types<commit_after>package listeners

import annotation.Command
import data.CommandData
import net.dv8tion.jda.core.entities.Message
import net.dv8tion.jda.core.events.message.MessageReceivedEvent
import net.dv8tion.jda.core.hooks.ListenerAdapter
import java.io.File

class MessageListener(val commandPrefix: String) : ListenerAdapter() {
  val commands = mutableMapOf<String, CommandData>()

  init {
    val extensionsDirectory = File(ClassLoader.getSystemResource("extensions/").toURI())
    extensionsDirectory.list()
        .filter { it.endsWith(".class") }
        .forEach {
          val className = "extensions." + it.substring(0, it.length - 6)
          val extension = Class.forName(className)
          val instance = extension.newInstance()
          extension.methods
              .filter { it.isAnnotationPresent(Command::class.java) }
              .forEach {
                val annotation = it.getAnnotation(Command::class.java)
                val commandName = when (annotation.name) {
                  "" -> it.name
                  else -> annotation.name
                }
                commands.put(commandName, CommandData(instance, it))
              }
        }
  }

  override fun onMessageReceived(evt: MessageReceivedEvent) {
    val author = evt.author
    val message = evt.message
    if (!author.isBot) {
      if (message.content.startsWith(commandPrefix)) {
        val commandKey = message.content.split(" ")[0].substring(commandPrefix.length)
        if (commandKey in commands && commands[commandKey] != null) {
          val method = commands[commandKey].method
          val parameters = getParameters(message, method)
          method.invoke(command.instance, evt, *parameters.toTypedArray())
        }
      }
    }
  }

  fun getParameters(evt: MessageReceivedEvent, method: Method): MutableList<Any> {
    val delimitFinalParameter = method.getAnnotation(Command::class.java).delimitFinalParameter
    val parameterTypes = method.parameterTypes.toMutableList<Any>()
    val parameterCount = method.parameterCount
    val splitParameters = evt.message.content.split(" ").toMutableList<Any>()
    splitParameters.removeAt(0)
    if (delimitFinalParameter) {
      return splitParameters
    } else {
      val parameters = mutableListOf<Any>()
      for (i in (0..parameterCount - 3)) {
        parameters.add(splitParameters[i])
        splitParameters.removeAt(0)
      }
      parameters.add(splitParameters.joinToString(" "))
      return parameters
    }
  }
}
{'fraction_non_alphanumeric': 0.10657051282051282, 'fraction_numerical': 0.004006410256410256, 'mean_word_length': 2.6445255474452556, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 16, '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': '1073021', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1342, 'n_words': 272}