From e71399b321fac01f36f73e7e4a473478d06d95b3 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Wed, 13 Oct 2021 03:07:15 +0200 Subject: [PATCH 1/7] Unify permission overwrite API (Fix #302) --- .../builder/channel/CategoryCreateBuilder.kt | 38 +++------------- .../builder/channel/CategoryModifyBuilder.kt | 41 +++-------------- .../channel/EditGuildChannelBuilder.kt | 9 ++-- .../channel/NewsChannelCreateBuilder.kt | 16 ++++--- .../channel/PermissionOverritesBuilder.kt | 44 +++++++++++++++++++ .../channel/TextChannelCreateBuilder.kt | 7 +-- .../channel/VoiceChannelCreateBuilder.kt | 14 +++--- .../main/kotlin/json/request/GuildRequests.kt | 24 +++++++--- 8 files changed, 99 insertions(+), 94 deletions(-) create mode 100644 rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt diff --git a/rest/src/main/kotlin/builder/channel/CategoryCreateBuilder.kt b/rest/src/main/kotlin/builder/channel/CategoryCreateBuilder.kt index 5aa735f397f9..6ebbaf504d24 100644 --- a/rest/src/main/kotlin/builder/channel/CategoryCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/CategoryCreateBuilder.kt @@ -1,22 +1,17 @@ package dev.kord.rest.builder.channel +import dev.kord.common.annotation.KordDsl import dev.kord.common.entity.ChannelType import dev.kord.common.entity.Overwrite -import dev.kord.rest.builder.AuditRequestBuilder -import dev.kord.common.annotation.KordDsl -import dev.kord.common.entity.OverwriteType -import dev.kord.common.entity.Snowflake import dev.kord.common.entity.optional.Optional import dev.kord.common.entity.optional.OptionalBoolean import dev.kord.common.entity.optional.OptionalInt import dev.kord.common.entity.optional.delegate.delegate +import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.rest.json.request.GuildChannelCreateRequest -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract @KordDsl -class CategoryCreateBuilder(var name: String) : AuditRequestBuilder { +class CategoryCreateBuilder(var name: String) : PermissionOverritesBuilder, AuditRequestBuilder { override var reason: String? = null private var _position: OptionalInt = OptionalInt.Missing @@ -25,35 +20,14 @@ class CategoryCreateBuilder(var name: String) : AuditRequestBuilder = mutableListOf() - - /** - * adds a [Overwrite] for the [memberId]. - */ - @OptIn(ExperimentalContracts::class) - inline fun addMemberOverwrite(memberId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) { - contract { - callsInPlace(builder, InvocationKind.EXACTLY_ONCE) - } - permissionOverwrites += PermissionOverwriteBuilder(OverwriteType.Member, memberId).apply(builder).toOverwrite() - } - - /** - * adds a [Overwrite] for the [roleId]. - */ - @OptIn(ExperimentalContracts::class) - inline fun addRoleOverwrite(roleId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) { - contract { - callsInPlace(builder, InvocationKind.EXACTLY_ONCE) - } - permissionOverwrites += PermissionOverwriteBuilder(OverwriteType.Role, roleId).apply(builder).toOverwrite() - } + private var _permissionOverwrites: Optional> = Optional.Missing() + override var permissionOverwrites by ::_permissionOverwrites.delegate() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name = name, position = _position, nsfw = _nsfw, - permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites), + permissionOverwrite = _permissionOverwrites, type = ChannelType.GuildCategory ) } diff --git a/rest/src/main/kotlin/builder/channel/CategoryModifyBuilder.kt b/rest/src/main/kotlin/builder/channel/CategoryModifyBuilder.kt index f59ef97c872d..fe1011cb1510 100644 --- a/rest/src/main/kotlin/builder/channel/CategoryModifyBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/CategoryModifyBuilder.kt @@ -1,20 +1,15 @@ package dev.kord.rest.builder.channel -import dev.kord.common.entity.Overwrite -import dev.kord.common.entity.Snowflake -import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.common.annotation.KordDsl -import dev.kord.common.entity.OverwriteType +import dev.kord.common.entity.Overwrite import dev.kord.common.entity.optional.Optional import dev.kord.common.entity.optional.OptionalInt import dev.kord.common.entity.optional.delegate.delegate +import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.rest.json.request.ChannelModifyPatchRequest -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract @KordDsl -class CategoryModifyBuilder : AuditRequestBuilder { +class CategoryModifyBuilder : PermissionOverritesBuilder, AuditRequestBuilder { override var reason: String? = null @@ -37,37 +32,11 @@ class CategoryModifyBuilder : AuditRequestBuilder { /** * The permission overwrites for this category. */ - var permissionOverwrites: MutableSet? by ::_permissionOverwrites.delegate() - - /** - * adds a [Overwrite] for the [memberId]. - */ - @OptIn(ExperimentalContracts::class) - inline fun addMemberOverwrite(memberId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) { - contract { - callsInPlace(builder, InvocationKind.EXACTLY_ONCE) - } - val overwrite = permissionOverwrites ?: mutableSetOf() - overwrite.add(PermissionOverwriteBuilder(OverwriteType.Member, memberId).apply(builder).toOverwrite()) - permissionOverwrites = overwrite - } - - /** - * adds a [Overwrite] for the [roleId]. - */ - @OptIn(ExperimentalContracts::class) - inline fun addRoleOverwrite(roleId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) { - contract { - callsInPlace(builder, InvocationKind.EXACTLY_ONCE) - } - val overwrite = permissionOverwrites ?: mutableSetOf() - overwrite.add(PermissionOverwriteBuilder(OverwriteType.Role, roleId).apply(builder).toOverwrite()) - permissionOverwrites = overwrite - } + override var permissionOverwrites by ::_permissionOverwrites.delegate() override fun toRequest(): ChannelModifyPatchRequest = ChannelModifyPatchRequest( name = _name, position = _position, permissionOverwrites = _permissionOverwrites ) -} \ No newline at end of file +} diff --git a/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt b/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt index 9d30e3329d17..1a3c51938bd0 100644 --- a/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt @@ -1,18 +1,19 @@ package dev.kord.rest.builder.channel +import dev.kord.common.annotation.KordDsl import dev.kord.common.entity.Overwrite import dev.kord.common.entity.Snowflake -import dev.kord.rest.builder.AuditRequestBuilder -import dev.kord.common.annotation.KordDsl import dev.kord.common.entity.optional.Optional import dev.kord.common.entity.optional.OptionalBoolean import dev.kord.common.entity.optional.OptionalInt import dev.kord.common.entity.optional.OptionalSnowflake import dev.kord.common.entity.optional.delegate.delegate +import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.rest.json.request.ChannelModifyPatchRequest @KordDsl -class TextChannelModifyBuilder : AuditRequestBuilder { +class TextChannelModifyBuilder : PermissionOverritesBuilder, + AuditRequestBuilder { override var reason: String? = null private var _name: Optional = Optional.Missing() @@ -34,7 +35,7 @@ class TextChannelModifyBuilder : AuditRequestBuilder var rateLimitPerUser: Int? by ::_rateLimitPerUser.delegate() private var _permissionOverwrites: Optional?> = Optional.Missing() - var permissionOverwrites: MutableSet? by ::_permissionOverwrites.delegate() + override var permissionOverwrites: MutableSet? by ::_permissionOverwrites.delegate() override fun toRequest(): ChannelModifyPatchRequest = ChannelModifyPatchRequest( name = _name, diff --git a/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt b/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt index 55cf353badf3..f53592db92cd 100644 --- a/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt @@ -1,19 +1,20 @@ package dev.kord.rest.builder.channel -import dev.kord.common.entity.Overwrite -import dev.kord.common.entity.Snowflake -import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.common.annotation.KordDsl import dev.kord.common.entity.ChannelType +import dev.kord.common.entity.Overwrite +import dev.kord.common.entity.Snowflake import dev.kord.common.entity.optional.Optional import dev.kord.common.entity.optional.OptionalBoolean import dev.kord.common.entity.optional.OptionalInt import dev.kord.common.entity.optional.OptionalSnowflake import dev.kord.common.entity.optional.delegate.delegate +import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.rest.json.request.GuildChannelCreateRequest @KordDsl -class NewsChannelCreateBuilder(var name: String) : AuditRequestBuilder { +class NewsChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, + AuditRequestBuilder { override var reason: String? = null private var _topic: Optional = Optional.Missing() @@ -28,7 +29,8 @@ class NewsChannelCreateBuilder(var name: String) : AuditRequestBuilder = mutableListOf() + private var _permissionOverwrites: Optional> = Optional.Missing() + override var permissionOverwrites by ::_permissionOverwrites.delegate() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name = name, @@ -36,7 +38,7 @@ class NewsChannelCreateBuilder(var name: String) : AuditRequestBuilder? +} + +/** + * adds a [Overwrite] for the [memberId]. + */ +@OptIn(ExperimentalContracts::class) +inline fun PermissionOverritesBuilder.addMemberOverwrite(memberId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) { + contract { + callsInPlace(builder, InvocationKind.EXACTLY_ONCE) + } + val overwrite = permissionOverwrites ?: mutableSetOf() + overwrite.add(PermissionOverwriteBuilder(OverwriteType.Member, memberId).apply(builder).toOverwrite()) + permissionOverwrites = overwrite +} + +/** + * adds a [Overwrite] for the [roleId]. + */ +@OptIn(ExperimentalContracts::class) +inline fun PermissionOverritesBuilder.addRoleOverwrite(roleId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) { + contract { + callsInPlace(builder, InvocationKind.EXACTLY_ONCE) + } + val overwrite = permissionOverwrites ?: mutableSetOf() + overwrite.add(PermissionOverwriteBuilder(OverwriteType.Role, roleId).apply(builder).toOverwrite()) + permissionOverwrites = overwrite +} diff --git a/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt b/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt index 41d1093a38cf..935ef1936dd6 100644 --- a/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt @@ -13,7 +13,7 @@ import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.rest.json.request.GuildChannelCreateRequest @KordDsl -class TextChannelCreateBuilder(var name: String) : AuditRequestBuilder { +class TextChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, AuditRequestBuilder { override var reason: String? = null private var _topic: Optional = Optional.Missing() @@ -31,7 +31,8 @@ class TextChannelCreateBuilder(var name: String) : AuditRequestBuilder = mutableListOf() + private var _permissionOverwrites: Optional> = Optional.Missing() + override var permissionOverwrites by ::_permissionOverwrites.delegate() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name, @@ -41,6 +42,6 @@ class TextChannelCreateBuilder(var name: String) : AuditRequestBuilder { +class VoiceChannelCreateBuilder(var name: String) : + PermissionOverritesBuilder, AuditRequestBuilder { override var reason: String? = null private var _bitrate: OptionalInt = OptionalInt.Missing @@ -27,7 +28,8 @@ class VoiceChannelCreateBuilder(var name: String) : AuditRequestBuilder = mutableListOf() + private var _permissionOverwrites: Optional> = Optional.Missing() + override var permissionOverwrites by ::_permissionOverwrites.delegate() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name = name, @@ -35,7 +37,7 @@ class VoiceChannelCreateBuilder(var name: String) : AuditRequestBuilder = Optional.Missing(), val position: OptionalInt = OptionalInt.Missing, @SerialName("permission_overwrites") - val permissionOverwrite: Optional> = Optional.Missing(), + val permissionOverwrite: Optional> = Optional.Missing(), @SerialName("parent_id") val parentId: OptionalSnowflake = OptionalSnowflake.Missing, val nsfw: OptionalBoolean = OptionalBoolean.Missing, @@ -237,4 +249,4 @@ data class GuildWelcomeScreenModifyRequest( val enabled: OptionalBoolean = OptionalBoolean.Missing, val welcomeScreenChannels: Optional> = Optional.Missing(), val description: Optional = Optional.Missing() -) \ No newline at end of file +) From 9fdabf54fcc406d928ca2f4247c68f9ff43201b5 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 18 Oct 2021 04:43:43 +0200 Subject: [PATCH 2/7] Don't use optional in create builders --- common/src/main/kotlin/entity/optional/Optional.kt | 10 ++++++++-- .../kotlin/builder/channel/EditGuildChannelBuilder.kt | 5 ++--- .../kotlin/builder/channel/NewsChannelCreateBuilder.kt | 5 ++--- .../kotlin/builder/channel/TextChannelCreateBuilder.kt | 5 ++--- .../builder/channel/VoiceChannelCreateBuilder.kt | 5 ++--- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/common/src/main/kotlin/entity/optional/Optional.kt b/common/src/main/kotlin/entity/optional/Optional.kt index ad49d7847012..3b0a8c43823e 100644 --- a/common/src/main/kotlin/entity/optional/Optional.kt +++ b/common/src/main/kotlin/entity/optional/Optional.kt @@ -1,7 +1,10 @@ package dev.kord.common.entity.optional import dev.kord.common.entity.Snowflake -import dev.kord.common.entity.optional.Optional.* +import dev.kord.common.entity.optional.Optional.Missing +import dev.kord.common.entity.optional.Optional.Null +import dev.kord.common.entity.optional.Optional.OptionalSerializer +import dev.kord.common.entity.optional.Optional.Value import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable @@ -128,6 +131,9 @@ sealed class Optional { if (value.isEmpty()) Missing() else Value(value) + fun > missingOnEmptyOrOnNull(value: C?): Optional = + if (value == null) Missing() else missingOnEmpty(value) + /** * Returns a [Missing] optional of type [T]. */ @@ -304,7 +310,7 @@ fun T.optional(): Optional.Value = Optional.Value(this) fun T?.optional(): Optional = Optional(this) -fun Optional.toPrimitive() : OptionalBoolean = when(this){ +fun Optional.toPrimitive(): OptionalBoolean = when (this) { is Value -> OptionalBoolean.Value(value) else -> OptionalBoolean.Missing } diff --git a/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt b/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt index 1a3c51938bd0..95b45bc35701 100644 --- a/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt @@ -34,8 +34,7 @@ class TextChannelModifyBuilder : PermissionOverritesBuilder, private var _rateLimitPerUser: OptionalInt? = OptionalInt.Missing var rateLimitPerUser: Int? by ::_rateLimitPerUser.delegate() - private var _permissionOverwrites: Optional?> = Optional.Missing() - override var permissionOverwrites: MutableSet? by ::_permissionOverwrites.delegate() + override var permissionOverwrites: MutableSet? = mutableSetOf() override fun toRequest(): ChannelModifyPatchRequest = ChannelModifyPatchRequest( name = _name, @@ -43,7 +42,7 @@ class TextChannelModifyBuilder : PermissionOverritesBuilder, topic = _topic, nsfw = _nsfw, rateLimitPerUser = _rateLimitPerUser, - permissionOverwrites = _permissionOverwrites, + permissionOverwrites = Optional.missingOnEmptyOrOnNull(permissionOverwrites), parentId = _parentId ) diff --git a/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt b/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt index f53592db92cd..f3b7469612f8 100644 --- a/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt @@ -29,8 +29,7 @@ class NewsChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, private var _position: OptionalInt = OptionalInt.Missing var position: Int? by ::_position.delegate() - private var _permissionOverwrites: Optional> = Optional.Missing() - override var permissionOverwrites by ::_permissionOverwrites.delegate() + override var permissionOverwrites: MutableSet? = mutableSetOf() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name = name, @@ -38,7 +37,7 @@ class NewsChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, nsfw = _nsfw, parentId = _parentId, position = _position, - permissionOverwrite = _permissionOverwrites, + permissionOverwrite = Optional.missingOnEmptyOrOnNull(permissionOverwrites), type = ChannelType.GuildNews ) } diff --git a/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt b/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt index 935ef1936dd6..2173c81baefa 100644 --- a/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt @@ -31,8 +31,7 @@ class TextChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, A private var _nsfw: OptionalBoolean = OptionalBoolean.Missing var nsfw: Boolean? by ::_nsfw.delegate() - private var _permissionOverwrites: Optional> = Optional.Missing() - override var permissionOverwrites by ::_permissionOverwrites.delegate() + override var permissionOverwrites: MutableSet? = mutableSetOf() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name, @@ -42,6 +41,6 @@ class TextChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, A _position, parentId = _parentId, nsfw = _nsfw, - permissionOverwrite = _permissionOverwrites, + permissionOverwrite = Optional.missingOnEmptyOrOnNull(permissionOverwrites), ) } diff --git a/rest/src/main/kotlin/builder/channel/VoiceChannelCreateBuilder.kt b/rest/src/main/kotlin/builder/channel/VoiceChannelCreateBuilder.kt index 5d1904f5561f..675f156a11d3 100644 --- a/rest/src/main/kotlin/builder/channel/VoiceChannelCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/VoiceChannelCreateBuilder.kt @@ -28,8 +28,7 @@ class VoiceChannelCreateBuilder(var name: String) : private var _position: OptionalInt = OptionalInt.Missing var position: Int? by ::_position.delegate() - private var _permissionOverwrites: Optional> = Optional.Missing() - override var permissionOverwrites by ::_permissionOverwrites.delegate() + override var permissionOverwrites: MutableSet? = mutableSetOf() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name = name, @@ -37,7 +36,7 @@ class VoiceChannelCreateBuilder(var name: String) : userLimit = _userLimit, parentId = _parentId, position = _position, - permissionOverwrite = _permissionOverwrites, + permissionOverwrite = Optional.missingOnEmptyOrOnNull(permissionOverwrites), type = ChannelType.GuildVoice ) } From 4fd7b124bc41d9f9bf799e704565c46061af754e Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 1 Nov 2021 15:29:54 +0100 Subject: [PATCH 3/7] Improve documentation of PermissionOverritesBuilder Co-authored-by: Bart Arys --- .../main/kotlin/builder/channel/PermissionOverritesBuilder.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt b/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt index 3e8ca20e52ed..b4bb6d998951 100644 --- a/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt @@ -8,7 +8,7 @@ import kotlin.contracts.InvocationKind import kotlin.contracts.contract /** - * Interface containing shared behavior on all builders modifying permission overwrites. + * Builder that can modify [permission overwrites][Overwrite]. */ interface PermissionOverritesBuilder { /** From e32c8ba45d8220529e19eb0922c8971b026b08e6 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 1 Nov 2021 15:30:21 +0100 Subject: [PATCH 4/7] Improve documentation of PermissionOverritesBuilder Co-authored-by: Bart Arys --- .../main/kotlin/builder/channel/PermissionOverritesBuilder.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt b/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt index b4bb6d998951..597fda6bd791 100644 --- a/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt @@ -18,7 +18,7 @@ interface PermissionOverritesBuilder { } /** - * adds a [Overwrite] for the [memberId]. + * Adds an [Overwrite] for the [memberId]. */ @OptIn(ExperimentalContracts::class) inline fun PermissionOverritesBuilder.addMemberOverwrite(memberId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) { From 08f007cec9a4ec0990ddb91c74e1afe7597ca84e Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 1 Nov 2021 15:30:27 +0100 Subject: [PATCH 5/7] Improve documentation of PermissionOverritesBuilder Co-authored-by: Bart Arys --- .../main/kotlin/builder/channel/PermissionOverritesBuilder.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt b/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt index 597fda6bd791..cda01110fbf3 100644 --- a/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt @@ -31,7 +31,7 @@ inline fun PermissionOverritesBuilder.addMemberOverwrite(memberId: Snowflake, bu } /** - * adds a [Overwrite] for the [roleId]. + * Adds an [Overwrite] for the [roleId]. */ @OptIn(ExperimentalContracts::class) inline fun PermissionOverritesBuilder.addRoleOverwrite(roleId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) { From 7fd89a42a2842afc7541a86c643e0b349240917f Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 1 Nov 2021 15:50:14 +0100 Subject: [PATCH 6/7] Make interface use a function instead of a property --- .../main/kotlin/entity/optional/Optional.kt | 3 - core/src/samples/kotlin/PingBot.kt | 2 +- .../builder/channel/CategoryCreateBuilder.kt | 10 ++- .../builder/channel/CategoryModifyBuilder.kt | 2 +- .../channel/EditGuildChannelBuilder.kt | 7 +- .../channel/NewsChannelCreateBuilder.kt | 6 +- .../channel/PermissionOverritesBuilder.kt | 44 ----------- .../channel/PermissionOverwritesBuilder.kt | 75 +++++++++++++++++++ .../channel/TextChannelCreateBuilder.kt | 6 +- .../channel/VoiceChannelCreateBuilder.kt | 6 +- 10 files changed, 96 insertions(+), 65 deletions(-) delete mode 100644 rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt create mode 100644 rest/src/main/kotlin/builder/channel/PermissionOverwritesBuilder.kt diff --git a/common/src/main/kotlin/entity/optional/Optional.kt b/common/src/main/kotlin/entity/optional/Optional.kt index 3b0a8c43823e..ac4894b1f9f7 100644 --- a/common/src/main/kotlin/entity/optional/Optional.kt +++ b/common/src/main/kotlin/entity/optional/Optional.kt @@ -131,9 +131,6 @@ sealed class Optional { if (value.isEmpty()) Missing() else Value(value) - fun > missingOnEmptyOrOnNull(value: C?): Optional = - if (value == null) Missing() else missingOnEmpty(value) - /** * Returns a [Missing] optional of type [T]. */ diff --git a/core/src/samples/kotlin/PingBot.kt b/core/src/samples/kotlin/PingBot.kt index 048abf4e1b50..588a3d287316 100644 --- a/core/src/samples/kotlin/PingBot.kt +++ b/core/src/samples/kotlin/PingBot.kt @@ -10,5 +10,5 @@ suspend fun main(args: Array) { if (message.content == "!ping") message.channel.createMessage("pong") } - kord.login { playing("!ping to pong") } + kord.login { presence { playing("!ping to pong") } } } diff --git a/rest/src/main/kotlin/builder/channel/CategoryCreateBuilder.kt b/rest/src/main/kotlin/builder/channel/CategoryCreateBuilder.kt index 6ebbaf504d24..76308cd0b4b7 100644 --- a/rest/src/main/kotlin/builder/channel/CategoryCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/CategoryCreateBuilder.kt @@ -11,7 +11,7 @@ import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.rest.json.request.GuildChannelCreateRequest @KordDsl -class CategoryCreateBuilder(var name: String) : PermissionOverritesBuilder, AuditRequestBuilder { +class CategoryCreateBuilder(var name: String) : PermissionOverwritesCreateBuilder, AuditRequestBuilder { override var reason: String? = null private var _position: OptionalInt = OptionalInt.Missing @@ -20,14 +20,16 @@ class CategoryCreateBuilder(var name: String) : PermissionOverritesBuilder, Audi private var _nsfw: OptionalBoolean = OptionalBoolean.Missing var nsfw: Boolean? by ::_nsfw.delegate() - private var _permissionOverwrites: Optional> = Optional.Missing() - override var permissionOverwrites by ::_permissionOverwrites.delegate() + override var permissionOverwrites: MutableSet = mutableSetOf() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name = name, position = _position, nsfw = _nsfw, - permissionOverwrite = _permissionOverwrites, + permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites), type = ChannelType.GuildCategory ) + + override fun addOverwrite(overwrite: Overwrite) { + } } diff --git a/rest/src/main/kotlin/builder/channel/CategoryModifyBuilder.kt b/rest/src/main/kotlin/builder/channel/CategoryModifyBuilder.kt index fe1011cb1510..3d4369332b68 100644 --- a/rest/src/main/kotlin/builder/channel/CategoryModifyBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/CategoryModifyBuilder.kt @@ -9,7 +9,7 @@ import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.rest.json.request.ChannelModifyPatchRequest @KordDsl -class CategoryModifyBuilder : PermissionOverritesBuilder, AuditRequestBuilder { +class CategoryModifyBuilder : PermissionOverwritesModifyBuilder, AuditRequestBuilder { override var reason: String? = null diff --git a/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt b/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt index 95b45bc35701..342ade69e88e 100644 --- a/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt @@ -12,7 +12,7 @@ import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.rest.json.request.ChannelModifyPatchRequest @KordDsl -class TextChannelModifyBuilder : PermissionOverritesBuilder, +class TextChannelModifyBuilder : PermissionOverwritesModifyBuilder, AuditRequestBuilder { override var reason: String? = null @@ -34,7 +34,8 @@ class TextChannelModifyBuilder : PermissionOverritesBuilder, private var _rateLimitPerUser: OptionalInt? = OptionalInt.Missing var rateLimitPerUser: Int? by ::_rateLimitPerUser.delegate() - override var permissionOverwrites: MutableSet? = mutableSetOf() + private var _permissionOverwrites: Optional> = Optional.Missing() + override var permissionOverwrites: MutableSet? by ::_permissionOverwrites.delegate() override fun toRequest(): ChannelModifyPatchRequest = ChannelModifyPatchRequest( name = _name, @@ -42,7 +43,7 @@ class TextChannelModifyBuilder : PermissionOverritesBuilder, topic = _topic, nsfw = _nsfw, rateLimitPerUser = _rateLimitPerUser, - permissionOverwrites = Optional.missingOnEmptyOrOnNull(permissionOverwrites), + permissionOverwrites = _permissionOverwrites, parentId = _parentId ) diff --git a/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt b/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt index f3b7469612f8..f6a5bc05834a 100644 --- a/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt @@ -13,7 +13,7 @@ import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.rest.json.request.GuildChannelCreateRequest @KordDsl -class NewsChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, +class NewsChannelCreateBuilder(var name: String) : PermissionOverwritesCreateBuilder, AuditRequestBuilder { override var reason: String? = null @@ -29,7 +29,7 @@ class NewsChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, private var _position: OptionalInt = OptionalInt.Missing var position: Int? by ::_position.delegate() - override var permissionOverwrites: MutableSet? = mutableSetOf() + override var permissionOverwrites: MutableSet = mutableSetOf() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name = name, @@ -37,7 +37,7 @@ class NewsChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, nsfw = _nsfw, parentId = _parentId, position = _position, - permissionOverwrite = Optional.missingOnEmptyOrOnNull(permissionOverwrites), + permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites), type = ChannelType.GuildNews ) } diff --git a/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt b/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt deleted file mode 100644 index cda01110fbf3..000000000000 --- a/rest/src/main/kotlin/builder/channel/PermissionOverritesBuilder.kt +++ /dev/null @@ -1,44 +0,0 @@ -package dev.kord.rest.builder.channel - -import dev.kord.common.entity.Overwrite -import dev.kord.common.entity.OverwriteType -import dev.kord.common.entity.Snowflake -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract - -/** - * Builder that can modify [permission overwrites][Overwrite]. - */ -interface PermissionOverritesBuilder { - /** - * The permission overwrites for this category. - */ - var permissionOverwrites: MutableSet? -} - -/** - * Adds an [Overwrite] for the [memberId]. - */ -@OptIn(ExperimentalContracts::class) -inline fun PermissionOverritesBuilder.addMemberOverwrite(memberId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) { - contract { - callsInPlace(builder, InvocationKind.EXACTLY_ONCE) - } - val overwrite = permissionOverwrites ?: mutableSetOf() - overwrite.add(PermissionOverwriteBuilder(OverwriteType.Member, memberId).apply(builder).toOverwrite()) - permissionOverwrites = overwrite -} - -/** - * Adds an [Overwrite] for the [roleId]. - */ -@OptIn(ExperimentalContracts::class) -inline fun PermissionOverritesBuilder.addRoleOverwrite(roleId: Snowflake, builder: PermissionOverwriteBuilder.() -> Unit) { - contract { - callsInPlace(builder, InvocationKind.EXACTLY_ONCE) - } - val overwrite = permissionOverwrites ?: mutableSetOf() - overwrite.add(PermissionOverwriteBuilder(OverwriteType.Role, roleId).apply(builder).toOverwrite()) - permissionOverwrites = overwrite -} diff --git a/rest/src/main/kotlin/builder/channel/PermissionOverwritesBuilder.kt b/rest/src/main/kotlin/builder/channel/PermissionOverwritesBuilder.kt new file mode 100644 index 000000000000..a15983b3a499 --- /dev/null +++ b/rest/src/main/kotlin/builder/channel/PermissionOverwritesBuilder.kt @@ -0,0 +1,75 @@ +package dev.kord.rest.builder.channel + +import dev.kord.common.entity.Overwrite +import dev.kord.common.entity.OverwriteType +import dev.kord.common.entity.Snowflake +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract + +/** + * Builder that can modify [permission overwrites][Overwrite]. + */ +interface PermissionOverwritesBuilder { + /** + * Adds [overwrite] to this builder. + */ + fun addOverwrite(overwrite: Overwrite) +} + +/** + * [PermissionOverwritesBuilder] which creates an entity with overwrites. + */ +interface PermissionOverwritesCreateBuilder : PermissionOverwritesBuilder { + var permissionOverwrites: MutableSet + + override fun addOverwrite(overwrite: Overwrite) { + permissionOverwrites.add(overwrite) + } +} + +/** + * [PermissionOverwritesBuilder] which modifies an existing entity with overwrites. + */ +interface PermissionOverwritesModifyBuilder : PermissionOverwritesBuilder { + var permissionOverwrites: MutableSet? + + override fun addOverwrite(overwrite: Overwrite) { + val overwrites = permissionOverwrites ?: mutableSetOf() + overwrites.add(overwrite) + + permissionOverwrites = overwrites + } +} + +/** + * Adds an [Overwrite] for the [memberId]. + */ +@OptIn(ExperimentalContracts::class) +inline fun PermissionOverwritesBuilder.addMemberOverwrite( + memberId: Snowflake, + builder: PermissionOverwriteBuilder.() -> Unit +) { + contract { + callsInPlace(builder, InvocationKind.EXACTLY_ONCE) + } + val overwrite = PermissionOverwriteBuilder(OverwriteType.Member, memberId).apply(builder).toOverwrite() + + return addOverwrite(overwrite) +} + +/** + * Adds an [Overwrite] for the [roleId]. + */ +@OptIn(ExperimentalContracts::class) +inline fun PermissionOverwritesBuilder.addRoleOverwrite( + roleId: Snowflake, + builder: PermissionOverwriteBuilder.() -> Unit +) { + contract { + callsInPlace(builder, InvocationKind.EXACTLY_ONCE) + } + val overwrite = PermissionOverwriteBuilder(OverwriteType.Member, roleId).apply(builder).toOverwrite() + + return addOverwrite(overwrite) +} diff --git a/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt b/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt index 2173c81baefa..3ab6fb9a9621 100644 --- a/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/TextChannelCreateBuilder.kt @@ -13,7 +13,7 @@ import dev.kord.rest.builder.AuditRequestBuilder import dev.kord.rest.json.request.GuildChannelCreateRequest @KordDsl -class TextChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, AuditRequestBuilder { +class TextChannelCreateBuilder(var name: String) : PermissionOverwritesCreateBuilder, AuditRequestBuilder { override var reason: String? = null private var _topic: Optional = Optional.Missing() @@ -31,7 +31,7 @@ class TextChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, A private var _nsfw: OptionalBoolean = OptionalBoolean.Missing var nsfw: Boolean? by ::_nsfw.delegate() - override var permissionOverwrites: MutableSet? = mutableSetOf() + override var permissionOverwrites: MutableSet = mutableSetOf() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name, @@ -41,6 +41,6 @@ class TextChannelCreateBuilder(var name: String) : PermissionOverritesBuilder, A _position, parentId = _parentId, nsfw = _nsfw, - permissionOverwrite = Optional.missingOnEmptyOrOnNull(permissionOverwrites), + permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites), ) } diff --git a/rest/src/main/kotlin/builder/channel/VoiceChannelCreateBuilder.kt b/rest/src/main/kotlin/builder/channel/VoiceChannelCreateBuilder.kt index 675f156a11d3..f4b6c54794ef 100644 --- a/rest/src/main/kotlin/builder/channel/VoiceChannelCreateBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/VoiceChannelCreateBuilder.kt @@ -13,7 +13,7 @@ import dev.kord.rest.json.request.GuildChannelCreateRequest @KordDsl class VoiceChannelCreateBuilder(var name: String) : - PermissionOverritesBuilder, AuditRequestBuilder { + PermissionOverwritesCreateBuilder, AuditRequestBuilder { override var reason: String? = null private var _bitrate: OptionalInt = OptionalInt.Missing @@ -28,7 +28,7 @@ class VoiceChannelCreateBuilder(var name: String) : private var _position: OptionalInt = OptionalInt.Missing var position: Int? by ::_position.delegate() - override var permissionOverwrites: MutableSet? = mutableSetOf() + override var permissionOverwrites: MutableSet = mutableSetOf() override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest( name = name, @@ -36,7 +36,7 @@ class VoiceChannelCreateBuilder(var name: String) : userLimit = _userLimit, parentId = _parentId, position = _position, - permissionOverwrite = Optional.missingOnEmptyOrOnNull(permissionOverwrites), + permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites), type = ChannelType.GuildVoice ) } From 6d47ce1c76bbef2bc76e8bb6800443b7e3f645c2 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 1 Nov 2021 17:31:15 +0100 Subject: [PATCH 7/7] Make PermissionOverwritesBuilder sealed Co-authored-by: Bart Arys --- .../main/kotlin/builder/channel/PermissionOverwritesBuilder.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest/src/main/kotlin/builder/channel/PermissionOverwritesBuilder.kt b/rest/src/main/kotlin/builder/channel/PermissionOverwritesBuilder.kt index a15983b3a499..1f839b72de9c 100644 --- a/rest/src/main/kotlin/builder/channel/PermissionOverwritesBuilder.kt +++ b/rest/src/main/kotlin/builder/channel/PermissionOverwritesBuilder.kt @@ -10,7 +10,7 @@ import kotlin.contracts.contract /** * Builder that can modify [permission overwrites][Overwrite]. */ -interface PermissionOverwritesBuilder { +sealed interface PermissionOverwritesBuilder { /** * Adds [overwrite] to this builder. */