All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.jakejmattson.kutils.internal.command.CommandParsing.kt Maven / Gradle / Ivy

There is a newer version: 0.18.1
Show newest version
package me.jakejmattson.kutils.internal.command

data class RawInputs(
    val rawMessageContent: String,
    val commandName: String,
    val commandArgs: List = listOf(),
    val prefixCount: Int)

internal fun stripPrefixInvocation(message: String, prefix: String): RawInputs {
    val prefixSeq = generateSequence(prefix) { it + prefix }
    val prefixBlock = prefixSeq.takeWhile { message.startsWith(it) }.last()
    val trimmed = message.removePrefix(prefixBlock)
    val invocationCount = (message.length - trimmed.length) / prefix.length

    return produceCommandStruct(message, trimmed, invocationCount)
}

internal fun stripMentionInvocation(message: String): RawInputs {
    val trimmedMessage = message.substringAfter(">").trimStart()
    return produceCommandStruct(message, trimmedMessage)
}

private fun produceCommandStruct(raw: String, message: String, invocationCount: Int = 1): RawInputs {
    if (!message.contains(" ")) {
        return RawInputs(raw, message.toLowerCase(), listOf(), invocationCount)
    }

    val commandName = message.substring(0, message.indexOf(" ")).toLowerCase()
    val commandArgs = message.substring(message.indexOf(" ") + 1).split(" ")

    return RawInputs(raw, commandName, commandArgs, invocationCount)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy