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

Fix components in interactions #331

Merged
merged 3 commits into from
Jun 22, 2021
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
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