me.jakejmattson.kutils.api.arguments.ChoiceArg.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 ChoiceArg(override val name: String, vararg choices: T) : ArgumentType() {
private val enumerations = choices.associateBy { it.toString().toLowerCase() }
private val options = enumerations.keys
override fun convert(arg: String, args: List, event: CommandEvent<*>): ArgumentResult {
val selection = enumerations[arg.toLowerCase()] as? String
?: return ArgumentResult.Error("Invalid choice for $name. Available choices: ${options.joinToString()}")
return ArgumentResult.Success(selection)
}
override fun generateExamples(event: CommandEvent<*>) = options.toList()
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy