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

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

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

import dev.kord.cache.api.data.DataDescription
import dev.kord.cache.api.data.description
import dev.kord.common.entity.*
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable

private val MemberData.id get() = "$userId$guildId"

@Serializable
public data class MemberData(
    val userId: Snowflake,
    val guildId: Snowflake,
    val nick: Optional = Optional.Missing(),
    val roles: List,
    val joinedAt: Instant,
    val premiumSince: Optional = Optional.Missing(),
    val flags: GuildMemberFlags,
    val permissions: Optional = Optional.Missing(),
    val pending: OptionalBoolean = OptionalBoolean.Missing,
    val avatar: Optional = Optional.Missing(),
    val communicationDisabledUntil: Optional = Optional.Missing()
) {

    public companion object {
        public val description: DataDescription = description(MemberData::id)

        public fun from(userId: Snowflake, guildId: Snowflake, entity: DiscordGuildMember): MemberData = with(entity) {
            MemberData(
                userId = userId,
                guildId = guildId,
                nick = nick,
                roles = roles,
                joinedAt = joinedAt,
                premiumSince = premiumSince,
                flags = flags,
                pending = pending,
                avatar = avatar,
                communicationDisabledUntil = communicationDisabledUntil,
            )
        }


        public fun from(userId: Snowflake, guildId: Snowflake, entity: DiscordInteractionGuildMember): MemberData =
            with(entity) {
                MemberData(
                    userId = userId,
                    guildId = guildId,
                    nick = nick,
                    roles = roles,
                    joinedAt = joinedAt,
                    premiumSince = premiumSince,
                    flags = flags,
                    permissions = Optional.Value(permissions),
                    pending = pending,
                    avatar = avatar,
                    communicationDisabledUntil = communicationDisabledUntil,
                )
            }

        public fun from(userId: Snowflake, entity: DiscordAddedGuildMember): MemberData = with(entity) {
            MemberData(
                userId = userId,
                guildId = guildId,
                nick = nick,
                roles = roles,
                joinedAt = joinedAt,
                premiumSince = premiumSince,
                flags = flags,
                pending = pending,
                avatar = avatar,
                communicationDisabledUntil = communicationDisabledUntil,
            )
        }

        public fun from(entity: DiscordUpdatedGuildMember): MemberData = with(entity) {
            MemberData(
                userId = user.id,
                guildId = guildId,
                nick = nick,
                roles = roles,
                joinedAt = joinedAt,
                premiumSince = premiumSince,
                flags = flags,
                pending = pending,
                avatar = avatar,
                communicationDisabledUntil = communicationDisabledUntil,
            )
        }
    }
}

public fun DiscordGuildMember.toData(userId: Snowflake, guildId: Snowflake): MemberData =
    MemberData.from(userId, guildId, this)

public fun DiscordInteractionGuildMember.toData(userId: Snowflake, guildId: Snowflake): MemberData =
    MemberData.from(userId, guildId, this)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy