Skip to content

Commit

Permalink
Fix Type bugs introduced by the interaction api refactor (#414)
Browse files Browse the repository at this point in the history
* change Int argument type to Long

* Add missing ephemeral function

* Clean up code

* refactor flags assignment

* fetch target message, not user

* clean up more code

* Fix missing ephemeral flag
  • Loading branch information
HopeBaron authored Oct 10, 2021
1 parent de12082 commit 8c683fb
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 36 deletions.
6 changes: 3 additions & 3 deletions common/src/main/kotlin/entity/Interactions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ sealed class Choice<out T> {
abstract val name: String
abstract val value: T

class IntChoice(override val name: String, override val value: Int) : Choice<Int>()
class IntChoice(override val name: String, override val value: Long) : Choice<Long>()
class NumberChoice(override val name: String, override val value: Double) : Choice<Double>()
class StringChoice(override val name: String, override val value: String) : Choice<String>()
internal class ChoiceSerializer<T>(serializer: KSerializer<T>) : KSerializer<Choice<*>> {
Expand All @@ -159,7 +159,7 @@ sealed class Choice<out T> {
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())
}
Expand All @@ -169,7 +169,7 @@ sealed class Choice<out T> {
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())
}
Expand Down
29 changes: 6 additions & 23 deletions core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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.
*/

Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/kotlin/entity/interaction/ContextInteraction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Snowflake, Message> get() = resolvedObjects!!.messages!!

Expand Down
4 changes: 2 additions & 2 deletions rest/src/main/kotlin/builder/interaction/OptionsBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ sealed class BaseChoiceBuilder<T>(

@KordDsl
class IntChoiceBuilder(name: String, description: String) :
BaseChoiceBuilder<Int>(name, description, ApplicationCommandOptionType.Integer) {
BaseChoiceBuilder<Long>(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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class InteractionResponseCreateBuilder(var ephemeral: Boolean = false)

override var allowedMentions: AllowedMentionsBuilder? = null


override val components: MutableList<MessageComponentBuilder> = mutableListOf()

override val files: MutableList<NamedFile> = mutableListOf()
Expand Down
15 changes: 14 additions & 1 deletion rest/src/main/kotlin/service/InteractionService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) }
Expand Down Expand Up @@ -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)
}

}

0 comments on commit 8c683fb

Please sign in to comment.