Skip to content

Commit c901125

Browse files
authored
Set an empty list for vararg arguments with no values available (#234)
Allows getting an empty list in slash commands Added checks, check for at least 1 required option in text command varargs so there is no confusion between, for example: - <prefix><command> <Int> - <prefix><command> <Int> <Int[0..N]>
1 parent 7cd9777 commit c901125

File tree

6 files changed

+24
-2
lines changed

6 files changed

+24
-2
lines changed

src/main/kotlin/io/github/freya022/botcommands/api/commands/annotations/VarArgs.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.github.freya022.botcommands.api.commands.application.slash.options.bui
77
import io.github.freya022.botcommands.api.commands.text.annotations.TextOption
88
import io.github.freya022.botcommands.api.commands.text.builder.TextCommandVariationBuilder
99
import io.github.freya022.botcommands.api.commands.text.options.builder.inlineClassOptionVararg
10+
import net.dv8tion.jda.api.interactions.commands.build.CommandData
1011

1112
/**
1213
* Generates N command options from the specified [@SlashOption][SlashOption] or [@TextOption][TextOption].
@@ -32,12 +33,16 @@ import io.github.freya022.botcommands.api.commands.text.options.builder.inlineCl
3233
@Retention(AnnotationRetention.RUNTIME)
3334
annotation class VarArgs(
3435
/**
35-
* The number of times this option needs to appear, which must be between 1 and {@value CommandData#MAX_OPTIONS}.
36+
* The number of times this option needs to appear, must be positive.
3637
*/
3738
val value: Int,
3839

3940
/**
4041
* The number of required options for this vararg.
42+
*
43+
* For slash commands, this must can be 0, positive, or how many remaining options there are until [MAX_OPTIONS][CommandData.MAX_OPTIONS].
44+
*
45+
* For text commands, this must be at least 1.
4146
*/
4247
val numRequired: Int = 1
4348
)

src/main/kotlin/io/github/freya022/botcommands/internal/commands/application/slash/builder/SlashCommandBuilderImpl.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ internal abstract class SlashCommandBuilderImpl internal constructor(
4444
}
4545

4646
final override fun optionVararg(declaredName: String, amount: Int, requiredAmount: Int, optionNameSupplier: (Int) -> String, block: SlashCommandOptionBuilder.(Int) -> Unit) {
47+
require(amount > 0) { "Amount must be positive" }
48+
require(requiredAmount >= 0) { "Required amount must be zero or positive" }
49+
4750
//Same as in TextCommandVariationBuilder#optionVararg
4851
varargAggregate(declaredName) {
4952
for (i in 0..<amount) {

src/main/kotlin/io/github/freya022/botcommands/internal/commands/application/slash/options/builder/SlashCommandOptionAggregateBuilderImpl.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ internal class SlashCommandOptionAggregateBuilderImpl internal constructor(
3939
}
4040

4141
override fun optionVararg(declaredName: String, amount: Int, requiredAmount: Int, optionNameSupplier: (Int) -> String, block: SlashCommandOptionBuilder.(Int) -> Unit) {
42+
require(amount > 0) { "Amount must be positive" }
43+
require(requiredAmount >= 0) { "Required amount must be zero or positive" }
44+
4245
//Same as in TextCommandVariationBuilder#optionVararg
4346
varargAggregate(declaredName) {
4447
for (i in 0..<amount) {

src/main/kotlin/io/github/freya022/botcommands/internal/commands/text/builder/TextCommandVariationBuilderImpl.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ internal class TextCommandVariationBuilderImpl internal constructor(
5353
}
5454

5555
override fun optionVararg(declaredName: String, amount: Int, requiredAmount: Int, optionNameSupplier: (Int) -> String, block: TextCommandOptionBuilder.(Int) -> Unit) {
56-
if (aggregateContainer.hasVararg())
56+
if (hasVararg())
5757
throwArgument("Cannot have more than 1 vararg in text commands")
58+
require(amount > 0) { "Amount must be positive" }
59+
require(requiredAmount > 0) { "Required amount must be positive" }
5860

5961
//Same as in SlashCommandBuilder#optionVararg
6062
aggregateContainer.varargAggregate(declaredName) {

src/main/kotlin/io/github/freya022/botcommands/internal/commands/text/options/builder/TextCommandOptionAggregateBuilderImpl.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ internal class TextCommandOptionAggregateBuilderImpl internal constructor(
3535
override fun optionVararg(declaredName: String, amount: Int, requiredAmount: Int, optionNameSupplier: (Int) -> String, block: TextCommandOptionBuilder.(Int) -> Unit) {
3636
if (hasVararg())
3737
throwArgument("Cannot have more than 1 vararg in text commands")
38+
require(amount > 0) { "Amount must be positive" }
39+
require(requiredAmount > 0) { "Required amount must be positive" }
3840

3941
//Same as in TextCommandVariationBuilder#optionVararg
4042
varargAggregate(declaredName) {

src/main/kotlin/io/github/freya022/botcommands/internal/utils/ExecutionUtils.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.github.freya022.botcommands.internal.parameters.AggregatedParameterMix
77
import io.github.freya022.botcommands.internal.parameters.MethodParameterMixin
88
import io.github.freya022.botcommands.internal.utils.ReflectionUtils.function
99
import kotlin.reflect.KParameter
10+
import kotlin.reflect.full.valueParameters
1011

1112
internal enum class InsertOptionResult {
1213
OK,
@@ -68,12 +69,18 @@ private suspend fun insertAggregate(firstParam: Any, aggregatedObjects: MutableM
6869
}
6970
} else {
7071
val aggregatorArguments: MutableMap<KParameter, Any?> = HashMap(aggregator.parametersSize)
72+
var addedOption = false
7173
for (option in parameter.options) {
7274
//This is necessary to distinguish between null mappings and default mappings
7375
if (option in optionValues) {
7476
aggregatorArguments[option] = optionValues[option]
77+
addedOption = true
7578
}
7679
}
80+
// If this is not a vararg, it should throw later when calling the aggregator
81+
if (!addedOption && parameter.isVararg) {
82+
aggregatorArguments[parameter.aggregator.kFunction.valueParameters.last()] = emptyList<Any?>()
83+
}
7784

7885
for (nestedAggregatedParameter in parameter.nestedAggregatedParameters) {
7986
insertAggregate(firstParam, aggregatorArguments, optionValues, nestedAggregatedParameter)

0 commit comments

Comments
 (0)