All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.freya022.botcommands.internal.components.handler.ComponentDescriptor.kt Maven / Gradle / Ivy

package io.github.freya022.botcommands.internal.components.handler

import io.github.freya022.botcommands.api.components.annotations.ComponentData
import io.github.freya022.botcommands.api.core.Logging.toUnwrappedLogger
import io.github.freya022.botcommands.api.core.reflect.wrap
import io.github.freya022.botcommands.api.core.service.getService
import io.github.freya022.botcommands.api.core.utils.hasAnnotationRecursive
import io.github.freya022.botcommands.api.parameters.AggregatedParameter
import io.github.freya022.botcommands.api.parameters.resolvers.ICustomResolver
import io.github.freya022.botcommands.internal.ExecutableMixin
import io.github.freya022.botcommands.internal.components.handler.options.ComponentHandlerParameterImpl
import io.github.freya022.botcommands.internal.components.handler.options.builder.ComponentHandlerOptionBuilderImpl
import io.github.freya022.botcommands.internal.core.BContextImpl
import io.github.freya022.botcommands.internal.core.options.OptionType
import io.github.freya022.botcommands.internal.core.options.builder.CustomOptionBuilderImpl
import io.github.freya022.botcommands.internal.core.reflection.toMemberParamFunction
import io.github.freya022.botcommands.internal.core.service.provider.canCreateWrappedService
import io.github.freya022.botcommands.internal.options.transformParameters
import io.github.freya022.botcommands.internal.parameters.OptionParameter
import io.github.freya022.botcommands.internal.parameters.ResolverContainer
import io.github.freya022.botcommands.internal.parameters.toFallbackOptionBuilder
import io.github.freya022.botcommands.internal.utils.ReflectionUtils.declaringClass
import io.github.freya022.botcommands.internal.utils.annotationRef
import io.github.freya022.botcommands.internal.utils.shortSignature
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent
import kotlin.reflect.KClass
import kotlin.reflect.KFunction

internal class ComponentDescriptor internal constructor(
    override val context: BContextImpl,
    handler: KFunction<*>,
    eventType: KClass
) : ExecutableMixin {
    override val eventFunction = handler.toMemberParamFunction(context, eventType)
    override val parameters: List

    init {
        val resolverContainer = context.getService()
        parameters = eventFunction.transformParameters(
            builderBlock = { function, parameter, declaredName ->
                val optionParameter = OptionParameter.fromSelfAggregate(function, declaredName)
                if (parameter.hasAnnotationRecursive()) {
                    ComponentHandlerOptionBuilderImpl(optionParameter)
                } else if (/* TODO remove */ context.serviceContainer.canCreateWrappedService(parameter) != null) {
                    // Fallback to component data if no service is found
                    function.declaringClass.java.toUnwrappedLogger()
                        .warn { "Component data parameter '$declaredName' must be annotated with ${annotationRef()}, it will be enforced in a later release, in ${function.shortSignature}" }

                    if (resolverContainer.hasResolverOfType>(parameter.wrap())) {
                        CustomOptionBuilderImpl(optionParameter)
                    } else {
                        ComponentHandlerOptionBuilderImpl(optionParameter)
                    }
                } else {
                    optionParameter.toFallbackOptionBuilder(context.serviceContainer, resolverContainer)
                }
            },
            aggregateBlock = { ComponentHandlerParameterImpl(this, it, eventType) }
        )
    }

    internal val optionSize = parameters.sumOf { p -> p.allOptions.count { o -> o.optionType == OptionType.OPTION } }

    @Suppress("DeprecatedCallableAddReplaceWith")
    @Deprecated("For removal, confusing on whether it searches nested parameters, prefer using collection operations on 'parameters' instead, make an extension or an utility method")
    override fun getParameter(declaredName: String): AggregatedParameter? =
        parameters.find { it.name == declaredName }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy