feat: Greedy argument

This commit is contained in:
Ahmad Ansori Palembani 2025-05-26 12:04:22 +07:00
parent ab5c116ced
commit 2e3c3cdd66
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
5 changed files with 29 additions and 9 deletions

View file

@ -6,6 +6,8 @@ import io.github.null2264.tsukumogami.core.commands.ext.user
class TestArguments : Arguments() {
val test by string("Test") {
isGreedy = true
default("Lmao")
}
}

View file

@ -17,12 +17,11 @@ abstract class Arguments {
}
suspend fun parse(context: Context) {
val currentValues = context.parseArguments()
val values = context.parseArguments()?.toMutableList() ?: return
run {
args.forEach { arg ->
val value = currentValues?.removeFirstOrNull() ?: return@run
arg.converter.parse(context, value)
arg.converter.consume(context, values)
}
}
}

View file

@ -7,11 +7,14 @@ import kotlin.reflect.KProperty
abstract class Converter<OutputType: Any?> {
open var isGreedy = false
lateinit var argumentObj: Argument<OutputType>
abstract var parsed: OutputType
abstract suspend fun parse(context: Context, input: String): OutputType
// FIXME: Maybe change the output to a boolean, indicates whether or not it successfully consume the inputs
abstract suspend fun consume(context: Context, inputs: MutableList<String>): OutputType
operator fun getValue(thisRef: Arguments, property: KProperty<*>): OutputType {
return this.parsed

View file

@ -7,8 +7,19 @@ class StringConverter : Converter<String>() {
override var parsed: String = ""
override suspend fun parse(context: Context, input: String): String {
this.parsed = input
return input
override suspend fun consume(context: Context, inputs: MutableList<String>): String {
this.parsed = if (isGreedy && inputs.size > 1) run {
val limit = inputs.size
var buffer = ""
var count = 0
for (i in (1..limit)) {
if (++count > 1) buffer += " "
if (count <= limit) {
buffer += inputs.removeFirstOrNull() ?: ""
} else break
}
buffer
} else inputs.removeFirstOrNull().orEmpty()
return this.parsed
}
}

View file

@ -3,7 +3,6 @@ package io.github.null2264.tsukumogami.core.commands.converters.impl
import dev.kord.common.entity.Snowflake
import dev.kord.core.entity.User
import io.github.null2264.tsukumogami.core.Context
import io.github.null2264.tsukumogami.core.annotation.TsukumogamiInternalApi
import io.github.null2264.tsukumogami.core.commands.converters.Converter
import io.github.null2264.tsukumogami.core.exceptions.CommandException
import io.github.null2264.tsukumogami.core.ext.users
@ -11,9 +10,15 @@ import kotlinx.coroutines.flow.firstOrNull
class UserConverter : Converter<User>() {
override var isGreedy: Boolean
get() = false
set(_) { error("UserConverter can't be greedy") }
override lateinit var parsed: User
override suspend fun parse(context: Context, input: String): User {
override suspend fun consume(context: Context, inputs: MutableList<String>): User {
val input = inputs.removeFirstOrNull() ?: throw CommandException("User ID is null")
if (input.equals("me", true)) {
val user = context.author
if (user != null) {