Skip to content

Commit

Permalink
Interaction message flags (#249)
Browse files Browse the repository at this point in the history
* acknowledgement message flags

* don't forget return types

* allow modifying flags in interaction responses

* add a overloaded acknowledgement

* update docs

* overload InteractionBehavior#acknowledge to accept list of flags

* update InteractionBehavior#respond docs and params

* introduce public and ephemeral responses

* remove unresolved classes

* inline InteractionResponseBehaviors edit functions

* Optionalize builder in ephemeral responds

* introduce PublicFollowupMessage and EphemeralFollowupMessage

* restructure interaction behaviors/builders/entities

* Fix follow up and allowedMentions builders

* Ephemeral defer + Followup returns an ephemeral followup

* Fix typing and support multipart public response creation

* Rewrite builders

* rewrite behaviors

* wait flag is always true for followups

* Add channel behavior

* Document public/ephemeral followup message and interaction responses

* Apply suggestions

Co-authored-by: Bart Arys <[email protected]>

* Fix imports

* implement Strategizable interface

* apply suggestions

Co-authored-by: Bart Arys <[email protected]>
  • Loading branch information
HopeBaron and BartArys authored Apr 15, 2021
1 parent e1f6fc7 commit 8ffbdfc
Show file tree
Hide file tree
Showing 22 changed files with 1,117 additions and 579 deletions.
1 change: 1 addition & 0 deletions core/src/main/kotlin/behavior/GuildInteractionBehavior.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.kord.core.behavior

import dev.kord.core.behavior.interaction.InteractionBehavior
import dev.kord.common.entity.Snowflake
import dev.kord.core.entity.Guild

Expand Down
100 changes: 0 additions & 100 deletions core/src/main/kotlin/behavior/InteractionBehavior.kt

This file was deleted.

59 changes: 0 additions & 59 deletions core/src/main/kotlin/behavior/InteractionFollowupBehavior.kt

This file was deleted.

57 changes: 0 additions & 57 deletions core/src/main/kotlin/behavior/InteractionResponseBehavior.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dev.kord.core.behavior.interaction

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.Snowflake
import dev.kord.core.Kord
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.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.builder.interaction.EphemeralFollowupMessageModifyBuilder
import dev.kord.rest.request.RestRequestException
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* The behavior of a [Discord Followup Message](https://discord.com/developers/docs/interactions/slash-commands#followup-messages)
* This followup message is visible to *only* to the user who made the interaction.
*/
@KordPreview
interface EphemeralFollowupMessageBehavior : FollowupMessageBehavior {

override fun withStrategy(strategy: EntitySupplyStrategy<*>): EphemeralFollowupMessageBehavior {
return EphemeralFollowupMessageBehavior(id, applicationId, token ,channelId, kord, strategy.supply(kord))
}
}

/**
* Requests to edit this followup message.
*
* @return The edited [PublicFollowupMessage] of the interaction response.
*
* @throws [RestRequestException] if something went wrong during the request.
*/
@KordPreview
@OptIn(ExperimentalContracts::class)
suspend inline fun EphemeralFollowupMessageBehavior.edit(builder: EphemeralFollowupMessageModifyBuilder.() -> Unit): EphemeralFollowupMessage {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = EphemeralFollowupMessageModifyBuilder().apply(builder)
val response = kord.rest.interaction.modifyFollowupMessage(applicationId, token, id, builder.toRequest())
return EphemeralFollowupMessage(Message(response.toData(), kord), applicationId, token, kord)
}

@KordPreview
fun EphemeralFollowupMessageBehavior(
id: Snowflake,
applicationId: Snowflake,
token: String,
channelId: Snowflake,
kord: Kord,
supplier: EntitySupplier
): EphemeralFollowupMessageBehavior = object : EphemeralFollowupMessageBehavior {
override val applicationId: Snowflake
get() = applicationId
override val token: String
get() = token
override val channelId: Snowflake
get() = channelId
override val kord: Kord
get() = kord
override val id: Snowflake
get() = id
override val supplier: EntitySupplier
get() = supplier

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package dev.kord.core.behavior.interaction

import dev.kord.common.annotation.KordPreview
import dev.kord.common.annotation.KordUnsafe
import dev.kord.common.entity.Snowflake
import dev.kord.core.Kord
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.interaction.EphemeralFollowupMessageCreateBuilder
import dev.kord.rest.builder.interaction.EphemeralInteractionResponseModifyBuilder
import dev.kord.rest.builder.interaction.PublicFollowupMessageCreateBuilder
import dev.kord.rest.request.RestRequestException
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* The behavior of a ephemeral [Discord Interaction Response](https://discord.com/developers/docs/interactions/slash-commands#interaction-response)
* This response is visible to *only* to the user who made the interaction.
*/
@KordPreview
interface EphemeralInteractionResponseBehavior : InteractionResponseBehavior

/**
* Requests to edit this interaction response.
*
* @return The edited [Message] of the interaction response.
*
* @throws [RestRequestException] if something went wrong during the request.
*/
@KordPreview
@OptIn(ExperimentalContracts::class)
suspend inline fun EphemeralInteractionResponseBehavior.edit(builder: EphemeralInteractionResponseModifyBuilder.() -> Unit) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = EphemeralInteractionResponseModifyBuilder().apply(builder)
kord.rest.interaction.modifyInteractionResponse(applicationId, token, builder.toRequest())
}

/**
* Follows-up this interaction response with a [EphemeralFollowupMessage]
*
* @return created [EphemeralFollowupMessage]
*/
@OptIn(ExperimentalContracts::class)
@KordPreview
suspend inline fun EphemeralInteractionResponseBehavior.followUp(
content: String,
builder: EphemeralFollowupMessageCreateBuilder.() -> Unit = {}
): EphemeralFollowupMessage {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = EphemeralFollowupMessageCreateBuilder(content).apply(builder)
val response = kord.rest.interaction.createFollowupMessage(applicationId, token, builder.toRequest())
val message = Message(response.toData(), kord)
return EphemeralFollowupMessage(message, applicationId, token, kord)
}


/**
* Follows-up this interaction response with a [PublicFollowupMessage]
*
* This function assumes that this interaction response has content in it.
* Use [the safe method overload][EphemeralInteractionResponseBehavior.followUp] if you are unsure
*
* @return created [PublicFollowupMessage]
*/
@OptIn(ExperimentalContracts::class)
@KordPreview
@KordUnsafe
suspend inline fun EphemeralInteractionResponseBehavior.followUp(
builder: PublicFollowupMessageCreateBuilder.() -> Unit
): PublicFollowupMessage {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = PublicFollowupMessageCreateBuilder().apply(builder)
val response = kord.rest.interaction.createFollowupMessage(applicationId, token, builder.toRequest())
val message = Message(response.toData(), kord)
return PublicFollowupMessage(message, applicationId, token, kord)
}

@KordPreview
fun EphemeralInteractionResponseBehavior(applicationId: Snowflake, token: String, kord: Kord): EphemeralInteractionResponseBehavior =
object : EphemeralInteractionResponseBehavior {
override val applicationId: Snowflake
get() = applicationId

override val token: String
get() = token

override val kord: Kord
get() = kord
}
Loading

0 comments on commit 8ffbdfc

Please sign in to comment.