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

Add components to webhook builders #324

Merged
merged 2 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 5 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,8 @@ 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()
val allowedMentions: Optional<AllowedMentions> = Optional.Missing(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have a allowed_mentions serial name here

val components: Optional<List<DiscordComponent>> = Optional.Missing()
)

data class MultiPartWebhookExecuteRequest(
Expand All @@ -38,7 +40,8 @@ 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()
val allowedMentions: Optional<AllowedMentions> = Optional.Missing(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

val components: Optional<List<DiscordComponent>> = Optional.Missing()
)

data class MultipartWebhookEditMessageRequest(
Expand Down