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 and HopeBaron committed Jun 24, 2021
1 parent 2ff2d22 commit c2750e1
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions rest/src/main/kotlin/builder/webhook/EditWebhookMessageBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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.common.entity.optional.mapList
import dev.kord.rest.builder.RequestBuilder
import dev.kord.rest.builder.component.ActionRowBuilder
import dev.kord.rest.builder.component.MessageComponentBuilder
Expand All @@ -25,29 +24,21 @@ class EditWebhookMessageBuilder : RequestBuilder<MultipartWebhookEditMessageRequ
private var _content: Optional<String> = Optional.Missing()
var content: String? by ::_content.delegate()

private var _embeds: Optional<MutableList<EmbedBuilder>> = Optional.Missing()
var embeds: MutableList<EmbedBuilder>? by ::_embeds.delegate()
var embeds: MutableList<EmbedBuilder> = mutableListOf()

val files: MutableList<Pair<String, InputStream>> = mutableListOf()

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

@OptIn(KordPreview::class)
private var _components: Optional<MutableList<MessageComponentBuilder>> = Optional.Missing()

@KordPreview
var components: MutableList<MessageComponentBuilder>? by ::_components.delegate()
val components: MutableList<MessageComponentBuilder> = mutableListOf()

@OptIn(ExperimentalContracts::class)
inline fun embed(builder: EmbedBuilder.() -> Unit) {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}

embeds = (embeds ?: mutableListOf()).also {
it.add(EmbedBuilder().apply(builder))
}
embeds.add(EmbedBuilder().apply(builder))
}

fun addFile(name: String, content: InputStream) {
Expand All @@ -65,17 +56,15 @@ class EditWebhookMessageBuilder : RequestBuilder<MultipartWebhookEditMessageRequ
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}

components = (components ?: mutableListOf()).also {
it.add(ActionRowBuilder().apply(builder))
}
components.add(ActionRowBuilder().apply(builder))
}

override fun toRequest(): MultipartWebhookEditMessageRequest = MultipartWebhookEditMessageRequest(
WebhookEditMessageRequest(
_content,
_embeds.mapList { it.toRequest() },
Optional.missingOnEmpty(embeds.map(EmbedBuilder::toRequest)),
_allowedMentions,
_components.mapList { it.build() }
Optional.missingOnEmpty(components.map { it.build() })
),
files
)
Expand Down

0 comments on commit c2750e1

Please sign in to comment.