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 Type bugs introduced by the interaction api refactor #414

Merged
merged 7 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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: 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
23 changes: 3 additions & 20 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 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()
HopeBaron marked this conversation as resolved.
Show resolved Hide resolved
),
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)
}

}