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

io.github.freya022.botcommands.api.parameters.ComponentParameterResolver.kt Maven / Gradle / Ivy

Go to download

A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.

There is a newer version: 3.0.0-alpha.18
Show newest version
package io.github.freya022.botcommands.api.parameters

import io.github.freya022.botcommands.api.components.annotations.JDAButtonListener
import io.github.freya022.botcommands.api.components.annotations.JDASelectMenuListener
import io.github.freya022.botcommands.api.components.builder.IPersistentActionableComponent
import io.github.freya022.botcommands.internal.components.ComponentDescriptor
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent
import kotlin.reflect.KParameter
import kotlin.reflect.KType

/**
 * Parameter resolver for parameters of [@JDAButtonListener][JDAButtonListener]
 * and [@JDASelectMenuListener][JDASelectMenuListener].
 *
 * Needs to be implemented alongside a [ParameterResolver] subclass.
 *
 * @param T Type of the implementation
 * @param R Type of the returned resolved objects
 */
interface ComponentParameterResolver where T : ParameterResolver,
                                                       T : ComponentParameterResolver {
    /**
     * Returns a resolved object from this component interaction.
     *
     * If this returns `null`, and the parameter is required, i.e., not [nullable][KType.isMarkedNullable]
     * or [optional][KParameter.isOptional], then the handler will throw.
     *
     * @param descriptor The descriptor of the component handler being executed
     * @param event      The corresponding event
     * @param arg        One of the data passed by the user in [IPersistentActionableComponent.bindTo]
     */
    fun resolve(descriptor: ComponentDescriptor, event: GenericComponentInteractionCreateEvent, arg: String): R? =
        throw NotImplementedError("${this.javaClass.simpleName} must implement the 'resolve' or 'resolveSuspend' method")

    /**
     * Returns a resolved object from this component interaction.
     *
     * If this returns `null`, and the parameter is required, i.e., not [nullable][KType.isMarkedNullable]
     * or [optional][KParameter.isOptional], then the handler will throw.
     *
     * @param descriptor The descriptor of the component being executed
     * @param event      The corresponding event
     * @param arg        One of the data passed by the user in [IPersistentActionableComponent.bindTo]
     */
    @JvmSynthetic
    suspend fun resolveSuspend(
        descriptor: ComponentDescriptor,
        event: GenericComponentInteractionCreateEvent,
        arg: String
    ) = resolve(descriptor, event, arg)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy