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

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

The newest version!
package dev.kord.core.cache.data

import dev.kord.common.Locale
import dev.kord.common.annotation.KordExperimental
import dev.kord.common.entity.*
import dev.kord.common.entity.optional.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

@Serializable
public data class InteractionData(
    val id: Snowflake,
    val applicationId: Snowflake,
    val type: InteractionType,
    val data: ApplicationInteractionData,
    val guildId: OptionalSnowflake = OptionalSnowflake.Missing,
    val channel: Optional = Optional.Missing(),
    val channelId: OptionalSnowflake = OptionalSnowflake.Missing,
    val member: Optional = Optional.Missing(),
    val user: Optional = Optional.Missing(),
    val token: String,
    val permissions: Optional = Optional.Missing(),
    val version: Int,
    val message: Optional = Optional.Missing(),
    val appPermissions: Optional = Optional.Missing(),
    val locale: Optional = Optional.Missing(),
    val guildLocale: Optional = Optional.Missing(),
    val entitlements: Optional> = Optional.Missing(),
) {
    public companion object {
        public fun from(interaction: DiscordInteraction): InteractionData {
            return with(interaction) {
                InteractionData(
                    id,
                    applicationId,
                    type,
                    ApplicationInteractionData.from(data, guildId.value),
                    guildId,
                    channel.map { ChannelData.from(it) },
                    channelId,
                    member.map { it.toData(it.user.value!!.id, guildId.value!!) },
                    //borrow user from member if present
                    user.switchOnMissing(member.flatMap { it.user }).map { it.toData() },
                    token,
                    member.map { it.permissions },
                    version,
                    message.map {
                        MessageData.from(it)
                    },
                    appPermissions,
                    locale,
                    guildLocale,
                    entitlements.mapList { EntitlementData.from(it) },
                )
            }
        }
    }
}

@Serializable
public data class ResolvedObjectsData(
    val members: Optional> = Optional.Missing(),
    val users: Optional> = Optional.Missing(),
    val roles: Optional> = Optional.Missing(),
    val channels: Optional> = Optional.Missing(),
    val messages: Optional> = Optional.Missing(),
    val attachments: Optional> = Optional.Missing()
) {
    public companion object {
        public fun from(data: ResolvedObjects, guildId: Snowflake?): ResolvedObjectsData {
            return ResolvedObjectsData(
                members = data.members.mapValues { MemberData.from(it.key, guildId!!, it.value) },
                channels = data.channels.mapValues { ChannelData.from(it.value) },
                roles = data.roles.mapValues { RoleData.from(guildId!!, it.value) },
                users = data.users.mapValues { it.value.toData() },
                messages = data.messages.mapValues { it.value.toData() },
                attachments = data.attachments.mapValues { AttachmentData.from(it.value) }
            )
        }
    }
}

@Serializable
public data class ApplicationInteractionData(
    val id: OptionalSnowflake = OptionalSnowflake.Missing,
    val type: Optional = Optional.Missing(),
    val targetId: OptionalSnowflake = OptionalSnowflake.Missing,
    val name: Optional = Optional.Missing(),
    val options: Optional> = Optional.Missing(),
    val resolvedObjectsData: Optional = Optional.Missing(),
    val guildId: OptionalSnowflake = OptionalSnowflake.Missing,
    val customId: Optional = Optional.Missing(),
    val componentType: Optional = Optional.Missing(),
    val values: Optional> = Optional.Missing(),
    val components: Optional> = Optional.Missing()
) {
    public companion object {

        public fun from(
            data: InteractionCallbackData,
            interactionGuildId: Snowflake?, // this is the id of the guild the interaction was triggered in
        ): ApplicationInteractionData {
            return with(data) {
                ApplicationInteractionData(
                    id,
                    type,
                    targetId,
                    name,
                    options.map { it.map { option -> OptionData.from(option) } },
                    resolved.map { ResolvedObjectsData.from(it, interactionGuildId) },
                    guildId, // this is the id of the guild the command is registered to
                    customId,
                    componentType,
                    values = values,
                    components = components.mapList { ComponentData.from(it) }
                )
            }
        }
    }
}


@Serializable
public data class OptionData(
    val name: String,
    val value: Optional> = Optional.Missing(),
    val values: Optional>> = Optional.Missing(),
    val subCommands: Optional> = Optional.Missing(),
    val focused: OptionalBoolean = OptionalBoolean.Missing
) {
    public companion object {
        public fun from(data: Option): OptionData = with(data) {
            when (data) {
                is SubCommand -> OptionData(name, values = data.options)
                is CommandArgument<*> -> OptionData(name, value = Optional(data), focused = data.focused)
                is CommandGroup -> OptionData(name, subCommands = data.options)
            }
        }
    }
}


/**
 * A serializer whose sole purpose is to provide a No-Op serializer for [Any].
 * The serializer is used when the generic type is neither known nor relevant to the serialization process
 *
 * e.g: `Choice<@Serializable(NotSerializable::class) Any?>`
 * The serialization is handled by [Choice] serializer instead where we don't care about the generic type.
 */
@KordExperimental
public object NotSerializable : KSerializer {
    override fun deserialize(decoder: Decoder): Nothing = error("This operation is not supported.")
    override val descriptor: SerialDescriptor = String.serializer().descriptor
    override fun serialize(encoder: Encoder, value: Any?): Nothing = error("This operation is not supported.")
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy