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

commonMain.cache.data.ComponentData.kt Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package dev.kord.core.cache.data

import dev.kord.common.entity.*
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.OptionalInt
import dev.kord.common.entity.optional.mapList
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonClassDiscriminator

@Serializable
@OptIn(ExperimentalSerializationApi::class)
@JsonClassDiscriminator("_type") // would otherwise conflict with `type` property
public sealed class ComponentData {
    public abstract val type: ComponentType
    public abstract val label: Optional
    //TODO: turn this emoji into a EmojiData, it's lacking the guild id
    public abstract val emoji: Optional
    public abstract val customId: Optional
    public abstract val url: Optional
    public abstract val disabled: OptionalBoolean
    public abstract val components: Optional>
    public abstract val defaultValues: Optional>
    public abstract val placeholder: Optional
    public abstract val minValues: OptionalInt
    public abstract val maxValues: OptionalInt
    public abstract val options: Optional>
    public abstract val minLength: OptionalInt
    public abstract val maxLength: OptionalInt
    public abstract val required: OptionalBoolean
    public abstract val value: Optional
    public abstract val channelTypes: Optional>

    public companion object {
        public fun from(entity: DiscordComponent): ComponentData = with (entity) {
            when (entity) {
                is DiscordChatComponent -> {
                    ChatComponentData(
                        type,
                        entity.style,
                        label,
                        emoji,
                        customId,
                        url,
                        disabled,
                        components.mapList { from(it) },
                        defaultValues = defaultValues,
                        placeholder = placeholder,
                        minValues = minValues,
                        maxValues = maxValues,
                        options = options.mapList { SelectOptionData.from(it) },
                        minLength = minLength,
                        maxLength = maxLength,
                        required = required,
                        value = value,
                        channelTypes = channelTypes
                    )
                }
                is DiscordTextInputComponent -> {
                    TextInputComponentData(
                        type,
                        entity.style,
                        label,
                        emoji,
                        customId,
                        url,
                        disabled,
                        components.mapList { from(it) },
                        defaultValues = defaultValues,
                        placeholder = placeholder,
                        minValues = minValues,
                        maxValues =  maxValues,
                        options = options.mapList { SelectOptionData.from(it) },
                        minLength = minLength,
                        maxLength = maxLength,
                        required = required,
                        value = value,
                        channelTypes = channelTypes
                    )
                }
            }
        }
    }
}

@Serializable
public data class ChatComponentData(
    override val type: ComponentType,
    val style: Optional = Optional.Missing(),
    override val label: Optional = Optional.Missing(),
    //TODO: turn this emoji into a EmojiData, it's lacking the guild id
    override val emoji: Optional = Optional.Missing(),
    override val customId: Optional = Optional.Missing(),
    override val url: Optional = Optional.Missing(),
    override val disabled: OptionalBoolean = OptionalBoolean.Missing,
    override val components: Optional> = Optional.Missing(),
    override val defaultValues: Optional> = Optional.Missing(),
    override val placeholder: Optional = Optional.Missing(),
    override val minValues: OptionalInt = OptionalInt.Missing,
    override val maxValues: OptionalInt = OptionalInt.Missing,
    override val options: Optional> = Optional.Missing(),
    override val minLength: OptionalInt = OptionalInt.Missing,
    override val maxLength: OptionalInt = OptionalInt.Missing,
    override val required: OptionalBoolean = OptionalBoolean.Missing,
    override val value: Optional = Optional.Missing(),
    override val channelTypes: Optional> = Optional.Missing(),
) : ComponentData()

@Serializable
public data class TextInputComponentData(
    override val type: ComponentType,
    val style: Optional = Optional.Missing(),
    override val label: Optional = Optional.Missing(),
    //TODO: turn this emoji into a EmojiData, it's lacking the guild id
    override val emoji: Optional = Optional.Missing(),
    override val customId: Optional = Optional.Missing(),
    override val url: Optional = Optional.Missing(),
    override val disabled: OptionalBoolean = OptionalBoolean.Missing,
    override val components: Optional> = Optional.Missing(),
    override val defaultValues: Optional> = Optional.Missing(),
    override val placeholder: Optional = Optional.Missing(),
    override val minValues: OptionalInt = OptionalInt.Missing,
    override val maxValues: OptionalInt = OptionalInt.Missing,
    override val options: Optional> = Optional.Missing(),
    override val minLength: OptionalInt = OptionalInt.Missing,
    override val maxLength: OptionalInt = OptionalInt.Missing,
    override val required: OptionalBoolean = OptionalBoolean.Missing,
    override val value: Optional = Optional.Missing(),
    override val channelTypes: Optional> = Optional.Missing(),
) : ComponentData()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy