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

Ephemeral files #397

Merged
merged 11 commits into from
Oct 3, 2021
2 changes: 2 additions & 0 deletions common/src/main/kotlin/entity/DiscordMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ data class DiscordAttachment(
2020-11-06 This field is marked as nullable but can be missing instead.
*/
val width: OptionalInt? = OptionalInt.Missing,

val ephemeral: OptionalBoolean = OptionalBoolean.Missing
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import dev.kord.common.entity.optional.Optional
import dev.kord.core.Kord
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.builder.message.modify.UpdateEphemeralMessageInteractionResponseCreateBuilder
import dev.kord.rest.builder.message.modify.UpdatePublicMessageInteractionResponseCreateBuilder
import dev.kord.rest.builder.message.create.UpdateMessageInteractionResponseCreateBuilder
import dev.kord.rest.json.request.InteractionApplicationCommandCallbackData
import dev.kord.rest.json.request.InteractionResponseCreateRequest
import kotlin.contracts.ExperimentalContracts
Expand Down Expand Up @@ -119,11 +118,11 @@ fun ComponentInteractionBehavior(

@OptIn(ExperimentalContracts::class)
suspend fun ComponentInteractionBehavior.acknowledgePublicUpdateMessage(
builder: UpdatePublicMessageInteractionResponseCreateBuilder.() -> Unit
builder: UpdateMessageInteractionResponseCreateBuilder.() -> Unit
): PublicInteractionResponseBehavior {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }

val request = UpdatePublicMessageInteractionResponseCreateBuilder().apply(builder).toRequest()
val request = UpdateMessageInteractionResponseCreateBuilder().apply(builder).toRequest()

kord.rest.interaction.createInteractionResponse(
id,
Expand All @@ -145,11 +144,11 @@ suspend fun ComponentInteractionBehavior.acknowledgePublicUpdateMessage(

@OptIn(ExperimentalContracts::class)
suspend fun ComponentInteractionBehavior.acknowledgeEphemeralUpdateMessage(
builder: UpdateEphemeralMessageInteractionResponseCreateBuilder.() -> Unit
builder: UpdateMessageInteractionResponseCreateBuilder.() -> Unit
): EphemeralInteractionResponseBehavior {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }

val request = UpdateEphemeralMessageInteractionResponseCreateBuilder().apply(builder).toRequest()
val request = UpdateMessageInteractionResponseCreateBuilder().apply(builder).toRequest()

kord.rest.interaction.createInteractionResponse(
id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
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.message.modify.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)
Expand All @@ -27,22 +17,6 @@ interface EphemeralFollowupMessageBehavior : FollowupMessageBehavior {
}
}

/**
* Requests to edit this followup message.
*
* @return The edited [PublicFollowupMessage] of the interaction response.
*
* @throws [RestRequestException] if something went wrong during the request.
*/

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


fun EphemeralFollowupMessageBehavior(
id: Snowflake,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
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.message.create.EphemeralFollowupMessageCreateBuilder
import dev.kord.rest.builder.message.create.PublicFollowupMessageCreateBuilder
import dev.kord.rest.builder.message.modify.EphemeralInteractionResponseModifyBuilder
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)
Expand All @@ -23,60 +10,6 @@ import kotlin.contracts.contract

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.
*/

@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)

suspend inline fun EphemeralInteractionResponseBehavior.followUpEphemeral(
builder: EphemeralFollowupMessageCreateBuilder.() -> Unit
): EphemeralFollowupMessage {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = EphemeralFollowupMessageCreateBuilder().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)

@KordUnsafe
suspend inline fun EphemeralInteractionResponseBehavior.followUpPublic(
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)
}


fun EphemeralInteractionResponseBehavior(
applicationId: Snowflake,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package dev.kord.core.behavior.interaction

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.channel.ChannelBehavior
import dev.kord.core.behavior.channel.MessageChannelBehavior
import dev.kord.core.cache.data.toData
import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.Message
import dev.kord.core.entity.Strategizable
import dev.kord.core.entity.channel.MessageChannel
import dev.kord.core.entity.interaction.EphemeralFollowupMessage
import dev.kord.core.entity.interaction.PublicFollowupMessage
import dev.kord.core.supplier.getChannelOf
import dev.kord.core.supplier.getChannelOfOrNull
import dev.kord.rest.builder.message.modify.FollowupMessageModifyBuilder
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)
Expand All @@ -26,4 +33,21 @@ interface FollowupMessageBehavior : KordEntity, Strategizable {

suspend fun getChannelOrNull(): MessageChannel? = supplier.getChannelOfOrNull(channelId)

}
}


/**
* Requests to edit this followup message.
*
* @return The edited [PublicFollowupMessage] of the interaction response.
*
* @throws [RestRequestException] if something went wrong during the request.
*/

