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

commonMain.builder.guild.GuildOnboardingModifyBuilder.kt Maven / Gradle / Ivy

package dev.kord.rest.builder.guild

import dev.kord.common.annotation.KordDsl
import dev.kord.common.entity.OnboardingMode
import dev.kord.common.entity.OnboardingPromptType
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.common.entity.optional.map
import dev.kord.common.entity.optional.mapCopy
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.builder.RequestBuilder
import dev.kord.rest.json.request.GuildOnboardingModifyRequest
import dev.kord.rest.json.request.OnboardingPromptOptionRequest
import dev.kord.rest.json.request.OnboardingPromptRequest
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

@KordDsl
public class GuildOnboardingModifyBuilder : AuditRequestBuilder {
    override var reason: String? = null

    private var _prompts: Optional> = Optional.Missing()

    /** The prompts shown during onboarding and in customize community. */
    public var prompts: MutableList? by ::_prompts.delegate()

    private var _defaultChannelIds: Optional> = Optional.Missing()

    /** The IDs of the channels that members get opted into automatically. */
    public var defaultChannelIds: MutableList? by ::_defaultChannelIds.delegate()

    private var _enabled: OptionalBoolean = OptionalBoolean.Missing

    /** Whether onboarding is enabled in the guild. */
    public var enabled: Boolean? by ::_enabled.delegate()

    private var _mode: Optional = Optional.Missing()

    /** Current [mode][OnboardingMode] of onboarding. */
    public var mode: OnboardingMode? by ::_mode.delegate()

    override fun toRequest(): GuildOnboardingModifyRequest = GuildOnboardingModifyRequest(
        prompts = _prompts.map { it.map(OnboardingPromptBuilder::toRequest) },
        defaultChannelIds = _defaultChannelIds.mapCopy(),
        enabled = _enabled,
        mode = _mode,
    )
}

/** Add a [channelId] to [defaultChannelIds][GuildOnboardingModifyBuilder.defaultChannelIds]. */
public fun GuildOnboardingModifyBuilder.defaultChannelId(channelId: Snowflake) {
    defaultChannelIds?.add(channelId) ?: run { defaultChannelIds = mutableListOf(channelId) }
}

/**
 * Add a prompt to [prompts][GuildOnboardingModifyBuilder.prompts].
 *
 * @param type The [type][OnboardingPromptType] of the prompt.
 * @param title The title of the prompt.
 * @param singleSelect Indicates whether users are limited to selecting one option for the prompt.
 * @param required Indicates whether the prompt is required before a user completes the onboarding flow.
 * @param inOnboarding Indicates whether the prompt is present in the onboarding flow. If `false`, the prompt will only
 * appear in the Channels & Roles tab.
 */
public inline fun GuildOnboardingModifyBuilder.prompt(
    type: OnboardingPromptType,
    title: String,
    singleSelect: Boolean,
    required: Boolean,
    inOnboarding: Boolean,
    builder: OnboardingPromptBuilder.() -> Unit,
) {
    contract { callsInPlace(builder, EXACTLY_ONCE) }
    val prompt = OnboardingPromptBuilder(type, title, singleSelect, required, inOnboarding).apply(builder)
    prompts?.add(prompt) ?: run { prompts = mutableListOf(prompt) }
}


@KordDsl
public class OnboardingPromptBuilder(
    /** The [type][OnboardingPromptType] of the prompt. */
    public var type: OnboardingPromptType,
    /** The title of the prompt. */
    public var title: String,
    /** Indicates whether users are limited to selecting one option for the prompt. */
    public var singleSelect: Boolean,
    /** Indicates whether the prompt is required before a user completes the onboarding flow. */
    public var required: Boolean,
    /**
     * Indicates whether the prompt is present in the onboarding flow. If `false`, the prompt will only appear in the
     * Channels & Roles tab.
     */
    public var inOnboarding: Boolean,
) : RequestBuilder {

    /** The ID of the prompt. */
    public var id: Snowflake? = null

    /** The options available within the prompt. */
    public val options: MutableList = mutableListOf()

    override fun toRequest(): OnboardingPromptRequest = OnboardingPromptRequest(
        id = id ?: Snowflake.min, // it needs an ID, if we don't have one yet, pass 0
        type = type,
        options = options.map(OnboardingPromptOptionBuilder::toRequest),
        title = title,
        singleSelect = singleSelect,
        required = required,
        inOnboarding = inOnboarding,
    )
}

/**
 * Add an option to [options][OnboardingPromptBuilder.options].
 *
 * @param title The title of the option.
 */
public inline fun OnboardingPromptBuilder.option(
    title: String,
    builder: OnboardingPromptOptionBuilder.() -> Unit,
) {
    contract { callsInPlace(builder, EXACTLY_ONCE) }
    options.add(OnboardingPromptOptionBuilder(title).apply(builder))
}


@KordDsl
public class OnboardingPromptOptionBuilder(
    /** The title of the option. */
    public var title: String,
) : RequestBuilder {

    /** The IDs for the channels a member is added to when the option is selected. */
    public val channelIds: MutableList = mutableListOf()

    /** The IDs for the roles assigned to a member when the option is selected. */
    public val roleIds: MutableList = mutableListOf()

    /** The description of the option. */
    public var description: String? = null

    override fun toRequest(): OnboardingPromptOptionRequest = OnboardingPromptOptionRequest(
        channelIds = channelIds.toList(),
        roleIds = roleIds.toList(),
        title = title,
        description = description,
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy