Skip to content

Commit

Permalink
Add components to webhook builders (#324)
Browse files Browse the repository at this point in the history
* Add components to webhook builders

This adds component fields and DSLs to webhook message builders

* Fix webhook builders allowed_mentions SerialName
  • Loading branch information
BartArys authored Jun 18, 2021
1 parent bc357a4 commit a9abe7b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -28,6 +31,8 @@ class EditWebhookMessageBuilder : RequestBuilder<MultipartWebhookEditMessageRequ
private var _allowedMentions: Optional<AllowedMentions> = Optional.Missing()
var allowedMentions: AllowedMentions? by ::_allowedMentions.delegate()

val components: MutableList<MessageComponentBuilder> = mutableListOf()

@OptIn(ExperimentalContracts::class)
inline fun embed(builder: EmbedBuilder.() -> Unit) {
contract {
Expand All @@ -44,10 +49,22 @@ class EditWebhookMessageBuilder : RequestBuilder<MultipartWebhookEditMessageRequ
addFile(path.fileName.toString(), Files.newInputStream(path))
}

@OptIn(ExperimentalContracts::class)
@KordPreview
inline fun <T> 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
)
Expand Down
22 changes: 20 additions & 2 deletions rest/src/main/kotlin/builder/webhook/ExecuteWebhookBuilder.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,10 +42,12 @@ class ExecuteWebhookBuilder : RequestBuilder<MultiPartWebhookExecuteRequest> {

var embeds: MutableList<EmbedRequest> = mutableListOf()


private var _allowedMentions: Optional<AllowedMentionsBuilder> = Optional.Missing()
var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate()

@OptIn(KordPreview::class)
val components: MutableList<MessageComponentBuilder> = mutableListOf()

fun addFile(name: String, content: InputStream) {
files += name to content
}
Expand All @@ -68,14 +73,27 @@ class ExecuteWebhookBuilder : RequestBuilder<MultiPartWebhookExecuteRequest> {
allowedMentions = (allowedMentions ?: AllowedMentionsBuilder()).apply(block)
}

@OptIn(ExperimentalContracts::class)
@KordPreview
inline fun <T> 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,
_username,
_avatarUrl,
_tts,
Optional.missingOnEmpty(embeds),
_allowedMentions.map { it.build() }),
_allowedMentions.map { it.build() },
Optional.missingOnEmpty(components.map { it.build() })
),
files
)

Expand Down
9 changes: 7 additions & 2 deletions rest/src/main/kotlin/json/request/WebhookRequests.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,7 +27,9 @@ data class WebhookExecuteRequest(
val avatar: Optional<String> = Optional.Missing(),
val tts: OptionalBoolean = OptionalBoolean.Missing,
val embeds: Optional<List<EmbedRequest>> = Optional.Missing(),
val allowedMentions: Optional<AllowedMentions> = Optional.Missing()
@SerialName("allowed_mentions")
val allowedMentions: Optional<AllowedMentions> = Optional.Missing(),
val components: Optional<List<DiscordComponent>> = Optional.Missing()
)

data class MultiPartWebhookExecuteRequest(
Expand All @@ -38,7 +41,9 @@ data class MultiPartWebhookExecuteRequest(
data class WebhookEditMessageRequest(
val content: Optional<String> = Optional.Missing(),
val embeds: Optional<List<EmbedRequest>> = Optional.Missing(),
val allowedMentions: Optional<AllowedMentions> = Optional.Missing()
@SerialName("allowed_mentions")
val allowedMentions: Optional<AllowedMentions> = Optional.Missing(),
val components: Optional<List<DiscordComponent>> = Optional.Missing()
)

data class MultipartWebhookEditMessageRequest(
Expand Down

0 comments on commit a9abe7b

Please sign in to comment.