diff --git a/common/src/main/kotlin/entity/Interactions.kt b/common/src/main/kotlin/entity/Interactions.kt index d6256a5817a3..e671f51ad47c 100644 --- a/common/src/main/kotlin/entity/Interactions.kt +++ b/common/src/main/kotlin/entity/Interactions.kt @@ -134,7 +134,7 @@ sealed class Choice { abstract val name: String abstract val value: T - class IntChoice(override val name: String, override val value: Int) : Choice() + class IntChoice(override val name: String, override val value: Long) : Choice() class NumberChoice(override val name: String, override val value: Double) : Choice() class StringChoice(override val name: String, override val value: String) : Choice() internal class ChoiceSerializer(serializer: KSerializer) : KSerializer> { @@ -159,7 +159,7 @@ sealed class Choice { endStructure(descriptor) } return when { - value.intOrNull != null -> IntChoice(name, value.int) + value.longOrNull != null -> IntChoice(name, value.long) value.doubleOrNull != null -> NumberChoice(name, value.double) else -> StringChoice(name, value.toString()) } @@ -169,7 +169,7 @@ sealed class Choice { encoder.encodeStructure(descriptor) { encodeStringElement(descriptor, 0, value.name) when (value) { - is IntChoice -> encodeIntElement(descriptor, 1, value.value) + is IntChoice -> encodeLongElement(descriptor, 1, value.value) is NumberChoice -> encodeDoubleElement(descriptor, 1, value.value) else -> encodeStringElement(descriptor, 1, value.value.toString()) } diff --git a/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt b/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt index a90a42cc989a..b99eb06592ab 100644 --- a/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt +++ b/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt @@ -1,10 +1,6 @@ package dev.kord.core.behavior.interaction -import dev.kord.common.entity.InteractionResponseType -import dev.kord.common.entity.MessageFlag -import dev.kord.common.entity.MessageFlags import dev.kord.common.entity.Snowflake -import dev.kord.common.entity.optional.Optional import dev.kord.core.Kord import dev.kord.core.behavior.channel.MessageChannelBehavior import dev.kord.core.entity.KordEntity @@ -16,8 +12,6 @@ import dev.kord.core.supplier.EntitySupplyStrategy import dev.kord.core.supplier.getChannelOf import dev.kord.core.supplier.getChannelOfOrNull import dev.kord.rest.builder.message.create.InteractionResponseCreateBuilder -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 @@ -49,15 +43,7 @@ public interface InteractionBehavior : KordEntity, Strategizable { * @return [EphemeralInteractionResponseBehavior] Ephemeral acknowledgement of the interaction. */ public suspend fun acknowledgeEphemeral(): EphemeralInteractionResponseBehavior { - val request = InteractionResponseCreateRequest( - type = InteractionResponseType.DeferredChannelMessageWithSource, - data = Optional( - InteractionApplicationCommandCallbackData( - flags = Optional(MessageFlags(MessageFlag.Ephemeral)) - ) - ) - ) - kord.rest.interaction.createInteractionResponse(id, token, request) + kord.rest.interaction.acknowledge(id, token, true) return EphemeralInteractionResponseBehavior(applicationId, token, kord) } @@ -67,10 +53,7 @@ public interface InteractionBehavior : KordEntity, Strategizable { * @return [PublicInteractionResponseBehavior] public acknowledgement of an interaction. */ public suspend fun acknowledgePublic(): PublicInteractionResponseBehavior { - val request = InteractionResponseCreateRequest( - type = InteractionResponseType.DeferredChannelMessageWithSource - ) - kord.rest.interaction.createInteractionResponse(id, token, request) + kord.rest.interaction.acknowledge(id, token) return PublicInteractionResponseBehavior(applicationId, token, kord) } @@ -89,7 +72,7 @@ public interface InteractionBehavior : KordEntity, Strategizable { /** * Acknowledges an interaction and responds with [PublicInteractionResponseBehavior]. * - * @param builder [PublicInteractionResponseCreateBuilder] used to a create an public response. + * @param builder [InteractionResponseCreateBuilder] used to create a public response. * @return [PublicInteractionResponseBehavior] public response to the interaction. */ @@ -110,8 +93,8 @@ public suspend inline fun InteractionBehavior.respondPublic( /** * Acknowledges an interaction and responds with [EphemeralInteractionResponseBehavior] with ephemeral flag. * - * @param builder [EphemeralInteractionResponseCreateBuilder] used to a create an ephemeral response. - * @return [EphemeralInteractionResponseBehavior] ephemeral response to the interaction. + * @param builder [InteractionResponseCreateBuilder] used to a create an ephemeral response. + * @return [InteractionResponseBehavior] ephemeral response to the interaction. */ @OptIn(ExperimentalContracts::class) @@ -120,7 +103,7 @@ public suspend inline fun InteractionBehavior.respondEphemeral( ): EphemeralInteractionResponseBehavior { contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } - val builder = InteractionResponseCreateBuilder().apply(builder) + val builder = InteractionResponseCreateBuilder(true).apply(builder) val request = builder.toRequest() kord.rest.interaction.createInteractionResponse(id, token, request) return EphemeralInteractionResponseBehavior(applicationId, token, kord) diff --git a/core/src/main/kotlin/behavior/interaction/InteractionResponseBehavior.kt b/core/src/main/kotlin/behavior/interaction/InteractionResponseBehavior.kt index ce9b65aee434..ddd358ee1f4f 100644 --- a/core/src/main/kotlin/behavior/interaction/InteractionResponseBehavior.kt +++ b/core/src/main/kotlin/behavior/interaction/InteractionResponseBehavior.kt @@ -4,6 +4,7 @@ import dev.kord.common.entity.Snowflake import dev.kord.core.KordObject import dev.kord.core.cache.data.toData import dev.kord.core.entity.Message +import dev.kord.core.entity.interaction.EphemeralFollowupMessage import dev.kord.core.entity.interaction.PublicFollowupMessage import dev.kord.rest.builder.message.create.FollowupMessageCreateBuilder import dev.kord.rest.builder.message.modify.InteractionResponseModifyBuilder @@ -22,15 +23,30 @@ public interface InteractionResponseBehavior : KordObject { } +/** + * Follows up an interaction response without the [Ephemeral flag][dev.kord.common.entity.MessageFlag.Ephemeral] + */ @OptIn(ExperimentalContracts::class) -public suspend inline fun InteractionResponseBehavior.followUp(ephemeral: Boolean = false, builder: FollowupMessageCreateBuilder.() -> Unit): PublicFollowupMessage { +public suspend inline fun InteractionResponseBehavior.followUp(builder: FollowupMessageCreateBuilder.() -> Unit): PublicFollowupMessage { contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } - val builder = FollowupMessageCreateBuilder(ephemeral).apply(builder) + val builder = FollowupMessageCreateBuilder(false).apply(builder) val message = kord.rest.interaction.createFollowupMessage(applicationId, token, builder.toRequest()) return PublicFollowupMessage(Message(message.toData(), kord), applicationId, token, kord) } +/** + * Follows up an interaction response with the [Ephemeral flag][dev.kord.common.entity.MessageFlag.Ephemeral] + * + */ +@OptIn(ExperimentalContracts::class) +public suspend inline fun InteractionResponseBehavior.ephemeralFollowup(builder: FollowupMessageCreateBuilder.() -> Unit): EphemeralFollowupMessage { + contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } + val builder = FollowupMessageCreateBuilder(true).apply(builder) + val message = kord.rest.interaction.createFollowupMessage(applicationId, token, builder.toRequest()) + return EphemeralFollowupMessage(Message(message.toData(), kord), applicationId, token, kord) +} + /** * Requests to edit this interaction response. * diff --git a/core/src/main/kotlin/entity/interaction/ContextInteraction.kt b/core/src/main/kotlin/entity/interaction/ContextInteraction.kt index 0e49b4313e0f..5a02db9237e3 100644 --- a/core/src/main/kotlin/entity/interaction/ContextInteraction.kt +++ b/core/src/main/kotlin/entity/interaction/ContextInteraction.kt @@ -4,6 +4,7 @@ import dev.kord.common.entity.ApplicationCommandType import dev.kord.common.entity.Snowflake import dev.kord.common.entity.optional.unwrap import dev.kord.core.Kord +import dev.kord.core.behavior.MessageBehavior import dev.kord.core.behavior.UserBehavior import dev.kord.core.behavior.interaction.ApplicationCommandInteractionBehavior import dev.kord.core.cache.data.InteractionData @@ -136,11 +137,11 @@ public sealed interface MessageCommandInteraction : ApplicationCommandInteractio public val targetId: Snowflake get() = data.data.targetId.value!! - public val targetBehavior: UserBehavior get() = UserBehavior(targetId, kord) + public val targetBehavior: MessageBehavior get() = MessageBehavior(channelId, targetId, kord) - public suspend fun getTarget(): User = supplier.getUser(targetId) + public suspend fun getTarget(): Message = supplier.getMessage(channelId, targetId) - public suspend fun getTargetOrNull(): User? = supplier.getUserOrNull(targetId) + public suspend fun getTargetOrNull(): Message? = supplier.getMessageOrNull(channelId, targetId) public val messages: Map get() = resolvedObjects!!.messages!! diff --git a/rest/src/main/kotlin/builder/interaction/OptionsBuilder.kt b/rest/src/main/kotlin/builder/interaction/OptionsBuilder.kt index 5ee5cc74944f..77ce7f81b561 100644 --- a/rest/src/main/kotlin/builder/interaction/OptionsBuilder.kt +++ b/rest/src/main/kotlin/builder/interaction/OptionsBuilder.kt @@ -60,9 +60,9 @@ sealed class BaseChoiceBuilder( @KordDsl class IntChoiceBuilder(name: String, description: String) : - BaseChoiceBuilder(name, description, ApplicationCommandOptionType.Integer) { + BaseChoiceBuilder(name, description, ApplicationCommandOptionType.Integer) { - override fun choice(name: String, value: Int) { + override fun choice(name: String, value: Long) { if (choices == null) choices = mutableListOf() choices!!.add(Choice.IntChoice(name, value)) } diff --git a/rest/src/main/kotlin/builder/message/create/PublicFollowupMessageCreateBuilder.kt b/rest/src/main/kotlin/builder/message/create/FollowupMessageCreateBuilder.kt similarity index 94% rename from rest/src/main/kotlin/builder/message/create/PublicFollowupMessageCreateBuilder.kt rename to rest/src/main/kotlin/builder/message/create/FollowupMessageCreateBuilder.kt index 9d498be36586..5557a9d537b2 100644 --- a/rest/src/main/kotlin/builder/message/create/PublicFollowupMessageCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/message/create/FollowupMessageCreateBuilder.kt @@ -43,7 +43,7 @@ class FollowupMessageCreateBuilder(var ephemeral: Boolean) embeds = Optional(embeds).mapList { it.toRequest() }, allowedMentions = Optional(allowedMentions).coerceToMissing().map { it.build() }, components = Optional(components).coerceToMissing().mapList { it.build() }, - flags = if(ephemeral) MessageFlags(MessageFlag.Ephemeral).optional() else Optional.Missing() + flags = Optional(if(ephemeral) MessageFlags(MessageFlag.Ephemeral) else null).coerceToMissing() ), files ) diff --git a/rest/src/main/kotlin/builder/message/create/PublicInteractionCreateBuilder.kt b/rest/src/main/kotlin/builder/message/create/InteractionResponseCreateBuilder.kt similarity index 99% rename from rest/src/main/kotlin/builder/message/create/PublicInteractionCreateBuilder.kt rename to rest/src/main/kotlin/builder/message/create/InteractionResponseCreateBuilder.kt index 71318c0f86d8..9c7d260b80ab 100644 --- a/rest/src/main/kotlin/builder/message/create/PublicInteractionCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/message/create/InteractionResponseCreateBuilder.kt @@ -31,7 +31,6 @@ class InteractionResponseCreateBuilder(var ephemeral: Boolean = false) override var allowedMentions: AllowedMentionsBuilder? = null - override val components: MutableList = mutableListOf() override val files: MutableList = mutableListOf() diff --git a/rest/src/main/kotlin/builder/message/modify/PublicFollowupMessageModifyBuilder.kt b/rest/src/main/kotlin/builder/message/modify/FollowupMessageModifyBuilder.kt similarity index 100% rename from rest/src/main/kotlin/builder/message/modify/PublicFollowupMessageModifyBuilder.kt rename to rest/src/main/kotlin/builder/message/modify/FollowupMessageModifyBuilder.kt diff --git a/rest/src/main/kotlin/builder/message/modify/PublicInteractionResponseModifyBuilder.kt b/rest/src/main/kotlin/builder/message/modify/InteractionResponseModifyBuilder.kt similarity index 100% rename from rest/src/main/kotlin/builder/message/modify/PublicInteractionResponseModifyBuilder.kt rename to rest/src/main/kotlin/builder/message/modify/InteractionResponseModifyBuilder.kt diff --git a/rest/src/main/kotlin/service/InteractionService.kt b/rest/src/main/kotlin/service/InteractionService.kt index 515435d66358..678f3614bca4 100644 --- a/rest/src/main/kotlin/service/InteractionService.kt +++ b/rest/src/main/kotlin/service/InteractionService.kt @@ -1,6 +1,8 @@ package dev.kord.rest.service import dev.kord.common.entity.* +import dev.kord.common.entity.optional.Optional +import dev.kord.common.entity.optional.coerceToMissing import dev.kord.common.entity.optional.orEmpty import dev.kord.rest.builder.interaction.* import dev.kord.rest.builder.message.create.FollowupMessageCreateBuilder @@ -534,7 +536,6 @@ class InteractionService(requestHandler: RequestHandler) : RestService(requestHa applicationId: Snowflake, interactionToken: String, messageId: Snowflake, - ephemeral: Boolean = false, builder: FollowupMessageModifyBuilder.() -> Unit = {} ): DiscordMessage { contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } @@ -577,4 +578,16 @@ class InteractionService(requestHandler: RequestHandler) : RestService(requestHa ) } + public suspend fun acknowledge(interactionId: Snowflake, interactionToken: String, ephemeral: Boolean = false) { + val request = InteractionResponseCreateRequest( + type = InteractionResponseType.DeferredChannelMessageWithSource, + data = Optional( + InteractionApplicationCommandCallbackData( + flags = Optional(if(ephemeral) MessageFlags(MessageFlag.Ephemeral) else null).coerceToMissing() + ) + ) + ) + createInteractionResponse(interactionId, interactionToken, request) + } + }