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