Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SelectDefaultValues #881

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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