From 88953521557b584f9927901a6232b405becd5576 Mon Sep 17 00:00:00 2001 From: lukellmann <47486203+lukellmann@users.noreply.github.com> Date: Sun, 3 Sep 2023 03:32:21 +0200 Subject: [PATCH] Add AttachmentFlags (#867) See https://github.com/discord/discord-api-docs/pull/6272 --- common/api/common.api | 76 +++- .../dev/kord/common/entity/AttachmentFlag.kt | 334 ++++++++++++++++++ .../kotlin/entity/DiscordMessage.kt | 20 +- core/api/core.api | 13 +- .../kotlin/cache/data/AttachmentData.kt | 20 +- .../commonMain/kotlin/entity/Attachment.kt | 34 +- 6 files changed, 469 insertions(+), 28 deletions(-) create mode 100644 common/build/generated/ksp/metadata/commonMain/kotlin/dev/kord/common/entity/AttachmentFlag.kt diff --git a/common/api/common.api b/common/api/common.api index 01642caab40..551779c7d9f 100644 --- a/common/api/common.api +++ b/common/api/common.api @@ -827,6 +827,70 @@ public final class dev/kord/common/entity/ArchiveDuration$Week : dev/kord/common public static final field INSTANCE Ldev/kord/common/entity/ArchiveDuration$Week; } +public abstract class dev/kord/common/entity/AttachmentFlag { + public static final field Companion Ldev/kord/common/entity/AttachmentFlag$Companion; + public synthetic fun (ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun equals (Ljava/lang/Object;)Z + public final fun getShift ()I + public final fun getValue ()I + public final fun hashCode ()I + public final fun plus (Ldev/kord/common/entity/AttachmentFlag;)Ldev/kord/common/entity/AttachmentFlags; + public final fun plus (Ldev/kord/common/entity/AttachmentFlags;)Ldev/kord/common/entity/AttachmentFlags; + public final fun toString ()Ljava/lang/String; +} + +public final class dev/kord/common/entity/AttachmentFlag$Companion { + public final fun fromShift (I)Ldev/kord/common/entity/AttachmentFlag; + public final fun getEntries ()Ljava/util/List; +} + +public final class dev/kord/common/entity/AttachmentFlag$IsRemix : dev/kord/common/entity/AttachmentFlag { + public static final field INSTANCE Ldev/kord/common/entity/AttachmentFlag$IsRemix; +} + +public final class dev/kord/common/entity/AttachmentFlag$Unknown : dev/kord/common/entity/AttachmentFlag { +} + +public final class dev/kord/common/entity/AttachmentFlagKt { + public static final fun AttachmentFlags (Ljava/lang/Iterable;)Ldev/kord/common/entity/AttachmentFlags; + public static final fun AttachmentFlags (Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/AttachmentFlags; + public static final fun AttachmentFlags ([Ldev/kord/common/entity/AttachmentFlag;)Ldev/kord/common/entity/AttachmentFlags; + public static final fun AttachmentFlags ([Ldev/kord/common/entity/AttachmentFlags;)Ldev/kord/common/entity/AttachmentFlags; + public static synthetic fun AttachmentFlags$default (Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ldev/kord/common/entity/AttachmentFlags; + public static final fun AttachmentFlags0 (Ljava/lang/Iterable;)Ldev/kord/common/entity/AttachmentFlags; +} + +public final class dev/kord/common/entity/AttachmentFlags { + public static final field Companion Ldev/kord/common/entity/AttachmentFlags$Companion; + public final fun contains (Ldev/kord/common/entity/AttachmentFlag;)Z + public final fun contains (Ldev/kord/common/entity/AttachmentFlags;)Z + public final fun copy (Lkotlin/jvm/functions/Function1;)Ldev/kord/common/entity/AttachmentFlags; + public fun equals (Ljava/lang/Object;)Z + public final fun getValue ()I + public final fun getValues ()Ljava/util/Set; + public fun hashCode ()I + public final fun minus (Ldev/kord/common/entity/AttachmentFlag;)Ldev/kord/common/entity/AttachmentFlags; + public final fun minus (Ldev/kord/common/entity/AttachmentFlags;)Ldev/kord/common/entity/AttachmentFlags; + public final fun plus (Ldev/kord/common/entity/AttachmentFlag;)Ldev/kord/common/entity/AttachmentFlags; + public final fun plus (Ldev/kord/common/entity/AttachmentFlags;)Ldev/kord/common/entity/AttachmentFlags; + public fun toString ()Ljava/lang/String; +} + +public final class dev/kord/common/entity/AttachmentFlags$Builder { + public fun ()V + public fun (I)V + public synthetic fun (IILkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun build ()Ldev/kord/common/entity/AttachmentFlags; + public final fun unaryMinus (Ldev/kord/common/entity/AttachmentFlag;)V + public final fun unaryMinus (Ldev/kord/common/entity/AttachmentFlags;)V + public final fun unaryPlus (Ldev/kord/common/entity/AttachmentFlag;)V + public final fun unaryPlus (Ldev/kord/common/entity/AttachmentFlags;)V +} + +public final class dev/kord/common/entity/AttachmentFlags$Companion { + public final fun serializer ()Lkotlinx/serialization/KSerializer; +} + public final class dev/kord/common/entity/AuditLogChange { public static final field Companion Ldev/kord/common/entity/AuditLogChange$Companion; public fun (Ljava/lang/Object;Ljava/lang/Object;Ldev/kord/common/entity/AuditLogChangeKey;)V @@ -2637,13 +2701,14 @@ public final class dev/kord/common/entity/DiscordApplicationRoleConnectionMetada public final class dev/kord/common/entity/DiscordAttachment { public static final field Companion Ldev/kord/common/entity/DiscordAttachment$Companion; - public synthetic fun (ILdev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V - public fun (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)V - public synthetic fun (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun (ILdev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V + public fun (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)V + public synthetic fun (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun component1 ()Ldev/kord/common/entity/Snowflake; public final fun component10 ()Ldev/kord/common/entity/optional/OptionalBoolean; public final fun component11 ()Ldev/kord/common/entity/optional/Optional; public final fun component12 ()Ldev/kord/common/entity/optional/Optional; + public final fun component13 ()Ldev/kord/common/entity/optional/Optional; public final fun component2 ()Ljava/lang/String; public final fun component3 ()Ldev/kord/common/entity/optional/Optional; public final fun component4 ()Ldev/kord/common/entity/optional/Optional; @@ -2652,14 +2717,15 @@ public final class dev/kord/common/entity/DiscordAttachment { public final fun component7 ()Ljava/lang/String; public final fun component8 ()Ldev/kord/common/entity/optional/OptionalInt; public final fun component9 ()Ldev/kord/common/entity/optional/OptionalInt; - public final fun copy (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)Ldev/kord/common/entity/DiscordAttachment; - public static synthetic fun copy$default (Ldev/kord/common/entity/DiscordAttachment;Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/Object;)Ldev/kord/common/entity/DiscordAttachment; + public final fun copy (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)Ldev/kord/common/entity/DiscordAttachment; + public static synthetic fun copy$default (Ldev/kord/common/entity/DiscordAttachment;Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/Object;)Ldev/kord/common/entity/DiscordAttachment; public fun equals (Ljava/lang/Object;)Z public final fun getContentType ()Ldev/kord/common/entity/optional/Optional; public final fun getDescription ()Ldev/kord/common/entity/optional/Optional; public final fun getDurationSecs ()Ldev/kord/common/entity/optional/Optional; public final fun getEphemeral ()Ldev/kord/common/entity/optional/OptionalBoolean; public final fun getFilename ()Ljava/lang/String; + public final fun getFlags ()Ldev/kord/common/entity/optional/Optional; public final fun getHeight ()Ldev/kord/common/entity/optional/OptionalInt; public final fun getId ()Ldev/kord/common/entity/Snowflake; public final fun getProxyUrl ()Ljava/lang/String; diff --git a/common/build/generated/ksp/metadata/commonMain/kotlin/dev/kord/common/entity/AttachmentFlag.kt b/common/build/generated/ksp/metadata/commonMain/kotlin/dev/kord/common/entity/AttachmentFlag.kt new file mode 100644 index 00000000000..3ae9d21959b --- /dev/null +++ b/common/build/generated/ksp/metadata/commonMain/kotlin/dev/kord/common/entity/AttachmentFlag.kt @@ -0,0 +1,334 @@ +// THIS FILE IS AUTO-GENERATED, DO NOT EDIT! +@file:Suppress(names = arrayOf("IncorrectFormatting", "ReplaceArrayOfWithLiteral", + "SpellCheckingInspection", "GrazieInspection")) + +package dev.kord.common.entity + +import kotlin.LazyThreadSafetyMode.PUBLICATION +import kotlin.contracts.InvocationKind.EXACTLY_ONCE +import kotlin.contracts.contract +import kotlin.jvm.JvmName +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +/** + * See [AttachmentFlag]s in the + * [Discord Developer Documentation](https://discord.com/developers/docs/resources/channel#attachment-object-attachment-flags). + */ +public sealed class AttachmentFlag( + /** + * The position of the bit that is set in this [AttachmentFlag]. This is always in 0..30. + */ + public val shift: Int, +) { + init { + require(shift in 0..30) { """shift has to be in 0..30 but was $shift""" } + } + + /** + * The raw value used by Discord. + */ + public val `value`: Int + get() = 1 shl shift + + /** + * Returns an instance of [AttachmentFlags] that has all bits set that are set in `this` and + * [flag]. + */ + public operator fun plus(flag: AttachmentFlag): AttachmentFlags = + AttachmentFlags(this.value or flag.value) + + /** + * Returns an instance of [AttachmentFlags] that has all bits set that are set in `this` and + * [flags]. + */ + public operator fun plus(flags: AttachmentFlags): AttachmentFlags = + AttachmentFlags(this.value or flags.value) + + final override fun equals(other: Any?): Boolean = this === other || + (other is AttachmentFlag && this.shift == other.shift) + + final override fun hashCode(): Int = shift.hashCode() + + final override fun toString(): String = if (this is Unknown) + "AttachmentFlag.Unknown(shift=$shift)" else "AttachmentFlag.${this::class.simpleName}" + + /** + * An unknown [AttachmentFlag]. + * + * This is used as a fallback for [AttachmentFlag]s that haven't been added to Kord yet. + */ + public class Unknown internal constructor( + shift: Int, + ) : AttachmentFlag(shift) + + /** + * This attachment has been edited using the remix feature on mobile. + */ + public object IsRemix : AttachmentFlag(2) + + public companion object { + /** + * A [List] of all known [AttachmentFlag]s. + */ + public val entries: List by lazy(mode = PUBLICATION) { + listOf( + IsRemix, + ) + } + + + /** + * Returns an instance of [AttachmentFlag] with [AttachmentFlag.shift] equal to the + * specified [shift]. + * + * @throws IllegalArgumentException if [shift] is not in 0..30. + */ + public fun fromShift(shift: Int): AttachmentFlag = when (shift) { + 2 -> IsRemix + else -> Unknown(shift) + } + } +} + +/** + * A collection of multiple [AttachmentFlag]s. + * + * ## Creating an instance of [AttachmentFlags] + * + * You can create an instance of [AttachmentFlags] using the following methods: + * ```kotlin + * // from individual AttachmentFlags + * val attachmentFlags1 = AttachmentFlags(AttachmentFlag.IsRemix, AttachmentFlag.fromShift(22)) + * + * // from an Iterable + * val iterable: Iterable = TODO() + * val attachmentFlags2 = AttachmentFlags(iterable) + * + * // using a builder + * val attachmentFlags3 = AttachmentFlags { + * +attachmentFlags2 + * +AttachmentFlag.IsRemix + * -AttachmentFlag.fromShift(22) + * } + * ``` + * + * ## Modifying an existing instance of [AttachmentFlags] + * + * You can create a modified copy of an existing instance of [AttachmentFlags] using the [copy] + * method: + * ```kotlin + * attachmentFlags.copy { + * +AttachmentFlag.IsRemix + * } + * ``` + * + * ## Mathematical operators + * + * All [AttachmentFlags] objects can use `+`/`-` operators: + * ```kotlin + * val attachmentFlags1 = attachmentFlags + AttachmentFlag.IsRemix + * val attachmentFlags2 = attachmentFlags - AttachmentFlag.fromShift(22) + * val attachmentFlags3 = attachmentFlags1 + attachmentFlags2 + * ``` + * + * ## Checking for [AttachmentFlag]s + * + * You can use the [contains] operator to check whether an instance of [AttachmentFlags] contains + * specific [AttachmentFlag]s: + * ```kotlin + * val hasAttachmentFlag = AttachmentFlag.IsRemix in attachmentFlags + * val hasAttachmentFlags = AttachmentFlags(AttachmentFlag.IsRemix, AttachmentFlag.fromShift(22)) in attachmentFlags + * ``` + * + * ## Unknown [AttachmentFlag]s + * + * Whenever [AttachmentFlag]s haven't been added to Kord yet, they will be deserialized as instances + * of [AttachmentFlag.Unknown]. + * + * You can also use [AttachmentFlag.fromShift] to check for [unknown][AttachmentFlag.Unknown] + * [AttachmentFlag]s. + * ```kotlin + * val hasUnknownAttachmentFlag = AttachmentFlag.fromShift(23) in attachmentFlags + * ``` + * + * @see AttachmentFlag + * @see AttachmentFlags.Builder + */ +@Serializable(with = AttachmentFlags.Serializer::class) +public class AttachmentFlags internal constructor( + /** + * The raw value used by Discord. + */ + public val `value`: Int, +) { + /** + * A [Set] of all [AttachmentFlag]s contained in this instance of [AttachmentFlags]. + */ + public val values: Set + get() = buildSet { + var remaining = value + var shift = 0 + while (remaining != 0) { + if ((remaining and 1) != 0) add(AttachmentFlag.fromShift(shift)) + remaining = remaining ushr 1 + shift++ + } + } + + /** + * Checks if this instance of [AttachmentFlags] has all bits set that are set in [flag]. + */ + public operator fun contains(flag: AttachmentFlag): Boolean = + this.value and flag.value == flag.value + + /** + * Checks if this instance of [AttachmentFlags] has all bits set that are set in [flags]. + */ + public operator fun contains(flags: AttachmentFlags): Boolean = + this.value and flags.value == flags.value + + /** + * Returns an instance of [AttachmentFlags] that has all bits set that are set in `this` and + * [flag]. + */ + public operator fun plus(flag: AttachmentFlag): AttachmentFlags = + AttachmentFlags(this.value or flag.value) + + /** + * Returns an instance of [AttachmentFlags] that has all bits set that are set in `this` and + * [flags]. + */ + public operator fun plus(flags: AttachmentFlags): AttachmentFlags = + AttachmentFlags(this.value or flags.value) + + /** + * Returns an instance of [AttachmentFlags] that has all bits set that are set in `this` except + * the bits that are set in [flag]. + */ + public operator fun minus(flag: AttachmentFlag): AttachmentFlags = + AttachmentFlags(this.value and flag.value.inv()) + + /** + * Returns an instance of [AttachmentFlags] that has all bits set that are set in `this` except + * the bits that are set in [flags]. + */ + public operator fun minus(flags: AttachmentFlags): AttachmentFlags = + AttachmentFlags(this.value and flags.value.inv()) + + /** + * Returns a copy of this instance of [AttachmentFlags] modified with [builder]. + */ + public inline fun copy(builder: Builder.() -> Unit): AttachmentFlags { + contract { callsInPlace(builder, EXACTLY_ONCE) } + return Builder(value).apply(builder).build() + } + + override fun equals(other: Any?): Boolean = this === other || + (other is AttachmentFlags && this.value == other.value) + + override fun hashCode(): Int = value.hashCode() + + override fun toString(): String = "AttachmentFlags(values=$values)" + + public class Builder( + private var `value`: Int = 0, + ) { + /** + * Sets all bits in the [Builder] that are set in this [AttachmentFlag]. + */ + public operator fun AttachmentFlag.unaryPlus() { + this@Builder.value = this@Builder.value or this.value + } + + /** + * Sets all bits in the [Builder] that are set in this [AttachmentFlags]. + */ + public operator fun AttachmentFlags.unaryPlus() { + this@Builder.value = this@Builder.value or this.value + } + + /** + * Unsets all bits in the [Builder] that are set in this [AttachmentFlag]. + */ + public operator fun AttachmentFlag.unaryMinus() { + this@Builder.value = this@Builder.value and this.value.inv() + } + + /** + * Unsets all bits in the [Builder] that are set in this [AttachmentFlags]. + */ + public operator fun AttachmentFlags.unaryMinus() { + this@Builder.value = this@Builder.value and this.value.inv() + } + + /** + * Returns an instance of [AttachmentFlags] that has all bits set that are currently set in + * this [Builder]. + */ + public fun build(): AttachmentFlags = AttachmentFlags(value) + } + + internal object Serializer : KSerializer { + override val descriptor: SerialDescriptor = + PrimitiveSerialDescriptor("dev.kord.common.entity.AttachmentFlags", + PrimitiveKind.INT) + + private val `delegate`: KSerializer = Int.serializer() + + override fun serialize(encoder: Encoder, `value`: AttachmentFlags) { + encoder.encodeSerializableValue(delegate, value.value) + } + + override fun deserialize(decoder: Decoder): AttachmentFlags = + AttachmentFlags(decoder.decodeSerializableValue(delegate)) + } +} + +/** + * Returns an instance of [AttachmentFlags] built with [AttachmentFlags.Builder]. + */ +public inline fun AttachmentFlags(builder: AttachmentFlags.Builder.() -> Unit = {}): + AttachmentFlags { + contract { callsInPlace(builder, EXACTLY_ONCE) } + return AttachmentFlags.Builder().apply(builder).build() +} + +/** + * Returns an instance of [AttachmentFlags] that has all bits set that are set in any element of + * [flags]. + */ +public fun AttachmentFlags(vararg flags: AttachmentFlag): AttachmentFlags = AttachmentFlags { + flags.forEach { +it } +} + +/** + * Returns an instance of [AttachmentFlags] that has all bits set that are set in any element of + * [flags]. + */ +public fun AttachmentFlags(vararg flags: AttachmentFlags): AttachmentFlags = AttachmentFlags { + flags.forEach { +it } +} + +/** + * Returns an instance of [AttachmentFlags] that has all bits set that are set in any element of + * [flags]. + */ +public fun AttachmentFlags(flags: Iterable): AttachmentFlags = AttachmentFlags { + flags.forEach { +it } +} + +/** + * Returns an instance of [AttachmentFlags] that has all bits set that are set in any element of + * [flags]. + */ +@JvmName("AttachmentFlags0") +public fun AttachmentFlags(flags: Iterable): AttachmentFlags = AttachmentFlags { + flags.forEach { +it } +} diff --git a/common/src/commonMain/kotlin/entity/DiscordMessage.kt b/common/src/commonMain/kotlin/entity/DiscordMessage.kt index 84306dab4d9..68c4bb916cd 100644 --- a/common/src/commonMain/kotlin/entity/DiscordMessage.kt +++ b/common/src/commonMain/kotlin/entity/DiscordMessage.kt @@ -114,6 +114,14 @@ ], ) +@file:Generate( + INT_FLAGS, name = "AttachmentFlag", + docUrl = "https://discord.com/developers/docs/resources/channel#attachment-object-attachment-flags", + entries = [ + Entry("IsRemix", shift = 2, kDoc = "This attachment has been edited using the remix feature on mobile."), + ], +) + package dev.kord.common.entity import dev.kord.common.entity.optional.Optional @@ -457,21 +465,13 @@ public data class DiscordAttachment( val url: String, @SerialName("proxy_url") val proxyUrl: String, - /* - Do not trust the docs: - 2020-11-06 This field is marked as nullable but can be missing instead. - */ val height: OptionalInt? = OptionalInt.Missing, - /* - Do not trust the docs: - 2020-11-06 This field is marked as nullable but can be missing instead. - */ val width: OptionalInt? = OptionalInt.Missing, - val ephemeral: OptionalBoolean = OptionalBoolean.Missing, @SerialName("duration_secs") val durationSecs: Optional = Optional.Missing(), - val waveform: Optional = Optional.Missing() + val waveform: Optional = Optional.Missing(), + val flags: Optional = Optional.Missing(), ) /** diff --git a/core/api/core.api b/core/api/core.api index 4d6d1ab94f7..5d184781e3d 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -2648,13 +2648,14 @@ public final class dev/kord/core/cache/data/ApplicationInteractionData$Companion public final class dev/kord/core/cache/data/AttachmentData { public static final field Companion Ldev/kord/core/cache/data/AttachmentData$Companion; - public synthetic fun (ILdev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V - public fun (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)V - public synthetic fun (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun (ILdev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V + public fun (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)V + public synthetic fun (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun component1 ()Ldev/kord/common/entity/Snowflake; public final fun component10 ()Ldev/kord/common/entity/optional/OptionalBoolean; public final fun component11 ()Ldev/kord/common/entity/optional/Optional; public final fun component12 ()Ldev/kord/common/entity/optional/Optional; + public final fun component13 ()Ldev/kord/common/entity/optional/Optional; public final fun component2 ()Ljava/lang/String; public final fun component3 ()Ldev/kord/common/entity/optional/Optional; public final fun component4 ()Ldev/kord/common/entity/optional/Optional; @@ -2663,14 +2664,15 @@ public final class dev/kord/core/cache/data/AttachmentData { public final fun component7 ()Ljava/lang/String; public final fun component8 ()Ldev/kord/common/entity/optional/OptionalInt; public final fun component9 ()Ldev/kord/common/entity/optional/OptionalInt; - public final fun copy (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)Ldev/kord/core/cache/data/AttachmentData; - public static synthetic fun copy$default (Ldev/kord/core/cache/data/AttachmentData;Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/Object;)Ldev/kord/core/cache/data/AttachmentData; + public final fun copy (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)Ldev/kord/core/cache/data/AttachmentData; + public static synthetic fun copy$default (Ldev/kord/core/cache/data/AttachmentData;Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/String;Ljava/lang/String;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalInt;Ldev/kord/common/entity/optional/OptionalBoolean;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/Object;)Ldev/kord/core/cache/data/AttachmentData; public fun equals (Ljava/lang/Object;)Z public final fun getContentType ()Ldev/kord/common/entity/optional/Optional; public final fun getDescription ()Ldev/kord/common/entity/optional/Optional; public final fun getDurationSecs ()Ldev/kord/common/entity/optional/Optional; public final fun getEphemeral ()Ldev/kord/common/entity/optional/OptionalBoolean; public final fun getFilename ()Ljava/lang/String; + public final fun getFlags ()Ldev/kord/common/entity/optional/Optional; public final fun getHeight ()Ldev/kord/common/entity/optional/OptionalInt; public final fun getId ()Ldev/kord/common/entity/Snowflake; public final fun getProxyUrl ()Ljava/lang/String; @@ -5827,6 +5829,7 @@ public final class dev/kord/core/entity/Attachment : dev/kord/core/entity/KordEn public final fun getDescription ()Ljava/lang/String; public final fun getDuration-FghU774 ()Lkotlin/time/Duration; public final fun getFilename ()Ljava/lang/String; + public final fun getFlags ()Ldev/kord/common/entity/AttachmentFlags; public final fun getHeight ()Ljava/lang/Integer; public fun getId ()Ldev/kord/common/entity/Snowflake; public fun getKord ()Ldev/kord/core/Kord; diff --git a/core/src/commonMain/kotlin/cache/data/AttachmentData.kt b/core/src/commonMain/kotlin/cache/data/AttachmentData.kt index 509117004fe..46f12824a0e 100644 --- a/core/src/commonMain/kotlin/cache/data/AttachmentData.kt +++ b/core/src/commonMain/kotlin/cache/data/AttachmentData.kt @@ -1,5 +1,6 @@ package dev.kord.core.cache.data +import dev.kord.common.entity.AttachmentFlags import dev.kord.common.entity.DiscordAttachment import dev.kord.common.entity.Snowflake import dev.kord.common.entity.optional.Optional @@ -21,11 +22,26 @@ public data class AttachmentData( val width: OptionalInt? = OptionalInt.Missing, val ephemeral: OptionalBoolean = OptionalBoolean.Missing, val durationSecs: Optional = Optional.Missing(), - val waveform: Optional = Optional.Missing() + val waveform: Optional = Optional.Missing(), + val flags: Optional = Optional.Missing(), ) { public companion object { public fun from(entity: DiscordAttachment): AttachmentData = with(entity) { - AttachmentData(id, filename, description, contentType, size, url, proxyUrl, height, width, ephemeral, durationSecs, waveform) + AttachmentData( + id, + filename, + description, + contentType, + size, + url, + proxyUrl, + height, + width, + ephemeral, + durationSecs, + waveform, + flags, + ) } } } diff --git a/core/src/commonMain/kotlin/entity/Attachment.kt b/core/src/commonMain/kotlin/entity/Attachment.kt index 5c0022bde37..cd6b00b4e46 100644 --- a/core/src/commonMain/kotlin/entity/Attachment.kt +++ b/core/src/commonMain/kotlin/entity/Attachment.kt @@ -1,5 +1,6 @@ package dev.kord.core.entity +import dev.kord.common.entity.AttachmentFlags import dev.kord.common.entity.DiscordAttachment import dev.kord.common.entity.Snowflake import dev.kord.common.entity.optional.value @@ -60,6 +61,14 @@ public data class Attachment(val data: AttachmentData, override val kord: Kord) */ val width: Int? get() = data.width.value + /** + * Whether this attachment is ephemeral. + * + * Ephemeral attachments will automatically be removed after a set period of time. Ephemeral attachments on messages + * are guaranteed to be available as long as the message itself exists. + */ + val isEphemeral: Boolean get() = data.ephemeral.orElse(false) + /** * The duration of the audio file (currently for voice messages). */ @@ -70,13 +79,14 @@ public data class Attachment(val data: AttachmentData, override val kord: Kord) */ val waveform: ByteArray? get() = data.waveform.value?.decodeBase64Bytes() + /** The [AttachmentFlags] of this attachment. */ + val flags: AttachmentFlags? get() = data.flags.value + /** * If this file is displayed as a spoiler. Denoted by the `SPOILER_` prefix in the name. */ val isSpoiler: Boolean get() = filename.startsWith("SPOILER_") - val isEphemeral: Boolean get() = data.ephemeral.discordBoolean - /** * If this file is an image. Denoted by the presence of a [width] and [height]. */ @@ -94,8 +104,20 @@ public data class Attachment(val data: AttachmentData, override val kord: Kord) } } -public fun Attachment.toRawType(): DiscordAttachment { - with(data) { - return DiscordAttachment(id, filename, description, contentType, size, url, proxyUrl, height, width) - } +public fun Attachment.toRawType(): DiscordAttachment = with(data) { + DiscordAttachment( + id, + filename, + description, + contentType, + size, + url, + proxyUrl, + height, + width, + ephemeral, + durationSecs, + waveform, + flags, + ) }