me.jakejmattson.kutils.api.arguments.QuoteArg.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of KUtils Show documentation
Show all versions of KUtils Show documentation
A Discord bot framework for Kotlin.
package me.jakejmattson.kutils.api.arguments
import me.jakejmattson.kutils.api.dsl.arguments.*
import me.jakejmattson.kutils.api.dsl.command.CommandEvent
open class QuoteArg(override val name: String = "Quote") : ArgumentType() {
companion object : QuoteArg()
override fun convert(arg: String, args: List, event: CommandEvent<*>): ArgumentResult {
val quotationMark = '"'
if (!arg.startsWith(quotationMark)) {
return ArgumentResult.Error("Expected an opening quotation mark, found: $arg")
}
val rawQuote = if (arg.endsWith(quotationMark)) {
arg
} else {
args.takeUntil { !it.endsWith(quotationMark) }.joinToString(" ")
}
if (!rawQuote.endsWith(quotationMark)) {
return ArgumentResult.Error("Missing closing quotation mark.")
}
val quote = rawQuote.trim(quotationMark)
val consumedCount = quote.split(" ").size
return ArgumentResult.Success(quote, consumedCount)
}
override fun generateExamples(event: CommandEvent<*>) = listOf("\"A Quote\"")
}
private fun List.takeUntil(predicate: (String) -> Boolean): List {
val result = this.takeWhile(predicate).toMutableList()
val index = result.size
if (index in indices)
result.add(this[index])
return result
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy