Skip to content

Commit

Permalink
Unify permission overwrite API (Fix #302) (#420)
Browse files Browse the repository at this point in the history
* Unify permission overwrite API (Fix #302)

* Don't use optional in create builders

* Improve documentation of PermissionOverritesBuilder

Co-authored-by: Bart Arys <[email protected]>

* Improve documentation of PermissionOverritesBuilder

Co-authored-by: Bart Arys <[email protected]>

* Improve documentation of PermissionOverritesBuilder

Co-authored-by: Bart Arys <[email protected]>

* Make interface use a function instead of a property

* Make PermissionOverwritesBuilder sealed

Co-authored-by: Bart Arys <[email protected]>

Co-authored-by: Bart Arys <[email protected]>
  • Loading branch information
DRSchlaubi and BartArys authored Nov 3, 2021
1 parent 330d0d5 commit 07e64fb
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 94 deletions.
7 changes: 5 additions & 2 deletions common/src/main/kotlin/entity/optional/Optional.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -304,7 +307,7 @@ fun <T : Any> T.optional(): Optional.Value<T> = Optional.Value(this)

fun <T : Any?> T?.optional(): Optional<T?> = Optional(this)

fun Optional<Boolean>.toPrimitive() : OptionalBoolean = when(this){
fun Optional<Boolean>.toPrimitive(): OptionalBoolean = when (this) {
is Value -> OptionalBoolean.Value(value)
else -> OptionalBoolean.Missing
}
2 changes: 1 addition & 1 deletion core/src/samples/kotlin/PingBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ suspend fun main(args: Array<String>) {
if (message.content == "!ping") message.channel.createMessage("pong")
}

kord.login { playing("!ping to pong") }
kord.login { presence { playing("!ping to pong") } }
}
38 changes: 7 additions & 31 deletions rest/src/main/kotlin/builder/channel/CategoryCreateBuilder.kt
Original file line number Diff line number Diff line change
@@ -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<GuildChannelCreateRequest> {
class CategoryCreateBuilder(var name: String) : PermissionOverwritesCreateBuilder, AuditRequestBuilder<GuildChannelCreateRequest> {
override var reason: String? = null

private var _position: OptionalInt = OptionalInt.Missing
Expand All @@ -25,29 +20,7 @@ class CategoryCreateBuilder(var name: String) : AuditRequestBuilder<GuildChannel
private var _nsfw: OptionalBoolean = OptionalBoolean.Missing
var nsfw: Boolean? by ::_nsfw.delegate()

val permissionOverwrites: MutableList<Overwrite> = 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()
}
override var permissionOverwrites: MutableSet<Overwrite> = mutableSetOf()

override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest(
name = name,
Expand All @@ -56,4 +29,7 @@ class CategoryCreateBuilder(var name: String) : AuditRequestBuilder<GuildChannel
permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites),
type = ChannelType.GuildCategory
)

override fun addOverwrite(overwrite: Overwrite) {
}
}
41 changes: 5 additions & 36 deletions rest/src/main/kotlin/builder/channel/CategoryModifyBuilder.kt
Original file line number Diff line number Diff line change
@@ -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<ChannelModifyPatchRequest> {
class CategoryModifyBuilder : PermissionOverwritesModifyBuilder, AuditRequestBuilder<ChannelModifyPatchRequest> {

override var reason: String? = null

Expand All @@ -37,37 +32,11 @@ class CategoryModifyBuilder : AuditRequestBuilder<ChannelModifyPatchRequest> {
/**
* The permission overwrites for this category.
*/
var permissionOverwrites: MutableSet<Overwrite>? 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
)
}
}
11 changes: 6 additions & 5 deletions rest/src/main/kotlin/builder/channel/EditGuildChannelBuilder.kt
Original file line number Diff line number Diff line change
@@ -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<ChannelModifyPatchRequest> {
class TextChannelModifyBuilder : PermissionOverwritesModifyBuilder,
AuditRequestBuilder<ChannelModifyPatchRequest> {
override var reason: String? = null

private var _name: Optional<String> = Optional.Missing()
Expand All @@ -33,8 +34,8 @@ class TextChannelModifyBuilder : AuditRequestBuilder<ChannelModifyPatchRequest>
private var _rateLimitPerUser: OptionalInt? = OptionalInt.Missing
var rateLimitPerUser: Int? by ::_rateLimitPerUser.delegate()

private var _permissionOverwrites: Optional<MutableSet<Overwrite>?> = Optional.Missing()
var permissionOverwrites: MutableSet<Overwrite>? by ::_permissionOverwrites.delegate()
private var _permissionOverwrites: Optional<MutableSet<Overwrite>> = Optional.Missing()
override var permissionOverwrites: MutableSet<Overwrite>? by ::_permissionOverwrites.delegate()

override fun toRequest(): ChannelModifyPatchRequest = ChannelModifyPatchRequest(
name = _name,
Expand Down
13 changes: 7 additions & 6 deletions rest/src/main/kotlin/builder/channel/NewsChannelCreateBuilder.kt
Original file line number Diff line number Diff line change
@@ -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<GuildChannelCreateRequest> {
class NewsChannelCreateBuilder(var name: String) : PermissionOverwritesCreateBuilder,
AuditRequestBuilder<GuildChannelCreateRequest> {
override var reason: String? = null

private var _topic: Optional<String> = Optional.Missing()
Expand All @@ -28,7 +29,7 @@ class NewsChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChan
private var _position: OptionalInt = OptionalInt.Missing
var position: Int? by ::_position.delegate()

val permissionOverwrites: MutableList<Overwrite> = mutableListOf()
override var permissionOverwrites: MutableSet<Overwrite> = mutableSetOf()

override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest(
name = name,
Expand All @@ -39,4 +40,4 @@ class NewsChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChan
permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites),
type = ChannelType.GuildNews
)
}
}
Original file line number Diff line number Diff line change
@@ -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].
*/
sealed interface PermissionOverwritesBuilder {
/**
* Adds [overwrite] to this builder.
*/
fun addOverwrite(overwrite: Overwrite)
}

/**
* [PermissionOverwritesBuilder] which creates an entity with overwrites.
*/
interface PermissionOverwritesCreateBuilder : PermissionOverwritesBuilder {
var permissionOverwrites: MutableSet<Overwrite>

override fun addOverwrite(overwrite: Overwrite) {
permissionOverwrites.add(overwrite)
}
}

/**
* [PermissionOverwritesBuilder] which modifies an existing entity with overwrites.
*/
interface PermissionOverwritesModifyBuilder : PermissionOverwritesBuilder {
var permissionOverwrites: MutableSet<Overwrite>?

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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.GuildChannelCreateRequest

@KordDsl
class TextChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChannelCreateRequest> {
class TextChannelCreateBuilder(var name: String) : PermissionOverwritesCreateBuilder, AuditRequestBuilder<GuildChannelCreateRequest> {
override var reason: String? = null

private var _topic: Optional<String> = Optional.Missing()
Expand All @@ -31,7 +31,7 @@ class TextChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChan
private var _nsfw: OptionalBoolean = OptionalBoolean.Missing
var nsfw: Boolean? by ::_nsfw.delegate()

val permissionOverwrites: MutableList<Overwrite> = mutableListOf()
override var permissionOverwrites: MutableSet<Overwrite> = mutableSetOf()

override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest(
name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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.Snowflake
import dev.kord.common.entity.optional.Optional
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 VoiceChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildChannelCreateRequest> {
class VoiceChannelCreateBuilder(var name: String) :
PermissionOverwritesCreateBuilder, AuditRequestBuilder<GuildChannelCreateRequest> {
override var reason: String? = null

private var _bitrate: OptionalInt = OptionalInt.Missing
Expand All @@ -27,7 +28,7 @@ class VoiceChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildCha
private var _position: OptionalInt = OptionalInt.Missing
var position: Int? by ::_position.delegate()

val permissionOverwrites: MutableList<Overwrite> = mutableListOf()
override var permissionOverwrites: MutableSet<Overwrite> = mutableSetOf()

override fun toRequest(): GuildChannelCreateRequest = GuildChannelCreateRequest(
name = name,
Expand All @@ -38,4 +39,4 @@ class VoiceChannelCreateBuilder(var name: String) : AuditRequestBuilder<GuildCha
permissionOverwrite = Optional.missingOnEmpty(permissionOverwrites),
type = ChannelType.GuildVoice
)
}
}
Loading

0 comments on commit 07e64fb

Please sign in to comment.