Skip to content

Commit

Permalink
Add missing invitable field (#398)
Browse files Browse the repository at this point in the history
* Add missing `invitable` field

* Add the "invitable" flag in the ThreadModifyBuilder, ChannelData and ThreadMetadata

* Add invitable flag to the StartThreadBuilder

* Add invitable flag on the startThread methods

* Start Thread builders

* Use thread builders in the core module

* Make ChannelType val instead of var

* Use the StartThreadBuilder for the Audit Log reason instead of using a reason argument

Co-authored-by: Hope <[email protected]>
  • Loading branch information
MrPowerGamerBR and HopeBaron authored Oct 5, 2021
1 parent f6545a9 commit 7a2e86e
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 17 deletions.
3 changes: 2 additions & 1 deletion common/src/main/kotlin/entity/DiscordChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ class DiscordThreadMetadata(
val archiveTimestamp: String,
@SerialName("auto_archive_duration")
val autoArchiveDuration: ArchiveDuration,
val locked: OptionalBoolean = OptionalBoolean.Missing
val locked: OptionalBoolean = OptionalBoolean.Missing,
val invitable: OptionalBoolean = OptionalBoolean.Missing
)

@Serializable(with = ArchiveDuration.Serializer::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public interface NewsChannelBehavior : ThreadParentChannelBehavior {
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
reason: String? = null
): NewsChannelThread {
return unsafeStartThread(name, archiveDuration, ChannelType.PublicNewsThread, reason) as NewsChannelThread
return unsafeStartThread(name, archiveDuration, ChannelType.PublicNewsThread) { this.reason = reason } as NewsChannelThread
}

public suspend fun startPublicThreadWithMessage(
Expand Down
18 changes: 13 additions & 5 deletions core/src/main/kotlin/behavior/channel/TextChannelBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dev.kord.core.behavior.channel
import dev.kord.common.entity.ArchiveDuration
import dev.kord.common.entity.ChannelType
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.optional
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.behavior.channel.threads.PrivateThreadParentChannelBehavior
Expand All @@ -16,6 +18,7 @@ import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.builder.channel.TextChannelModifyBuilder
import dev.kord.rest.builder.channel.thread.StartThreadBuilder
import dev.kord.rest.request.RestRequestException
import dev.kord.rest.service.patchTextChannel
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -68,18 +71,23 @@ public interface TextChannelBehavior : PrivateThreadParentChannelBehavior {
public suspend fun startPublicThread(
name: String,
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
reason: String? = null

builder: StartThreadBuilder.() -> Unit = {}
): TextChannelThread {
return unsafeStartThread(name, archiveDuration, ChannelType.PublicGuildThread, reason) as TextChannelThread
return unsafeStartThread(
name,
archiveDuration,
ChannelType.PublicGuildThread,
builder
) as TextChannelThread
}

public suspend fun startPrivateThread(
name: String,
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
reason: String? = null
builder: StartThreadBuilder.() -> Unit = {}
): TextChannelThread {
return unsafeStartThread(name, archiveDuration, ChannelType.PrivateThread, reason) as TextChannelThread
val startBuilder = StartThreadBuilder(name, archiveDuration, ChannelType.PrivateThread).apply(builder)
return unsafeStartThread(startBuilder.name, startBuilder.autoArchiveDuration, ChannelType.PrivateThread, builder) as TextChannelThread
}

public suspend fun startPublicThreadWithMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import dev.kord.common.entity.ArchiveDuration
import dev.kord.common.entity.ChannelType
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.optional
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.behavior.channel.ChannelBehavior
Expand All @@ -16,6 +18,7 @@ import dev.kord.core.entity.channel.thread.ThreadChannel
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.builder.channel.thread.StartThreadBuilder
import dev.kord.rest.json.request.StartThreadRequest
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
Expand Down Expand Up @@ -131,11 +134,10 @@ internal suspend fun ThreadParentChannelBehavior.unsafeStartThread(
name: String,
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
type: ChannelType,
reason: String? = null
builder: StartThreadBuilder.() -> Unit
): ThreadChannel {

val response =
kord.rest.channel.startThread(id, StartThreadRequest(name, archiveDuration, Optional.Value(type)), reason)
kord.rest.channel.startThread(id, name, archiveDuration, type, builder)
val data = ChannelData.from(response)

return Channel.from(data, kord) as ThreadChannel
Expand Down
9 changes: 6 additions & 3 deletions core/src/main/kotlin/cache/data/ChannelData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ public data class ThreadMetadataData(
val archived: Boolean,
val archiveTimestamp: String,
val autoArchiveDuration: ArchiveDuration,
val locked: OptionalBoolean = OptionalBoolean.Missing
val locked: OptionalBoolean = OptionalBoolean.Missing,
val invitable: OptionalBoolean = OptionalBoolean.Missing
) {
public companion object {

public companion object {
public fun from(threadMetadata: DiscordThreadMetadata): ThreadMetadataData = with(threadMetadata) {
ThreadMetadataData(archived, archiveTimestamp, autoArchiveDuration, locked)
ThreadMetadataData(archived, archiveTimestamp, autoArchiveDuration, locked, invitable)

}
}
}
Expand Down
25 changes: 25 additions & 0 deletions rest/src/main/kotlin/builder/channel/thread/StartThreadBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.kord.rest.builder.channel.thread

import dev.kord.common.entity.ArchiveDuration
import dev.kord.common.entity.ChannelType
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.delegate.delegate
import dev.kord.common.entity.optional.optional
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.StartThreadRequest

class StartThreadBuilder(var name: String, var autoArchiveDuration: ArchiveDuration, val type: ChannelType) : AuditRequestBuilder<StartThreadRequest> {
override var reason: String? = null

private var _invitable: OptionalBoolean = OptionalBoolean.Missing
var invitable: Boolean? by ::_invitable.delegate()

override fun toRequest(): StartThreadRequest {
return StartThreadRequest(
name = name,
autoArchiveDuration = autoArchiveDuration,
type = type.optional(), // Currently this is optional, but in API v10 it will be required according to Discord's docs.
invitable = _invitable
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.kord.rest.builder.channel.thread

import dev.kord.common.entity.ArchiveDuration
import dev.kord.rest.builder.AuditRequestBuilder
import dev.kord.rest.json.request.StartThreadRequest

class StartThreadWithMessageBuilder(var name: String, var autoArchiveDuration: ArchiveDuration) : AuditRequestBuilder<StartThreadRequest> {
override var reason: String? = null

override fun toRequest(): StartThreadRequest {
return StartThreadRequest(
name = name,
autoArchiveDuration = autoArchiveDuration
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ class ThreadModifyBuilder : AuditRequestBuilder<ChannelModifyPatchRequest> {
private var _autoArchiveDuration: OptionalInt = OptionalInt.Missing
var autoArchiveDuration: Int? by ::_autoArchiveDuration.delegate()

private var _invitable: OptionalBoolean = OptionalBoolean.Missing
var invitable: Boolean? by ::_invitable.delegate()

override fun toRequest(): ChannelModifyPatchRequest {
return ChannelModifyPatchRequest(
name = _name,
locked = _locked,
archived = _archived,
autoArchiveDuration = _autoArchiveDuration,
rateLimitPerUser = _rateLimitPerUser
rateLimitPerUser = _rateLimitPerUser,
invitable = _invitable
)
}

Expand Down
4 changes: 3 additions & 1 deletion rest/src/main/kotlin/json/request/ChannelRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ data class ChannelModifyPatchRequest(
@SerialName("auto_archive_duration")
val autoArchiveDuration: OptionalInt = OptionalInt.Missing,
val locked: OptionalBoolean = OptionalBoolean.Missing,
val invitable: OptionalBoolean = OptionalBoolean.Missing,
)

@Serializable
Expand All @@ -65,7 +66,8 @@ data class StartThreadRequest(
val name: String,
@SerialName("auto_archive_duration")
val autoArchiveDuration: ArchiveDuration,
val type: Optional<ChannelType> = Optional.Missing()
val type: Optional<ChannelType> = Optional.Missing(),
val invitable: OptionalBoolean = OptionalBoolean.Missing
)

data class ListThreadsBySnowflakeRequest(
Expand Down
30 changes: 28 additions & 2 deletions rest/src/main/kotlin/service/ChannelService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.*
import dev.kord.common.entity.optional.orEmpty
import dev.kord.rest.builder.channel.*
import dev.kord.rest.builder.channel.thread.StartThreadBuilder
import dev.kord.rest.builder.channel.thread.StartThreadWithMessageBuilder
import dev.kord.rest.builder.message.create.UserMessageCreateBuilder
import dev.kord.rest.builder.message.modify.UserMessageModifyBuilder
import dev.kord.rest.json.request.*
Expand Down Expand Up @@ -274,6 +276,7 @@ class ChannelService(requestHandler: RequestHandler) : RestService(requestHandle
keys[Route.ChannelId] = channelId
body(ChannelFollowRequest.serializer(), request)
}

suspend fun startThreadWithMessage(
channelId: Snowflake,
messageId: Snowflake,
Expand All @@ -285,10 +288,21 @@ class ChannelService(requestHandler: RequestHandler) : RestService(requestHandle
keys[Route.MessageId] = messageId
body(StartThreadRequest.serializer(), request)
reason?.let { header("X-Audit-Log-Reason", reason) }

}
}

@OptIn(ExperimentalContracts::class)
suspend fun startThreadWithMessage(
channelId: Snowflake,
messageId: Snowflake,
name: String,
archiveDuration: ArchiveDuration,
builder: StartThreadWithMessageBuilder.() -> Unit
): DiscordChannel {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val startBuilder = StartThreadWithMessageBuilder(name, archiveDuration).apply(builder)
return startThreadWithMessage(channelId, messageId, startBuilder.toRequest(), startBuilder.reason)
}

suspend fun startThread(
channelId: Snowflake,
Expand All @@ -299,10 +313,22 @@ class ChannelService(requestHandler: RequestHandler) : RestService(requestHandle
keys[Route.ChannelId] = channelId
body(StartThreadRequest.serializer(), request)
reason?.let { header("X-Audit-Log-Reason", reason) }

}
}

@OptIn(ExperimentalContracts::class)
suspend fun startThread(
channelId: Snowflake,
name: String,
archiveDuration: ArchiveDuration,
type: ChannelType,
builder: StartThreadBuilder.() -> Unit = {}
): DiscordChannel {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val startBuilder = StartThreadBuilder(name, archiveDuration, type).apply(builder)
return startThread(channelId, startBuilder.toRequest(), startBuilder.reason)
}

suspend fun joinThread(channelId: Snowflake) {
call(Route.JoinThreadPut) {
keys[Route.ChannelId] = channelId
Expand Down

0 comments on commit 7a2e86e

Please sign in to comment.