Skip to content

Commit

Permalink
Add new select menu types (#707)
Browse files Browse the repository at this point in the history
Major API changes are:

 * renamed ComponentType.SelectMenu to ComponentType.StringSelect

 * new ComponentTypes: UserSelect, RoleSelect, MentionableSelect and
   ChannelSelect

 * SelectMenuComponent subtype for each select menu type

 * DSL function for each select menu type in ActionRowBuilder

see discord/discord-api-docs#5543 and
discord/discord-api-docs#5544

Co-authored-by: Lukellmann <lukellmann@gmail.com>
  • Loading branch information
baskerbyte and lukellmann committed Nov 14, 2022
1 parent 904964d commit 2db2d8b
Show file tree
Hide file tree
Showing 13 changed files with 457 additions and 64 deletions.
45 changes: 35 additions & 10 deletions common/api/common.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,45 @@ public sealed class ComponentType(
public object Button : ComponentType(2)

/**
* A select menu for picking from choices.
* A select menu for picking from defined text options.
*/
public object SelectMenu : ComponentType(3)
public object StringSelect : ComponentType(3)

/**
* A text input object.
*/
public object TextInput : ComponentType(4)

/**
* Select menu for users.
*/
public object UserSelect : ComponentType(5)

/**
* Select menu for roles.
*/
public object RoleSelect : ComponentType(6)

/**
* Select menu for mentionables (users and roles).
*/
public object MentionableSelect : ComponentType(7)

/**
* Select menu for channels.
*/
public object ChannelSelect : ComponentType(8)

/**
* A select menu for picking from choices.
*/
@Deprecated(
message = "Renamed by discord",
replaceWith = ReplaceWith(expression = "StringSelect", imports =
arrayOf("dev.kord.common.entity.ComponentType.StringSelect")),
)
public object SelectMenu : ComponentType(3)

internal object NewSerializer : KSerializer<ComponentType> {
public override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("dev.kord.common.entity.ComponentType", PrimitiveKind.INT)
Expand All @@ -81,8 +111,12 @@ public sealed class ComponentType(
public override fun deserialize(decoder: Decoder) = when (val value = decoder.decodeInt()) {
1 -> ActionRow
2 -> Button
3 -> SelectMenu
3 -> StringSelect
4 -> TextInput
5 -> UserSelect
6 -> RoleSelect
7 -> MentionableSelect
8 -> ChannelSelect
else -> Unknown(value)
}
}
Expand Down Expand Up @@ -111,8 +145,12 @@ public sealed class ComponentType(
listOf(
ActionRow,
Button,
SelectMenu,
StringSelect,
TextInput,
UserSelect,
RoleSelect,
MentionableSelect,
ChannelSelect,
)
}

Expand Down
25 changes: 21 additions & 4 deletions common/src/main/kotlin/entity/DiscordComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
entries = [
Entry("ActionRow", intValue = 1, kDoc = "A container for other components."),
Entry("Button", intValue = 2, kDoc = "A button object."),
Entry("SelectMenu", intValue = 3, kDoc = "A select menu for picking from choices."),
Entry("StringSelect", intValue = 3, kDoc = "A select menu for picking from defined text options."),
Entry("TextInput", intValue = 4, kDoc = "A text input object."),
Entry("UserSelect", intValue = 5, kDoc = "Select menu for users."),
Entry("RoleSelect", intValue = 6, kDoc = "Select menu for roles."),
Entry("MentionableSelect", intValue = 7, kDoc = "Select menu for mentionables (users and roles)."),
Entry("ChannelSelect", intValue = 8, kDoc = "Select menu for channels."),
],
deprecatedEntries = [
Entry("SelectMenu", intValue = 3, kDoc = "A select menu for picking from choices.",
deprecationMessage = "Renamed by discord", replaceWith = ReplaceWith("StringSelect", "dev.kord.common.entity.ComponentType.StringSelect"),
deprecationLevel = DeprecationLevel.WARNING
),
],
)

Expand Down Expand Up @@ -65,6 +75,7 @@ import kotlinx.serialization.json.*
* @property maxLength the maximum input length for a text input, min 1, max 4000.
* @property required whether this component is required to be filled, default false.
* @property value a pre-filled value for this component, max 4000 characters.
* @property channelTypes List of channel types to include in the channel select component ([ComponentType.ChannelSelect])
*/
@Serializable(with = DiscordComponent.Serializer::class)
public sealed class DiscordComponent {
Expand All @@ -88,6 +99,8 @@ public sealed class DiscordComponent {
public abstract val maxLength: OptionalInt
public abstract val required: OptionalBoolean
public abstract val value: Optional<String>
@SerialName("channel_types")
public abstract val channelTypes: Optional<List<ChannelType>>

internal object Serializer : JsonContentPolymorphicSerializer<DiscordComponent>(DiscordComponent::class) {
override fun selectDeserializer(element: JsonElement): KSerializer<out DiscordComponent> {
Expand Down Expand Up @@ -123,13 +136,15 @@ public data class DiscordChatComponent(
@SerialName("max_length")
override val maxLength: OptionalInt = OptionalInt.Missing,
override val required: OptionalBoolean = OptionalBoolean.Missing,
override val value: Optional<String> = Optional.Missing()
override val value: Optional<String> = Optional.Missing(),
@SerialName("channel_types")
override val channelTypes: Optional<List<ChannelType>> = Optional.Missing(),
) : DiscordComponent()

@Serializable
public data class DiscordTextInputComponent(
override val type: ComponentType,
public val style: Optional<TextInputStyle> = Optional.Missing(),
public val style: Optional<TextInputStyle> = Optional.Missing(),
override val label: Optional<String> = Optional.Missing(),
override val emoji: Optional<DiscordPartialEmoji> = Optional.Missing(),
@SerialName("custom_id")
Expand All @@ -148,5 +163,7 @@ public data class DiscordTextInputComponent(
@SerialName("max_length")
override val maxLength: OptionalInt = OptionalInt.Missing,
override val required: OptionalBoolean = OptionalBoolean.Missing,
override val value: Optional<String> = Optional.Missing()
override val value: Optional<String> = Optional.Missing(),
@SerialName("channel_types")
override val channelTypes: Optional<List<ChannelType>> = Optional.Missing(),
) : DiscordComponent()
2 changes: 1 addition & 1 deletion common/src/test/kotlin/json/InteractionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class InteractionTest {
applicationId shouldBe "845027738276462632"
channelId shouldBe "772908445358620702"
with(data){
componentType shouldBe ComponentType.SelectMenu
componentType shouldBe ComponentType.StringSelect
customId shouldBe "class_select_1"
values shouldBe listOf("mage", "rogue")
}
Expand Down
Loading

0 comments on commit 2db2d8b

Please sign in to comment.