
com.freya02.botcommands.internal.commands.prefixed.TextCommandVariation.kt Maven / Gradle / Ivy
package com.freya02.botcommands.internal.commands.prefixed
import com.freya02.botcommands.api.commands.prefixed.BaseCommandEvent
import com.freya02.botcommands.api.commands.prefixed.CommandEvent
import com.freya02.botcommands.api.commands.prefixed.builder.TextCommandVariationBuilder
import com.freya02.botcommands.internal.BContextImpl
import com.freya02.botcommands.internal.IExecutableInteractionInfo
import com.freya02.botcommands.internal.commands.application.slash.SlashUtils.getCheckedDefaultValue
import com.freya02.botcommands.internal.core.CooldownService
import com.freya02.botcommands.internal.core.options.Option
import com.freya02.botcommands.internal.core.options.OptionType
import com.freya02.botcommands.internal.core.reflection.toMemberEventFunction
import com.freya02.botcommands.internal.parameters.CustomMethodOption
import com.freya02.botcommands.internal.throwInternal
import com.freya02.botcommands.internal.transform
import com.freya02.botcommands.internal.utils.InsertOptionResult
import com.freya02.botcommands.internal.utils.mapFinalParameters
import com.freya02.botcommands.internal.utils.mapOptions
import com.freya02.botcommands.internal.utils.tryInsertNullableOption
import mu.KotlinLogging
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import kotlin.reflect.full.callSuspendBy
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.jvm.jvmErasure
private val logger = KotlinLogging.logger { }
class TextCommandVariation internal constructor(
private val context: BContextImpl,
val info: TextCommandInfo,
builder: TextCommandVariationBuilder
) : IExecutableInteractionInfo {
override val eventFunction = builder.toMemberEventFunction(context)
override val parameters: List
val completePattern: Regex?
private val useTokenizedEvent: Boolean
init {
useTokenizedEvent = eventFunction.eventParameter.type.jvmErasure.isSubclassOf(CommandEvent::class)
parameters = builder.optionAggregateBuilders.transform {
TextCommandParameter(context, it)
}
completePattern = when {
parameters.flatMap { it.allOptions }.any { it.optionType == OptionType.OPTION } -> CommandPattern.of(this)
else -> null
}
}
internal suspend fun execute(
jdaEvent: MessageReceivedEvent,
cooldownService: CooldownService,
args: String,
matchResult: MatchResult?
): ExecutionResult {
val event = when {
useTokenizedEvent -> CommandEventImpl.create(context, jdaEvent, args)
else -> BaseCommandEventImpl(context, jdaEvent, args)
}
val groupsIterator = matchResult?.groups?.iterator()
groupsIterator?.next() //Skip entire match
val optionValues = parameters.mapOptions { option ->
if (tryInsertOption(event, this, option, groupsIterator, args) == InsertOptionResult.ABORT)
return ExecutionResult.CONTINUE //Go to next variation
}
cooldownService.applyCooldown(info, event)
function.callSuspendBy(parameters.mapFinalParameters(event, optionValues))
return ExecutionResult.OK
}
/**
* Will return a null value if it can go to the next option
*
* A non-null value is returned immediately to #insertAggregate caller
*/
private suspend fun tryInsertOption(
event: BaseCommandEvent,
optionMap: MutableMap
© 2015 - 2025 Weber Informatics LLC | Privacy Policy