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 missing components #318

Merged
merged 9 commits into from
Jun 16, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class ApplicationCommandCreateBuilder(
override fun toRequest(): ApplicationCommandCreateRequest {
return ApplicationCommandCreateRequest(name,
description,
_options.mapList { it.toRequest() }, _defaultPermission)
_options.mapList { it.toRequest() }, _defaultPermission
)

}

Expand Down Expand Up @@ -147,12 +148,15 @@ class ApplicationCommandModifyBuilder : BaseApplicationBuilder(),
override var options: MutableList<OptionsBuilder>? 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
)

}

Expand All @@ -162,15 +166,15 @@ class ApplicationCommandModifyBuilder : BaseApplicationBuilder(),
@KordDsl
@KordPreview
class ApplicationCommandPermissionsBulkModifyBuilder :
RequestBuilder<List<PartialDiscordGuildApplicationCommandPermissions>> {
RequestBuilder<List<PartialDiscordGuildApplicationCommandPermissions>> {

@PublishedApi
internal val permissions = mutableMapOf<Snowflake, ApplicationCommandPermissionsModifyBuilder>()

@OptIn(ExperimentalContracts::class)
inline fun command(
commandId: Snowflake,
builder: ApplicationCommandPermissionsModifyBuilder.() -> Unit,
commandId: Snowflake,
builder: ApplicationCommandPermissionsModifyBuilder.() -> Unit,
) {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
Expand All @@ -182,7 +186,7 @@ class ApplicationCommandPermissionsBulkModifyBuilder :
override fun toRequest(): List<PartialDiscordGuildApplicationCommandPermissions> {
return permissions.map { (id, builder) ->
PartialDiscordGuildApplicationCommandPermissions(
id, builder.permissions.toList()
id, builder.permissions.toList()
)
}
}
Expand All @@ -191,31 +195,31 @@ class ApplicationCommandPermissionsBulkModifyBuilder :
@KordDsl
@KordPreview
class ApplicationCommandPermissionsModifyBuilder :
RequestBuilder<ApplicationCommandPermissionsEditRequest> {
RequestBuilder<ApplicationCommandPermissionsEditRequest> {

var permissions = mutableListOf<DiscordGuildApplicationCommandPermission>()

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)

}
25 changes: 22 additions & 3 deletions rest/src/main/kotlin/builder/interaction/Base.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +18,8 @@ sealed interface BaseInteractionResponseBuilder<T> : RequestBuilder<T> {

val embeds: MutableList<EmbedBuilder>

val components: MutableList<MessageComponentBuilder>

var allowedMentions: AllowedMentionsBuilder?


Expand All @@ -35,11 +39,26 @@ inline fun <T> BaseInteractionResponseBuilder<T>.embed(builder: EmbedBuilder.()
@OptIn(ExperimentalContracts::class)
inline fun <T> BaseInteractionResponseBuilder<T>.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<MultipartInteractionResponseCreateRequest>
inline fun <T> BaseInteractionResponseBuilder<T>.actionRow(builder: ActionRowBuilder.() -> Unit) {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}

components.add(ActionRowBuilder().apply(builder))
}


@KordPreview
interface BaseInteractionResponseCreateBuilder :
BaseInteractionResponseBuilder<MultipartInteractionResponseCreateRequest>

@KordPreview
interface BaseInteractionResponseModifyBuilder : BaseInteractionResponseBuilder<MultipartInteractionResponseModifyRequest>
interface BaseInteractionResponseModifyBuilder :
BaseInteractionResponseBuilder<MultipartInteractionResponseModifyRequest>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,23 +30,7 @@ class EphemeralInteractionResponseModifyBuilder : BaseInteractionResponseModifyB
private var _allowedMentions: Optional<AllowedMentionsBuilder> = Optional.Missing()
override var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate()

val components: MutableList<MessageComponentBuilder> = 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<MessageComponentBuilder> = mutableListOf()

override fun toRequest(): MultipartInteractionResponseModifyRequest {
return MultipartInteractionResponseModifyRequest(
Expand All @@ -68,6 +53,8 @@ class EphemeralInteractionResponseCreateBuilder : BaseInteractionResponseCreateB

override val embeds: MutableList<EmbedBuilder> = mutableListOf()

override val components: MutableList<MessageComponentBuilder> = mutableListOf()


private var _allowedMentions: Optional<AllowedMentionsBuilder> = Optional.Missing()
override var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate()
Expand All @@ -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())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract


@KordPreview
sealed interface FollowupMessageBuilder<T> : RequestBuilder<T> {

var tts: Boolean?
Expand All @@ -35,17 +35,16 @@ sealed interface FollowupMessageBuilder<T> : RequestBuilder<T> {
val content: String?

}

@OptIn(ExperimentalContracts::class)
@KordPreview
@OptIn(ExperimentalContracts::class)
inline fun <T> FollowupMessageBuilder<T>.actionRow(builder: ActionRowBuilder.() -> Unit) {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}

components.add(ActionRowBuilder().apply(builder))
}

@KordPreview
@OptIn(ExperimentalContracts::class)
inline fun <T> FollowupMessageBuilder<T>.embed(builder: EmbedBuilder.() -> Unit) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
Expand All @@ -57,6 +56,7 @@ inline fun <T> FollowupMessageBuilder<T>.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 <T> FollowupMessageBuilder<T>.allowedMentions(block: AllowedMentionsBuilder.() -> Unit = {}) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
Expand All @@ -83,7 +83,6 @@ class PublicFollowupMessageModifyBuilder :
private var _allowedMentions: Optional<AllowedMentionsBuilder> = Optional.Missing()
override var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate()

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -38,40 +34,11 @@ class PublicInteractionResponseCreateBuilder :
private var _tts: OptionalBoolean = OptionalBoolean.Missing
var tts: Boolean? by ::_tts.delegate()

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

val files: MutableList<Pair<String, InputStream>> = 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
}
Expand Down Expand Up @@ -116,22 +83,12 @@ class PublicInteractionResponseModifyBuilder :

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

val components: MutableList<MessageComponentBuilder> = mutableListOf()
override val components: MutableList<MessageComponentBuilder> = 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))
}
Expand Down