Skip to content

Commit

Permalink
Add SelectDefaultValues (#881)
Browse files Browse the repository at this point in the history
The DSL looks like this:
val message = channel.createMessage {
    actionRow {
        mentionableSelect("customId") {
            allowedValues = 0..5
            defaultUsers += userId
            defaultRoles += roleId
        }
    }
}

See discord/discord-api-docs#6442
  • Loading branch information
lukellmann authored Nov 18, 2023
1 parent a4f7143 commit d537432
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 43 deletions.
98 changes: 82 additions & 16 deletions common/api/common.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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 kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
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 [SelectDefaultValueType]s in the
* [Discord Developer Documentation](https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-default-value-structure).
*/
@Serializable(with = SelectDefaultValueType.Serializer::class)
public sealed class SelectDefaultValueType(
/**
* The raw value used by Discord.
*/
public val `value`: String,
) {
final override fun equals(other: Any?): Boolean = this === other ||
(other is SelectDefaultValueType && this.value == other.value)

final override fun hashCode(): Int = value.hashCode()

final override fun toString(): String =
if (this is Unknown) "SelectDefaultValueType.Unknown(value=$value)"
else "SelectDefaultValueType.${this::class.simpleName}"

/**
* An unknown [SelectDefaultValueType].
*
* This is used as a fallback for [SelectDefaultValueType]s that haven't been added to Kord yet.
*/
public class Unknown internal constructor(
`value`: String,
) : SelectDefaultValueType(value)

public object User : SelectDefaultValueType("user")

public object Role : SelectDefaultValueType("role")

public object Channel : SelectDefaultValueType("channel")

internal object Serializer : KSerializer<SelectDefaultValueType> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("dev.kord.common.entity.SelectDefaultValueType",
PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, `value`: SelectDefaultValueType) {
encoder.encodeString(value.value)
}

override fun deserialize(decoder: Decoder): SelectDefaultValueType =
from(decoder.decodeString())
}

public companion object {
/**
* A [List] of all known [SelectDefaultValueType]s.
*/
public val entries: List<SelectDefaultValueType> by lazy(mode = PUBLICATION) {
listOf(
User,
Role,
Channel,
)
}


/**
* Returns an instance of [SelectDefaultValueType] with [SelectDefaultValueType.value] equal
* to the specified [value].
*/
public fun from(`value`: String): SelectDefaultValueType = when (value) {
"user" -> User
"role" -> Role
"channel" -> Channel
else -> Unknown(value)
}
}
}
6 changes: 6 additions & 0 deletions common/src/commonMain/kotlin/entity/DiscordComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public sealed class DiscordComponent {
public abstract val components: Optional<List<DiscordComponent>>
public abstract val options: Optional<List<DiscordSelectOption>>
public abstract val placeholder: Optional<String>
@SerialName("default_values")
public abstract val defaultValues: Optional<List<DiscordSelectDefaultValue>>
@SerialName("min_values")
public abstract val minValues: OptionalInt
@SerialName("max_values")
Expand Down Expand Up @@ -119,6 +121,8 @@ public data class DiscordChatComponent(
override val components: Optional<List<DiscordComponent>> = Optional.Missing(),
override val options: Optional<List<DiscordSelectOption>> = Optional.Missing(),
override val placeholder: Optional<String> = Optional.Missing(),
@SerialName("default_values")
override val defaultValues: Optional<List<DiscordSelectDefaultValue>> = Optional.Missing(),
@SerialName("min_values")
override val minValues: OptionalInt = OptionalInt.Missing,
@SerialName("max_values")
Expand Down Expand Up @@ -146,6 +150,8 @@ public data class DiscordTextInputComponent(
override val components: Optional<List<DiscordComponent>> = Optional.Missing(),
override val options: Optional<List<DiscordSelectOption>> = Optional.Missing(),
override val placeholder: Optional<String> = Optional.Missing(),
@SerialName("default_values")
override val defaultValues: Optional<List<DiscordSelectDefaultValue>> = Optional.Missing(),
@SerialName("min_values")
override val minValues: OptionalInt = OptionalInt.Missing,
@SerialName("max_values")
Expand Down
22 changes: 22 additions & 0 deletions common/src/commonMain/kotlin/entity/DiscordSelectDefaultValue.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@file:Generate(
STRING_KORD_ENUM, name = "SelectDefaultValueType", unknownConstructorWasPublic = false,
docUrl = "https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-default-value-structure",
entries = [
Entry("User", stringValue = "user"),
Entry("Role", stringValue = "role"),
Entry("Channel", stringValue = "channel"),
],
)

package dev.kord.common.entity

import dev.kord.ksp.Generate
import dev.kord.ksp.Generate.EntityType.STRING_KORD_ENUM
import dev.kord.ksp.Generate.Entry
import kotlinx.serialization.Serializable

@Serializable
public data class DiscordSelectDefaultValue(
val id: Snowflake,
val type: SelectDefaultValueType,
)
Loading

0 comments on commit d537432

Please sign in to comment.