@OptIn(ExperimentalContracts::class)
suspend inline fun FollowupMessageBehavior.edit(builder: FollowupMessageModifyBuilder.() -> Unit): EphemeralFollowupMessage {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = FollowupMessageModifyBuilder().apply(builder)
val response = kord.rest.interaction.modifyFollowupMessage(applicationId, token, id, builder.toRequest())
return EphemeralFollowupMessage(Message(response.toData(), kord), applicationId, token, kord)
}
12 changes: 5 additions & 7 deletions core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.kord.core.behavior.interaction

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.InteractionResponseType
import dev.kord.common.entity.MessageFlag
import dev.kord.common.entity.MessageFlags
Expand All @@ -16,8 +15,7 @@ import dev.kord.core.supplier.EntitySupplier
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.EphemeralInteractionResponseCreateBuilder
import dev.kord.rest.builder.message.create.PublicInteractionResponseCreateBuilder
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
Expand Down Expand Up @@ -97,12 +95,12 @@ interface InteractionBehavior : KordEntity, Strategizable {

@OptIn(ExperimentalContracts::class)
suspend inline fun InteractionBehavior.respondPublic(
builder: PublicInteractionResponseCreateBuilder.() -> Unit
builder: InteractionResponseCreateBuilder.() -> Unit
): PublicInteractionResponseBehavior {

contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }

val request = PublicInteractionResponseCreateBuilder().apply(builder).toRequest()
val request = InteractionResponseCreateBuilder().apply(builder).toRequest()
kord.rest.interaction.createInteractionResponse(id, token, request)
return PublicInteractionResponseBehavior(applicationId, token, kord)

Expand All @@ -118,11 +116,11 @@ suspend inline fun InteractionBehavior.respondPublic(

@OptIn(ExperimentalContracts::class)
suspend inline fun InteractionBehavior.respondEphemeral(
builder: EphemeralInteractionResponseCreateBuilder.() -> Unit
builder: InteractionResponseCreateBuilder.() -> Unit
): EphemeralInteractionResponseBehavior {

contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = EphemeralInteractionResponseCreateBuilder().apply(builder)
val builder = InteractionResponseCreateBuilder().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
@@ -1,8 +1,16 @@
package dev.kord.core.behavior.interaction

import dev.kord.common.annotation.KordPreview
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.PublicFollowupMessage
import dev.kord.rest.builder.message.create.FollowupMessageCreateBuilder
import dev.kord.rest.builder.message.modify.InteractionResponseModifyBuilder
import dev.kord.rest.request.RestRequestException
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* The behavior of a [Discord Interaction Response](https://discord.com/developers/docs/interactions/slash-commands#interaction-response)
Expand All @@ -14,3 +22,26 @@ interface InteractionResponseBehavior : KordObject {

}

@OptIn(ExperimentalContracts::class)
suspend inline fun InteractionResponseBehavior.followUp(ephemeral: Boolean = false, builder: FollowupMessageCreateBuilder.() -> Unit): PublicFollowupMessage {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = FollowupMessageCreateBuilder(ephemeral).apply(builder)
val message = kord.rest.interaction.createFollowupMessage(applicationId, token, builder.toRequest())
return PublicFollowupMessage(Message(message.toData(), kord), applicationId, token, kord)
}


/**
* Requests to edit this interaction response.
*
* @return The edited [Message] of the interaction response.
*
* @throws [RestRequestException] if something went wrong during the request.
*/

@OptIn(ExperimentalContracts::class)
suspend inline fun InteractionResponseBehavior.edit(builder: InteractionResponseModifyBuilder.() -> Unit) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = InteractionResponseModifyBuilder().apply(builder)
kord.rest.interaction.modifyInteractionResponse(applicationId, token, builder.toRequest())
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import dev.kord.core.entity.Message
import dev.kord.core.entity.interaction.PublicFollowupMessage
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.builder.message.modify.PublicFollowupMessageModifyBuilder
import dev.kord.rest.builder.message.modify.FollowupMessageModifyBuilder
import dev.kord.rest.request.RestRequestException
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
Expand Down Expand Up @@ -45,9 +45,9 @@ interface PublicFollowupMessageBehavior : FollowupMessageBehavior {
*/

@OptIn(ExperimentalContracts::class)
suspend inline fun PublicFollowupMessageBehavior.edit(builder: PublicFollowupMessageModifyBuilder.() -> Unit): PublicFollowupMessage {
suspend inline fun PublicFollowupMessageBehavior.edit(builder: FollowupMessageModifyBuilder.() -> Unit): PublicFollowupMessage {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = PublicFollowupMessageModifyBuilder().apply(builder)
val builder = FollowupMessageModifyBuilder().apply(builder)
val response = kord.rest.interaction.modifyFollowupMessage(applicationId, token, id, builder.toRequest())
return PublicFollowupMessage(Message(response.toData(), kord), applicationId, token, kord)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
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.Guild
import dev.kord.core.entity.Message
import dev.kord.core.entity.interaction.PublicFollowupMessage
import dev.kord.rest.builder.message.create.PublicFollowupMessageCreateBuilder
import dev.kord.rest.builder.message.modify.PublicInteractionResponseModifyBuilder
import dev.kord.rest.request.RestRequestException
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract


/**
Expand All @@ -34,37 +24,6 @@ interface PublicInteractionResponseBehavior : InteractionResponseBehavior {
}


/**
* Requests to edit this interaction response.
*
* @return The edited [Message] of the interaction response.
*
* @throws [RestRequestException] if something went wrong during the request.
*/

@OptIn(ExperimentalContracts::class)
suspend inline fun PublicInteractionResponseBehavior.edit(builder: PublicInteractionResponseModifyBuilder.() -> Unit): Message {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = PublicInteractionResponseModifyBuilder().apply(builder)
val message = kord.rest.interaction.modifyInteractionResponse(applicationId, token, builder.toRequest())
return Message(message.toData(), kord)
}

/**
* Follows-up this interaction response with a [PublicFollowupMessage]
*
* @return created [PublicFollowupMessage]
*/

@OptIn(ExperimentalContracts::class)
suspend inline fun PublicInteractionResponseBehavior.followUp(builder: PublicFollowupMessageCreateBuilder.() -> Unit): PublicFollowupMessage {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val builder = PublicFollowupMessageCreateBuilder().apply(builder)
val message = kord.rest.interaction.createFollowupMessage(applicationId, token, builder.toRequest())
return PublicFollowupMessage(Message(message.toData(), kord), applicationId, token, kord)
}


fun PublicInteractionResponseBehavior(applicationId: Snowflake, token: String, kord: Kord) =
object : PublicInteractionResponseBehavior {
override val applicationId: Snowflake
Expand Down
Loading