diff --git a/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt b/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt index 8c696cb41ce8..13d674bb6361 100644 --- a/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt +++ b/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt @@ -127,8 +127,7 @@ suspend inline fun InteractionBehavior.respondPublic( @KordPreview @OptIn(ExperimentalContracts::class) suspend inline fun InteractionBehavior.respondEphemeral( - content: String, - builder: EphemeralInteractionResponseCreateBuilder.() -> Unit = {} + builder: EphemeralInteractionResponseCreateBuilder.() -> Unit ): EphemeralInteractionResponseBehavior { contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } diff --git a/rest/src/main/kotlin/builder/interaction/ApplicationCommandBuilders.kt b/rest/src/main/kotlin/builder/interaction/ApplicationCommandBuilders.kt index faabf826fab9..a97c63453236 100644 --- a/rest/src/main/kotlin/builder/interaction/ApplicationCommandBuilders.kt +++ b/rest/src/main/kotlin/builder/interaction/ApplicationCommandBuilders.kt @@ -104,7 +104,8 @@ class ApplicationCommandCreateBuilder( override fun toRequest(): ApplicationCommandCreateRequest { return ApplicationCommandCreateRequest(name, description, - _options.mapList { it.toRequest() }, _defaultPermission) + _options.mapList { it.toRequest() }, _defaultPermission + ) } @@ -147,12 +148,15 @@ class ApplicationCommandModifyBuilder : BaseApplicationBuilder(), override var options: MutableList? by ::_options.delegate() private var _defaultPermission: OptionalBoolean = OptionalBoolean.Missing - private var defaultPermission: Boolean? by ::_defaultPermission.delegate() + var defaultPermission: Boolean? by ::_defaultPermission.delegate() override fun toRequest(): ApplicationCommandModifyRequest { - return ApplicationCommandModifyRequest(_name, + return ApplicationCommandModifyRequest( + _name, _description, - _options.mapList { it.toRequest() }, _defaultPermission) + _options.mapList { it.toRequest() }, + _defaultPermission + ) } @@ -162,15 +166,15 @@ class ApplicationCommandModifyBuilder : BaseApplicationBuilder(), @KordDsl @KordPreview class ApplicationCommandPermissionsBulkModifyBuilder : - RequestBuilder> { + RequestBuilder> { @PublishedApi internal val permissions = mutableMapOf() @OptIn(ExperimentalContracts::class) inline fun command( - commandId: Snowflake, - builder: ApplicationCommandPermissionsModifyBuilder.() -> Unit, + commandId: Snowflake, + builder: ApplicationCommandPermissionsModifyBuilder.() -> Unit, ) { contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) @@ -182,7 +186,7 @@ class ApplicationCommandPermissionsBulkModifyBuilder : override fun toRequest(): List { return permissions.map { (id, builder) -> PartialDiscordGuildApplicationCommandPermissions( - id, builder.permissions.toList() + id, builder.permissions.toList() ) } } @@ -191,31 +195,31 @@ class ApplicationCommandPermissionsBulkModifyBuilder : @KordDsl @KordPreview class ApplicationCommandPermissionsModifyBuilder : - RequestBuilder { + RequestBuilder { var permissions = mutableListOf() fun role(id: Snowflake, allow: Boolean = true) { permissions.add( - DiscordGuildApplicationCommandPermission( - id, - DiscordGuildApplicationCommandPermission.Type.Role, - allow - ) + DiscordGuildApplicationCommandPermission( + id, + DiscordGuildApplicationCommandPermission.Type.Role, + allow + ) ) } fun user(id: Snowflake, allow: Boolean = true) { permissions.add( - DiscordGuildApplicationCommandPermission( - id, - DiscordGuildApplicationCommandPermission.Type.User, - allow - ) + DiscordGuildApplicationCommandPermission( + id, + DiscordGuildApplicationCommandPermission.Type.User, + allow + ) ) } override fun toRequest(): ApplicationCommandPermissionsEditRequest = - ApplicationCommandPermissionsEditRequest(permissions) + ApplicationCommandPermissionsEditRequest(permissions) } diff --git a/rest/src/main/kotlin/builder/interaction/Base.kt b/rest/src/main/kotlin/builder/interaction/Base.kt index 64845d6c68a3..803dc20a0010 100644 --- a/rest/src/main/kotlin/builder/interaction/Base.kt +++ b/rest/src/main/kotlin/builder/interaction/Base.kt @@ -2,6 +2,8 @@ package dev.kord.rest.builder.interaction import dev.kord.common.annotation.KordPreview import dev.kord.rest.builder.RequestBuilder +import dev.kord.rest.builder.component.ActionRowBuilder +import dev.kord.rest.builder.component.MessageComponentBuilder import dev.kord.rest.builder.message.AllowedMentionsBuilder import dev.kord.rest.builder.message.EmbedBuilder import dev.kord.rest.json.request.MultipartInteractionResponseCreateRequest @@ -16,6 +18,8 @@ sealed interface BaseInteractionResponseBuilder : RequestBuilder { val embeds: MutableList + val components: MutableList + var allowedMentions: AllowedMentionsBuilder? @@ -35,11 +39,26 @@ inline fun BaseInteractionResponseBuilder.embed(builder: EmbedBuilder.() @OptIn(ExperimentalContracts::class) inline fun BaseInteractionResponseBuilder.allowedMentions(block: AllowedMentionsBuilder.() -> Unit = {}) { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - if(allowedMentions == null) allowedMentions = AllowedMentionsBuilder() + if (allowedMentions == null) allowedMentions = AllowedMentionsBuilder() allowedMentions!!.apply(block) } + +@OptIn(ExperimentalContracts::class) @KordPreview -interface BaseInteractionResponseCreateBuilder : BaseInteractionResponseBuilder +inline fun BaseInteractionResponseBuilder.actionRow(builder: ActionRowBuilder.() -> Unit) { + contract { + callsInPlace(builder, InvocationKind.EXACTLY_ONCE) + } + + components.add(ActionRowBuilder().apply(builder)) +} + + +@KordPreview +interface BaseInteractionResponseCreateBuilder : + BaseInteractionResponseBuilder + @KordPreview -interface BaseInteractionResponseModifyBuilder : BaseInteractionResponseBuilder +interface BaseInteractionResponseModifyBuilder : + BaseInteractionResponseBuilder diff --git a/rest/src/main/kotlin/builder/interaction/EphemeralInteractionBuilders.kt b/rest/src/main/kotlin/builder/interaction/EphemeralInteractionBuilders.kt index beee94c55848..d89704019ff8 100644 --- a/rest/src/main/kotlin/builder/interaction/EphemeralInteractionBuilders.kt +++ b/rest/src/main/kotlin/builder/interaction/EphemeralInteractionBuilders.kt @@ -10,6 +10,7 @@ import dev.kord.common.entity.optional.delegate.delegate import dev.kord.common.entity.optional.map import dev.kord.common.entity.optional.optional import dev.kord.rest.builder.component.ActionRowBuilder +import dev.kord.rest.builder.component.ComponentBuilder import dev.kord.rest.builder.component.MessageComponentBuilder import dev.kord.rest.builder.message.AllowedMentionsBuilder import dev.kord.rest.builder.message.EmbedBuilder @@ -29,23 +30,7 @@ class EphemeralInteractionResponseModifyBuilder : BaseInteractionResponseModifyB private var _allowedMentions: Optional = Optional.Missing() override var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate() - val components: MutableList = mutableListOf() - - @OptIn(ExperimentalContracts::class) - inline fun allowedMentions(builder: AllowedMentionsBuilder.() -> Unit) { - contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } - allowedMentions = (allowedMentions ?: AllowedMentionsBuilder()).apply(builder) - } - - @OptIn(ExperimentalContracts::class) - @KordPreview - inline fun actionRow(builder: ActionRowBuilder.() -> Unit) { - contract { - callsInPlace(builder, InvocationKind.EXACTLY_ONCE) - } - - components.add(ActionRowBuilder().apply(builder)) - } + override val components: MutableList = mutableListOf() override fun toRequest(): MultipartInteractionResponseModifyRequest { return MultipartInteractionResponseModifyRequest( @@ -68,6 +53,8 @@ class EphemeralInteractionResponseCreateBuilder : BaseInteractionResponseCreateB override val embeds: MutableList = mutableListOf() + override val components: MutableList = mutableListOf() + private var _allowedMentions: Optional = Optional.Missing() override var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate() @@ -81,8 +68,10 @@ class EphemeralInteractionResponseCreateBuilder : BaseInteractionResponseCreateB val data = InteractionApplicationCommandCallbackData( content = _content, flags = flags, - embeds = Optional.missingOnEmpty(embeds.map { it.toRequest() }) + embeds = Optional.missingOnEmpty(embeds.map(EmbedBuilder::toRequest)), + components = Optional.missingOnEmpty(components.map(ComponentBuilder::build)) ) + return MultipartInteractionResponseCreateRequest( InteractionResponseCreateRequest(type, data.optional()) ) diff --git a/rest/src/main/kotlin/builder/interaction/FollowupMessageBuilders.kt b/rest/src/main/kotlin/builder/interaction/FollowupMessageBuilders.kt index b9366f26ee0d..59bd08b20c04 100644 --- a/rest/src/main/kotlin/builder/interaction/FollowupMessageBuilders.kt +++ b/rest/src/main/kotlin/builder/interaction/FollowupMessageBuilders.kt @@ -21,7 +21,7 @@ import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind import kotlin.contracts.contract - +@KordPreview sealed interface FollowupMessageBuilder : RequestBuilder { var tts: Boolean? @@ -35,9 +35,8 @@ sealed interface FollowupMessageBuilder : RequestBuilder { val content: String? } - -@OptIn(ExperimentalContracts::class) @KordPreview +@OptIn(ExperimentalContracts::class) inline fun FollowupMessageBuilder.actionRow(builder: ActionRowBuilder.() -> Unit) { contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) @@ -45,7 +44,7 @@ inline fun FollowupMessageBuilder.actionRow(builder: ActionRowBuilder.() components.add(ActionRowBuilder().apply(builder)) } - +@KordPreview @OptIn(ExperimentalContracts::class) inline fun FollowupMessageBuilder.embed(builder: EmbedBuilder.() -> Unit) { contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } @@ -57,6 +56,7 @@ inline fun FollowupMessageBuilder.embed(builder: EmbedBuilder.() -> Unit) * (ping everything), calling this function but not configuring it before the request is build will result in all * pings being ignored. */ +@KordPreview @OptIn(ExperimentalContracts::class) inline fun FollowupMessageBuilder.allowedMentions(block: AllowedMentionsBuilder.() -> Unit = {}) { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } @@ -83,7 +83,6 @@ class PublicFollowupMessageModifyBuilder : private var _allowedMentions: Optional = Optional.Missing() override var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate() - @KordPreview override val components: MutableList = mutableListOf() diff --git a/rest/src/main/kotlin/builder/interaction/PublicInteractionBuilder.kt b/rest/src/main/kotlin/builder/interaction/PublicInteractionBuilder.kt index b3d6f5910156..9d9e1d9ca6c5 100644 --- a/rest/src/main/kotlin/builder/interaction/PublicInteractionBuilder.kt +++ b/rest/src/main/kotlin/builder/interaction/PublicInteractionBuilder.kt @@ -8,7 +8,6 @@ import dev.kord.common.entity.optional.OptionalBoolean import dev.kord.common.entity.optional.delegate.delegate import dev.kord.common.entity.optional.map import dev.kord.common.entity.optional.optional -import dev.kord.rest.builder.component.ActionRowBuilder import dev.kord.rest.builder.component.MessageComponentBuilder import dev.kord.rest.builder.message.AllowedMentionsBuilder import dev.kord.rest.builder.message.EmbedBuilder @@ -18,9 +17,6 @@ import kotlinx.coroutines.withContext import java.io.InputStream import java.nio.file.Files import java.nio.file.Path -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract @KordPreview @KordDsl @@ -38,40 +34,11 @@ class PublicInteractionResponseCreateBuilder : private var _tts: OptionalBoolean = OptionalBoolean.Missing var tts: Boolean? by ::_tts.delegate() - @KordPreview - val components: MutableList = mutableListOf() + override val components: MutableList = mutableListOf() val files: MutableList> = mutableListOf() - /** - * Configures the mentions that should trigger a mention (aka ping). Not calling this function will result in the default behavior - * (ping everything), calling this function but not configuring it before the request is build will result in all - * pings being ignored. - */ - @OptIn(ExperimentalContracts::class) - inline fun allowedMentions(block: AllowedMentionsBuilder.() -> Unit = {}) { - contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - allowedMentions = (allowedMentions ?: AllowedMentionsBuilder()).apply(block) - } - - @OptIn(ExperimentalContracts::class) - inline fun embed(builder: EmbedBuilder.() -> Unit) { - contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } - if (embeds == null) embeds = mutableListOf() - embeds!! += EmbedBuilder().apply(builder) - } - - @OptIn(ExperimentalContracts::class) - @KordPreview - inline fun actionRow(builder: ActionRowBuilder.() -> Unit) { - contract { - callsInPlace(builder, InvocationKind.EXACTLY_ONCE) - } - - components.add(ActionRowBuilder().apply(builder)) - } - fun addFile(name: String, content: InputStream) { files += name to content } @@ -116,22 +83,12 @@ class PublicInteractionResponseModifyBuilder : val files: MutableList> = mutableListOf() - val components: MutableList = mutableListOf() + override val components: MutableList = mutableListOf() fun addFile(name: String, content: InputStream) { files += name to content } - @OptIn(ExperimentalContracts::class) - @KordPreview - inline fun actionRow(builder: ActionRowBuilder.() -> Unit) { - contract { - callsInPlace(builder, InvocationKind.EXACTLY_ONCE) - } - - components.add(ActionRowBuilder().apply(builder)) - } - suspend fun addFile(path: Path) = withContext(Dispatchers.IO) { addFile(path.fileName.toString(), Files.newInputStream(path)) }