fix: Dynamically parse arguments
This commit is contained in:
parent
411e183e04
commit
fce8afa2b2
9 changed files with 27 additions and 14 deletions
|
@ -68,8 +68,8 @@ open class Bot internal constructor(): IGroup {
|
|||
|
||||
private fun getContext(message: Message): Context {
|
||||
val candidate = message.content.parsePrefixCommandAndArguments()
|
||||
val context = Context(this, message, candidate?.first, candidate?.second)
|
||||
context.command = getCommand(candidate?.second?.get(0))
|
||||
val context = Context(this, message, candidate?.first, candidate?.second?.toMutableList())
|
||||
context.command = getCommand(context.candidate?.removeAt(0))
|
||||
return context
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
package io.github.null2264.tsukumogami.core
|
||||
|
||||
import dev.kord.core.Kord
|
||||
import io.github.null2264.tsukumogami.core.module.BotModule
|
||||
import kotlin.reflect.KFunction
|
||||
|
||||
class BotHolder internal constructor(
|
||||
class BotBuilder internal constructor(
|
||||
val bot: Bot
|
||||
) {
|
||||
|
||||
internal val prefixes = mutableListOf<String>()
|
||||
var token: String
|
||||
get() = bot.token
|
||||
set(value) {
|
||||
|
@ -24,9 +22,9 @@ class BotHolder internal constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun bot(clazz: KFunction<Bot> = ::Bot, declaration: BotHolder.() -> Unit): Bot {
|
||||
fun bot(clazz: KFunction<Bot> = ::Bot, declaration: BotBuilder.() -> Unit): Bot {
|
||||
val bot = clazz.call()
|
||||
val holder = BotHolder(bot)
|
||||
val holder = BotBuilder(bot)
|
||||
declaration(holder)
|
||||
return holder.bot
|
||||
}
|
|
@ -16,8 +16,9 @@ class Context(
|
|||
/**
|
||||
* Potential command name and/or arguments
|
||||
*/
|
||||
val candidate: List<String>?,
|
||||
val candidate: MutableList<String>?,
|
||||
) {
|
||||
|
||||
val author get() = message.author
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,6 +3,7 @@ package io.github.null2264.tsukumogami.core.commands
|
|||
import io.github.null2264.tsukumogami.core.commands.converters.Converter
|
||||
|
||||
abstract class Arguments {
|
||||
|
||||
val args = mutableListOf<Argument<*>>()
|
||||
|
||||
fun <R : Any> args(
|
||||
|
@ -14,9 +15,18 @@ abstract class Arguments {
|
|||
return converter
|
||||
}
|
||||
|
||||
suspend fun parse(value: String) {
|
||||
suspend fun parse(values: List<String>?) {
|
||||
val currentValues = values?.toMutableList()
|
||||
|
||||
run {
|
||||
args.forEach { arg ->
|
||||
val value = try {
|
||||
currentValues?.removeAt(0)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
} ?: return@run
|
||||
arg.converter.parse(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,8 +13,7 @@ open class Command(
|
|||
|
||||
open suspend fun invoke(context: Context) {
|
||||
val parsedArguments = arguments.call()
|
||||
// TODO: Don't hardcode this
|
||||
parsedArguments.parse(context.candidate?.get(1) ?: "test")
|
||||
parsedArguments.parse(context.candidate)
|
||||
handler.invoke(context, parsedArguments)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,8 @@ class Group(
|
|||
override val allCommands: MutableMap<String, Command> = mutableMapOf()
|
||||
|
||||
override suspend fun invoke(context: Context) {
|
||||
val command = allCommands["TODO"] ?: return
|
||||
val command = allCommands[context.candidate?.get(0)] ?: return
|
||||
context.candidate?.removeAt(0)
|
||||
context.command = command
|
||||
command.invoke(context)
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ interface IGroup {
|
|||
declaration: IGroup.() -> Unit,
|
||||
) {
|
||||
val group = Group(name, alias, description)
|
||||
declaration(group)
|
||||
addCommand(group)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import io.github.null2264.tsukumogami.core.commands.Arguments
|
|||
import kotlin.reflect.KProperty
|
||||
|
||||
abstract class Converter<OutputType: Any?> {
|
||||
|
||||
lateinit var argumentObj: Argument<OutputType>
|
||||
|
||||
abstract var parsed: OutputType
|
||||
|
|
|
@ -3,7 +3,9 @@ package io.github.null2264.tsukumogami.core.commands.converters.impl
|
|||
import io.github.null2264.tsukumogami.core.commands.converters.Converter
|
||||
|
||||
class StringConverter : Converter<String>() {
|
||||
|
||||
override var parsed: String = ""
|
||||
|
||||
override suspend fun parse(input: String): String {
|
||||
this.parsed = input
|
||||
return input
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue