Skip to content

Commit

Permalink
0.7.1 (#329)
Browse files Browse the repository at this point in the history
* Allow properties in message edits to be empty/null (#327)

* Allow properties in message edits to be empty/null

This gives properties in message edit builders the ability to encode values as null or empty lists when applicable, allowing you to remove fields from a message.

This is how it should have been from the start, but was missed in code review.

* Update CHANGELOG.md

* api dump

Co-authored-by: BartArys <[email protected]>
  • Loading branch information
HopeBaron and BartArys authored Jun 20, 2021
1 parent 96157ef commit bb9b447
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 52 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# 0.7.1

## Breaking

* `BaseInteractionResponseBuilder`
* `BaseInteractionResponseBuilder`
* `BaseInteractionResponseModifyBuilder`
* `EphemeralInteractionResponseModifyBuilder`
* `PublicInteractionResponseModifyBuilder`
* `MessageModifyBuilder`
* `EditWebhookMessageBuilder`
* `InteractionResponseModifyRequest`

## Changes

* Message-related builders have been changed to accept `null` (for non-collections) and "empty list" (for collections)
when editing a message. This makes it possible to remove fields from a message without providing a substitution.

# 0.7.0

## Additions
Expand Down
26 changes: 19 additions & 7 deletions rest/api/rest.api
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,15 @@ public abstract interface class dev/kord/rest/builder/interaction/BaseInteractio
}

public abstract interface class dev/kord/rest/builder/interaction/BaseInteractionResponseCreateBuilder : dev/kord/rest/builder/interaction/BaseInteractionResponseBuilder {
public abstract fun getComponents ()Ljava/util/List;
public abstract fun getEmbeds ()Ljava/util/List;
}

public abstract interface class dev/kord/rest/builder/interaction/BaseInteractionResponseModifyBuilder : dev/kord/rest/builder/interaction/BaseInteractionResponseBuilder {
public abstract fun getComponents ()Ljava/util/List;
public abstract fun getEmbeds ()Ljava/util/List;
public abstract fun setComponents (Ljava/util/List;)V
public abstract fun setEmbeds (Ljava/util/List;)V
}

public final class dev/kord/rest/builder/interaction/BaseKt {
Expand Down Expand Up @@ -730,7 +736,9 @@ public final class dev/kord/rest/builder/interaction/EphemeralInteractionRespons
public fun getContent ()Ljava/lang/String;
public fun getEmbeds ()Ljava/util/List;
public fun setAllowedMentions (Ldev/kord/rest/builder/message/AllowedMentionsBuilder;)V
public fun setComponents (Ljava/util/List;)V
public fun setContent (Ljava/lang/String;)V
public fun setEmbeds (Ljava/util/List;)V
public fun toRequest ()Ldev/kord/rest/json/request/MultipartInteractionResponseModifyRequest;
public synthetic fun toRequest ()Ljava/lang/Object;
}
Expand Down Expand Up @@ -839,7 +847,9 @@ public final class dev/kord/rest/builder/interaction/PublicInteractionResponseMo
public fun getEmbeds ()Ljava/util/List;
public final fun getFiles ()Ljava/util/List;
public fun setAllowedMentions (Ldev/kord/rest/builder/message/AllowedMentionsBuilder;)V
public fun setComponents (Ljava/util/List;)V
public fun setContent (Ljava/lang/String;)V
public fun setEmbeds (Ljava/util/List;)V
public fun toRequest ()Ldev/kord/rest/json/request/MultipartInteractionResponseModifyRequest;
public synthetic fun toRequest ()Ljava/lang/Object;
}
Expand Down Expand Up @@ -1088,6 +1098,7 @@ public final class dev/kord/rest/builder/message/MessageModifyBuilder : dev/kord
public final fun getEmbed ()Ldev/kord/rest/builder/message/EmbedBuilder;
public final fun getFlags ()Ldev/kord/common/entity/MessageFlags;
public final fun setAllowedMentions (Ldev/kord/rest/builder/message/AllowedMentionsBuilder;)V
public final fun setComponents (Ljava/util/List;)V
public final fun setContent (Ljava/lang/String;)V
public final fun setEmbed (Ldev/kord/rest/builder/message/EmbedBuilder;)V
public final fun setFlags (Ldev/kord/common/entity/MessageFlags;)V
Expand Down Expand Up @@ -1201,6 +1212,7 @@ public final class dev/kord/rest/builder/webhook/EditWebhookMessageBuilder : dev
public final fun getEmbeds ()Ljava/util/List;
public final fun getFiles ()Ljava/util/List;
public final fun setAllowedMentions (Ldev/kord/common/entity/AllowedMentions;)V
public final fun setComponents (Ljava/util/List;)V
public final fun setContent (Ljava/lang/String;)V
public final fun setEmbeds (Ljava/util/List;)V
public fun toRequest ()Ldev/kord/rest/json/request/MultipartWebhookEditMessageRequest;
Expand Down Expand Up @@ -2892,21 +2904,21 @@ public final class dev/kord/rest/json/request/InteractionResponseCreateRequest$C
public final class dev/kord/rest/json/request/InteractionResponseModifyRequest {
public static final field Companion Ldev/kord/rest/json/request/InteractionResponseModifyRequest$Companion;
public fun <init> ()V
public synthetic fun <init> (ILdev/kord/common/entity/optional/Optional;Ljava/util/List;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V
public fun <init> (Ldev/kord/common/entity/optional/Optional;Ljava/util/List;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)V
public synthetic fun <init> (Ldev/kord/common/entity/optional/Optional;Ljava/util/List;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (ILdev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Lkotlinx/serialization/internal/SerializationConstructorMarker;)V
public fun <init> (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)V
public synthetic fun <init> (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Ldev/kord/common/entity/optional/Optional;
public final fun component2 ()Ljava/util/List;
public final fun component2 ()Ldev/kord/common/entity/optional/Optional;
public final fun component3 ()Ldev/kord/common/entity/optional/Optional;
public final fun component4 ()Ldev/kord/common/entity/optional/Optional;
public final fun component5 ()Ldev/kord/common/entity/optional/Optional;
public final fun copy (Ldev/kord/common/entity/optional/Optional;Ljava/util/List;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)Ldev/kord/rest/json/request/InteractionResponseModifyRequest;
public static synthetic fun copy$default (Ldev/kord/rest/json/request/InteractionResponseModifyRequest;Ldev/kord/common/entity/optional/Optional;Ljava/util/List;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/Object;)Ldev/kord/rest/json/request/InteractionResponseModifyRequest;
public final fun copy (Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;)Ldev/kord/rest/json/request/InteractionResponseModifyRequest;
public static synthetic fun copy$default (Ldev/kord/rest/json/request/InteractionResponseModifyRequest;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;Ldev/kord/common/entity/optional/Optional;ILjava/lang/Object;)Ldev/kord/rest/json/request/InteractionResponseModifyRequest;
public fun equals (Ljava/lang/Object;)Z
public final fun getAllowedMentions ()Ldev/kord/common/entity/optional/Optional;
public final fun getComponents ()Ldev/kord/common/entity/optional/Optional;
public final fun getContent ()Ldev/kord/common/entity/optional/Optional;
public final fun getEmbeds ()Ljava/util/List;
public final fun getEmbeds ()Ldev/kord/common/entity/optional/Optional;
public final fun getFlags ()Ldev/kord/common/entity/optional/Optional;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
Expand Down
48 changes: 41 additions & 7 deletions rest/src/main/kotlin/builder/interaction/Base.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,38 @@ sealed interface BaseInteractionResponseBuilder<T> : RequestBuilder<T> {

var content: String?

val embeds: MutableList<EmbedBuilder>
val embeds: MutableList<EmbedBuilder>?

val components: MutableList<MessageComponentBuilder>
val components: MutableList<MessageComponentBuilder>?

var allowedMentions: AllowedMentionsBuilder?


}

@OptIn(ExperimentalContracts::class)
@KordPreview
@OptIn(ExperimentalContracts::class, KordPreview::class)
inline fun <T> BaseInteractionResponseBuilder<T>.embed(builder: EmbedBuilder.() -> Unit) {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
embeds += EmbedBuilder().apply(builder)

when(this){
is BaseInteractionResponseCreateBuilder -> {
embeds.add(EmbedBuilder().apply(builder))
}
is BaseInteractionResponseModifyBuilder -> {
embeds = (embeds ?: mutableListOf()).also {
it.add(EmbedBuilder().apply(builder))
}
}
}
}

/**
* 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.
*/
@KordPreview
@OptIn(ExperimentalContracts::class)
inline fun <T> BaseInteractionResponseBuilder<T>.allowedMentions(block: AllowedMentionsBuilder.() -> Unit = {}) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
Expand All @@ -51,14 +63,36 @@ inline fun <T> BaseInteractionResponseBuilder<T>.actionRow(builder: ActionRowBui
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}

components.add(ActionRowBuilder().apply(builder))
when (this) {
is BaseInteractionResponseCreateBuilder -> {
components.add(ActionRowBuilder().apply(builder))
}
is BaseInteractionResponseModifyBuilder -> {
components = (components ?: mutableListOf()).also {
it.add(ActionRowBuilder().apply(builder))
}
}
}

}


@KordPreview
interface BaseInteractionResponseCreateBuilder :
BaseInteractionResponseBuilder<MultipartInteractionResponseCreateRequest>
BaseInteractionResponseBuilder<MultipartInteractionResponseCreateRequest> {

override val components: MutableList<MessageComponentBuilder>

override val embeds: MutableList<EmbedBuilder>

}

@KordPreview
interface BaseInteractionResponseModifyBuilder :
BaseInteractionResponseBuilder<MultipartInteractionResponseModifyRequest>
BaseInteractionResponseBuilder<MultipartInteractionResponseModifyRequest> {

override var components: MutableList<MessageComponentBuilder>?

override var embeds: MutableList<EmbedBuilder>?

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,36 @@ import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.InteractionResponseType
import dev.kord.common.entity.MessageFlag
import dev.kord.common.entity.MessageFlags
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.*
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
import dev.kord.rest.json.request.*
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

@KordDsl
@KordPreview
class EphemeralInteractionResponseModifyBuilder : BaseInteractionResponseModifyBuilder {
private var _content: Optional<String> = Optional.Missing()
private var _content: Optional<String?> = Optional.Missing()
override var content: String? by ::_content.delegate()

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

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

override val components: MutableList<MessageComponentBuilder> = mutableListOf()
private var _components: Optional<MutableList<MessageComponentBuilder>> = Optional.Missing()
override var components: MutableList<MessageComponentBuilder>? by ::_components.delegate()

override fun toRequest(): MultipartInteractionResponseModifyRequest {
return MultipartInteractionResponseModifyRequest(
InteractionResponseModifyRequest(
content = _content,
allowedMentions = _allowedMentions.map { it.build() },
components = Optional.missingOnEmpty(components.map { it.build() }),
embeds = embeds.map { it.toRequest() }
components = _components.mapList { it.build() },
embeds = _embeds.mapList { it.toRequest() },
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package dev.kord.rest.builder.interaction
import dev.kord.common.annotation.KordDsl
import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.InteractionResponseType
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.*
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.MessageComponentBuilder
import dev.kord.rest.builder.message.AllowedMentionsBuilder
import dev.kord.rest.builder.message.EmbedBuilder
Expand Down Expand Up @@ -73,17 +70,19 @@ class PublicInteractionResponseCreateBuilder :
@KordDsl
class PublicInteractionResponseModifyBuilder :
BaseInteractionResponseModifyBuilder {
private var _content: Optional<String> = Optional.Missing()
private var _content: Optional<String?> = Optional.Missing()
override var content: String? by ::_content.delegate()

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

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

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

override val components: MutableList<MessageComponentBuilder> = mutableListOf()
private var _components: Optional<MutableList<MessageComponentBuilder>> = Optional.Missing()
override var components: MutableList<MessageComponentBuilder>? by ::_components.delegate()

fun addFile(name: String, content: InputStream) {
files += name to content
Expand All @@ -97,9 +96,9 @@ class PublicInteractionResponseModifyBuilder :
return MultipartInteractionResponseModifyRequest(
InteractionResponseModifyRequest(
content = _content,
embeds = embeds.map { it.toRequest() },
embeds = Optional(embeds).coerceToMissing().mapList { it.toRequest() },
allowedMentions = _allowedMentions.map { it.build() },
components = Optional.missingOnEmpty(components.map(MessageComponentBuilder::build))
components = Optional(components).coerceToMissing().mapList { it.build() },
),
files
)
Expand Down
13 changes: 9 additions & 4 deletions rest/src/main/kotlin/builder/message/MessageModifyBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.MessageFlags
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.common.entity.optional.mapNullable
import dev.kord.rest.builder.RequestBuilder
import dev.kord.rest.builder.component.ActionRowBuilder
Expand All @@ -29,8 +30,11 @@ class MessageModifyBuilder : RequestBuilder<MessageEditPatchRequest> {
private var _allowedMentions: Optional<AllowedMentionsBuilder?> = Optional.Missing()
var allowedMentions: AllowedMentionsBuilder? by ::_allowedMentions.delegate()

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

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

@OptIn(ExperimentalContracts::class)
inline fun embed(block: EmbedBuilder.() -> Unit) {
Expand All @@ -57,8 +61,9 @@ class MessageModifyBuilder : RequestBuilder<MessageEditPatchRequest> {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}

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

@OptIn(KordPreview::class)
Expand All @@ -67,6 +72,6 @@ class MessageModifyBuilder : RequestBuilder<MessageEditPatchRequest> {
_embed.mapNullable { it?.toRequest() },
_flags,
_allowedMentions.mapNullable { it?.build() },
Optional.missingOnEmpty(components.map(MessageComponentBuilder::build))
_components.mapList { it.build() }
)
}
Loading

0 comments on commit bb9b447

Please sign in to comment.