From a9abe7bd3b8072655fa42769b394303e5a746763 Mon Sep 17 00:00:00 2001 From: Bart Arys Date: Fri, 18 Jun 2021 11:11:32 +0200 Subject: [PATCH] Add components to webhook builders (#324) * Add components to webhook builders This adds component fields and DSLs to webhook message builders * Fix webhook builders allowed_mentions SerialName --- .../webhook/EditWebhookMessageBuilder.kt | 19 +++++++++++++++- .../builder/webhook/ExecuteWebhookBuilder.kt | 22 +++++++++++++++++-- .../kotlin/json/request/WebhookRequests.kt | 9 ++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/rest/src/main/kotlin/builder/webhook/EditWebhookMessageBuilder.kt b/rest/src/main/kotlin/builder/webhook/EditWebhookMessageBuilder.kt index 4f1d3a8f7b2..523d7bc2096 100644 --- a/rest/src/main/kotlin/builder/webhook/EditWebhookMessageBuilder.kt +++ b/rest/src/main/kotlin/builder/webhook/EditWebhookMessageBuilder.kt @@ -1,9 +1,12 @@ package dev.kord.rest.builder.webhook +import dev.kord.common.annotation.KordPreview import dev.kord.common.entity.AllowedMentions import dev.kord.common.entity.optional.Optional import dev.kord.common.entity.optional.delegate.delegate 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.EmbedBuilder import dev.kord.rest.json.request.MultipartWebhookEditMessageRequest import dev.kord.rest.json.request.WebhookEditMessageRequest @@ -28,6 +31,8 @@ class EditWebhookMessageBuilder : RequestBuilder = Optional.Missing() var allowedMentions: AllowedMentions? by ::_allowedMentions.delegate() + val components: MutableList = mutableListOf() + @OptIn(ExperimentalContracts::class) inline fun embed(builder: EmbedBuilder.() -> Unit) { contract { @@ -44,10 +49,22 @@ class EditWebhookMessageBuilder : RequestBuilder actionRow(builder: ActionRowBuilder.() -> Unit) { + contract { + callsInPlace(builder, InvocationKind.EXACTLY_ONCE) + } + + components.add(ActionRowBuilder().apply(builder)) + } override fun toRequest(): MultipartWebhookEditMessageRequest = MultipartWebhookEditMessageRequest( WebhookEditMessageRequest( - _content, Optional.missingOnEmpty(embeds.map(EmbedBuilder::toRequest)), _allowedMentions + _content, + Optional.missingOnEmpty(embeds.map(EmbedBuilder::toRequest)), + _allowedMentions, + Optional.missingOnEmpty(components.map { it.build() }) ), files ) diff --git a/rest/src/main/kotlin/builder/webhook/ExecuteWebhookBuilder.kt b/rest/src/main/kotlin/builder/webhook/ExecuteWebhookBuilder.kt index 41db26978ba..345ef6615a5 100644 --- a/rest/src/main/kotlin/builder/webhook/ExecuteWebhookBuilder.kt +++ b/rest/src/main/kotlin/builder/webhook/ExecuteWebhookBuilder.kt @@ -1,11 +1,14 @@ package dev.kord.rest.builder.webhook import dev.kord.common.annotation.KordDsl +import dev.kord.common.annotation.KordPreview import dev.kord.common.entity.optional.Optional 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.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.EmbedRequest @@ -39,10 +42,12 @@ class ExecuteWebhookBuilder : RequestBuilder { var embeds: MutableList = mutableListOf() - private var _allowedMentions: Optional = Optional.Missing() var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate() + @OptIn(KordPreview::class) + val components: MutableList = mutableListOf() + fun addFile(name: String, content: InputStream) { files += name to content } @@ -68,6 +73,17 @@ class ExecuteWebhookBuilder : RequestBuilder { allowedMentions = (allowedMentions ?: AllowedMentionsBuilder()).apply(block) } + @OptIn(ExperimentalContracts::class) + @KordPreview + inline fun actionRow(builder: ActionRowBuilder.() -> Unit) { + contract { + callsInPlace(builder, InvocationKind.EXACTLY_ONCE) + } + + components.add(ActionRowBuilder().apply(builder)) + } + + @OptIn(KordPreview::class) override fun toRequest(): MultiPartWebhookExecuteRequest = MultiPartWebhookExecuteRequest( WebhookExecuteRequest( _content, @@ -75,7 +91,9 @@ class ExecuteWebhookBuilder : RequestBuilder { _avatarUrl, _tts, Optional.missingOnEmpty(embeds), - _allowedMentions.map { it.build() }), + _allowedMentions.map { it.build() }, + Optional.missingOnEmpty(components.map { it.build() }) + ), files ) diff --git a/rest/src/main/kotlin/json/request/WebhookRequests.kt b/rest/src/main/kotlin/json/request/WebhookRequests.kt index 618ec531885..c496342ffeb 100644 --- a/rest/src/main/kotlin/json/request/WebhookRequests.kt +++ b/rest/src/main/kotlin/json/request/WebhookRequests.kt @@ -1,6 +1,7 @@ package dev.kord.rest.json.request import dev.kord.common.entity.AllowedMentions +import dev.kord.common.entity.DiscordComponent import dev.kord.common.entity.optional.Optional import dev.kord.common.entity.optional.OptionalBoolean import dev.kord.common.entity.optional.OptionalSnowflake @@ -26,7 +27,9 @@ data class WebhookExecuteRequest( val avatar: Optional = Optional.Missing(), val tts: OptionalBoolean = OptionalBoolean.Missing, val embeds: Optional> = Optional.Missing(), - val allowedMentions: Optional = Optional.Missing() + @SerialName("allowed_mentions") + val allowedMentions: Optional = Optional.Missing(), + val components: Optional> = Optional.Missing() ) data class MultiPartWebhookExecuteRequest( @@ -38,7 +41,9 @@ data class MultiPartWebhookExecuteRequest( data class WebhookEditMessageRequest( val content: Optional = Optional.Missing(), val embeds: Optional> = Optional.Missing(), - val allowedMentions: Optional = Optional.Missing() + @SerialName("allowed_mentions") + val allowedMentions: Optional = Optional.Missing(), + val components: Optional> = Optional.Missing() ) data class MultipartWebhookEditMessageRequest(