Skip to content

Commit

Permalink
Fix components in interactions (#331)
Browse files Browse the repository at this point in the history
* Store components in ComponentData

* Correctly retrieve the invoked component from an interaction

* Update CHANGELOG.md
  • Loading branch information
BartArys authored Jun 22, 2021
1 parent adc89f5 commit 68f8795
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@
* `PublicFollowupMessageCreateBuilder`
* `EphemeralFollowupMessageCreateBuilder`
* `FollowupMessageModifyRequest`
* `ComponentInteraction`

## Changes

* Message-related builders have been changed to accept `null` (for non-collections) and "empty list" (for collections)
when editing a message. This makes it possible to remove fields from a message without providing a substitution.
* `FollowupMessageBuilder` no longer has the `tts` field, since it does not apply to all its subclasses.

## Fixes

* `ActionRowComponent` properly returns its children.
* `ComponentInteraction#button` is now nullable as its documentation implies.

# 0.7.0

## Additions
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/kotlin/cache/data/ComponentData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dev.kord.common.entity.DiscordComponent
import dev.kord.common.entity.DiscordPartialEmoji
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.mapList
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -23,15 +24,16 @@ data class ComponentData(

companion object {

fun from(entity: DiscordComponent) = with(entity) {
fun from(entity: DiscordComponent): ComponentData = with(entity) {
ComponentData(
type,
style,
label,
emoji,
customId,
url,
disabled
disabled,
components.mapList { from(it) }
)
}

Expand Down
20 changes: 9 additions & 11 deletions core/src/main/kotlin/entity/interaction/Interaction.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dev.kord.core.entity.interaction

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.*
import dev.kord.common.entity.CommandArgument
import dev.kord.common.entity.InteractionType
import dev.kord.common.entity.Permissions
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.optional.*
import dev.kord.core.Kord
import dev.kord.core.KordObject
Expand All @@ -11,25 +14,18 @@ import dev.kord.core.behavior.MemberBehavior
import dev.kord.core.behavior.UserBehavior
import dev.kord.core.behavior.channel.GuildMessageChannelBehavior
import dev.kord.core.behavior.interaction.ComponentInteractionBehavior
import dev.kord.core.behavior.interaction.EphemeralInteractionResponseBehavior
import dev.kord.core.behavior.interaction.InteractionBehavior
import dev.kord.core.behavior.interaction.PublicInteractionResponseBehavior
import dev.kord.core.cache.data.ApplicationInteractionData
import dev.kord.core.cache.data.InteractionData
import dev.kord.core.cache.data.ResolvedObjectsData
import dev.kord.core.entity.*
import dev.kord.core.entity.channel.DmChannel
import dev.kord.core.entity.channel.ResolvedChannel
import dev.kord.core.entity.component.ActionRowComponent
import dev.kord.core.entity.component.ButtonComponent
import dev.kord.core.entity.component.Component
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.builder.interaction.UpdateMessageInteractionResponseCreateBuilder
import dev.kord.rest.json.request.InteractionApplicationCommandCallbackData
import dev.kord.rest.json.request.InteractionResponseCreateRequest
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* An instance of [Interaction] (https://discord.com/developers/docs/interactions/slash-commands#interaction)
Expand Down Expand Up @@ -385,9 +381,11 @@ class ComponentInteraction(
*
* @see Component
*/
val component: ButtonComponent
val component: ButtonComponent?
get() = message?.components.orEmpty()
.filterIsInstance<ButtonComponent>().first { it.customId == componentId }
.filterIsInstance<ActionRowComponent>()
.flatMap { it.buttons }
.firstOrNull { it.customId == componentId }


override fun withStrategy(strategy: EntitySupplyStrategy<*>): ComponentInteraction = ComponentInteraction(
Expand Down

0 comments on commit 68f8795

Please sign in to comment.