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

Forum posts #709

Merged
merged 25 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions common/api/common.api

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions common/src/main/kotlin/Locale.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public data class Locale(val language: String, val country: String? = null) {
/**
* Converts this into a [JLocale].
*/
@Suppress("DEPRECATION")
luisfbl marked this conversation as resolved.
Show resolved Hide resolved
public fun asJavaLocale(): JLocale = JLocale(language, country ?: "")

public companion object {
Expand Down
1 change: 1 addition & 0 deletions common/src/main/kotlin/entity/DiscordChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public data class DiscordChannel(
val defaultSortOrder: Optional<SortOrderType?> = Optional.Missing(),
@SerialName("default_forum_layout")
val defaultForumLayout: Optional<ForumLayoutType> = Optional.Missing(),
val message: Optional<DiscordMessage> = Optional.Missing(),
)

public enum class ChannelFlag(public val code: Int) {
Expand Down
202 changes: 184 additions & 18 deletions core/api/core.api

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions core/src/main/kotlin/behavior/channel/ForumChannelBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,49 @@ import dev.kord.core.behavior.channel.threads.ThreadParentChannelBehavior
import dev.kord.core.cache.data.ChannelData
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.ForumChannel
import dev.kord.core.entity.channel.thread.ForumChannelThread
import dev.kord.rest.builder.channel.ForumChannelModifyBuilder
import dev.kord.rest.builder.channel.thread.StartForumThreadBuilder
import dev.kord.rest.service.patchForumChannel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.datetime.Instant
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

public interface ForumChannelBehavior : ThreadParentChannelBehavior {
override val activeThreads: Flow<ForumChannelThread>
get() = super.activeThreads.filterIsInstance()

override fun getPublicArchivedThreads(before: Instant?, limit: Int?): Flow<ForumChannelThread> {
return super.getPublicArchivedThreads(before, limit).filterIsInstance()
}

public suspend fun startPublicThread(
name: String,
builder: StartForumThreadBuilder.() -> Unit = {}
): ForumChannelThread {
return unsafeStartThread(name, builder)
}
}

internal suspend fun ThreadParentChannelBehavior.unsafeStartThread(
name: String,
builder: StartForumThreadBuilder.() -> Unit
): ForumChannelThread {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }

val response = kord.rest.channel.startForumThread(id, name, builder)
val data = ChannelData.from(response)

return Channel.from(data, kord) as ForumChannelThread
}

public suspend inline fun ForumChannelBehavior.edit(builder: ForumChannelModifyBuilder.() -> Unit): ForumChannel {
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }

val response = kord.rest.channel.patchForumChannel(id, builder)
val data = ChannelData.from(response)

return Channel.from(data, kord) as ForumChannel
}
33 changes: 30 additions & 3 deletions core/src/main/kotlin/behavior/channel/NewsChannelBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.builder.channel.NewsChannelModifyBuilder
import dev.kord.rest.builder.channel.thread.StartThreadWithMessageBuilder
import dev.kord.rest.builder.channel.thread.StartThreadWithoutMessageBuilder
import dev.kord.rest.json.request.ChannelFollowRequest
import dev.kord.rest.request.RestRequestException
import dev.kord.rest.service.patchNewsChannel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.datetime.Instant
import java.util.*
import java.util.Objects
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

Expand Down Expand Up @@ -87,7 +89,21 @@ public interface NewsChannelBehavior : ThreadParentChannelBehavior {
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
reason: String? = null
): NewsChannelThread {
return unsafeStartThread(name, archiveDuration, ChannelType.PublicNewsThread) { this.reason = reason } as NewsChannelThread
return startPublicThread(name) {
this.reason = reason
this.autoArchiveDuration = archiveDuration
}
}

public suspend fun startPublicThread(
name: String,
builder: StartThreadWithoutMessageBuilder.() -> Unit = {}
): NewsChannelThread {
return unsafeStartThread(name) {
builder()

type = ChannelType.PublicNewsThread
} as NewsChannelThread
}

public suspend fun startPublicThreadWithMessage(
Expand All @@ -96,7 +112,18 @@ public interface NewsChannelBehavior : ThreadParentChannelBehavior {
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
reason: String? = null
): NewsChannelThread {
return unsafeStartPublicThreadWithMessage(messageId, name, archiveDuration, reason) as NewsChannelThread
return startPublicThreadWithMessage(messageId, name) {
this.reason = reason
this.autoArchiveDuration = archiveDuration
}
}

public suspend fun startPublicThreadWithMessage(
messageId: Snowflake,
name: String,
builder: StartThreadWithMessageBuilder.() -> Unit = {}
): NewsChannelThread {
return unsafeStartPublicThreadWithMessage(messageId, name, builder) as NewsChannelThread
}


Expand Down
61 changes: 48 additions & 13 deletions core/src/main/kotlin/behavior/channel/TextChannelBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ 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.builder.channel.thread.StartThreadWithMessageBuilder
import dev.kord.rest.builder.channel.thread.StartThreadWithoutMessageBuilder
import dev.kord.rest.request.RestRequestException
import dev.kord.rest.service.patchTextChannel
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -68,32 +69,66 @@ public interface TextChannelBehavior : PrivateThreadParentChannelBehavior {
public suspend fun startPublicThread(
name: String,
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
builder: StartThreadBuilder.() -> Unit = {}
reason: String? = null,
builder: StartThreadWithoutMessageBuilder.() -> Unit = {}
): TextChannelThread {
return unsafeStartThread(
name,
archiveDuration,
ChannelType.PublicGuildThread,
builder
) as TextChannelThread
return startPublicThread(name) {
this.reason = reason
this.autoArchiveDuration = archiveDuration
builder()
}
}

public suspend fun startPublicThread(
name: String,
builder: StartThreadWithoutMessageBuilder.() -> Unit = {}
): TextChannelThread {
return unsafeStartThread(name) {
builder()

type = ChannelType.PublicGuildThread
} as TextChannelThread
}

public suspend fun startPrivateThread(
name: String,
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
builder: StartThreadBuilder.() -> Unit = {}
reason: String? = null,
builder: StartThreadWithoutMessageBuilder.() -> Unit = {}
): TextChannelThread {
return startPrivateThread(name) {
this.reason = reason
this.autoArchiveDuration = archiveDuration
builder()
}
}

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

public suspend fun startPublicThreadWithMessage(
messageId: Snowflake,
name: String,
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
reason: String? = null
): TextChannelThread {
return unsafeStartPublicThreadWithMessage(messageId, name, archiveDuration, reason) as TextChannelThread
return startPublicThreadWithMessage(messageId, name) {
this.reason = reason
}
}

public suspend fun startPublicThreadWithMessage(
messageId: Snowflake,
name: String,
builder: StartThreadWithMessageBuilder.() -> Unit = {}
): TextChannelThread {
return unsafeStartPublicThreadWithMessage(messageId, name, builder) as TextChannelThread
}

override fun getPublicArchivedThreads(before: Instant?, limit: Int?): Flow<TextChannelThread> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.kord.core.behavior.channel.threads

import dev.kord.core.cache.data.toData
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.thread.ForumChannelThread
import dev.kord.core.entity.channel.thread.ThreadChannel
import dev.kord.rest.builder.channel.thread.ForumThreadModifyBuilder
import dev.kord.rest.builder.channel.thread.ThreadModifyBuilder
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

public interface ForumChannelThreadBehavior : ThreadChannelBehavior

/**
* * Editing a thread to set [archived][ThreadChannel.isArchived] to false only requires the current user to be in the thread.
* * If [locked][ThreadChannel.isLocked] is true, then the user must have [Manage Threads][dev.kord.common.entity.Permission.ManageThreads]
* * Editing a thread to change the
* [name][ThreadModifyBuilder.name],
* [archived][ThreadModifyBuilder.archived],
* [autoArchiveDuration][ThreadModifyBuilder.autoArchiveDuration] fields
* requires [Manage Threads][dev.kord.common.entity.Permission.ManageThreads] or that the current user is the thread creator.
*/
public suspend inline fun ForumChannelThreadBehavior.edit(builder: ForumThreadModifyBuilder.() -> Unit): ForumChannelThread {
luisfbl marked this conversation as resolved.
Show resolved Hide resolved
contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) }
val appliedBuilder = ForumThreadModifyBuilder().apply(builder)
val patchedChannel = kord.rest.channel.patchThread(id, appliedBuilder.toRequest(), appliedBuilder.reason)
return Channel.from(patchedChannel.toData(), kord) as ForumChannelThread
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.kord.core.behavior.channel.threads

import dev.kord.common.entity.ArchiveDuration
import dev.kord.common.entity.ChannelType
import dev.kord.common.entity.Snowflake
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
Expand All @@ -15,8 +13,8 @@ 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 dev.kord.rest.builder.channel.thread.StartThreadWithMessageBuilder
import dev.kord.rest.builder.channel.thread.StartThreadWithoutMessageBuilder
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.datetime.Instant
Expand Down Expand Up @@ -128,12 +126,10 @@ public interface PrivateThreadParentChannelBehavior : ThreadParentChannelBehavio
*/
internal suspend fun ThreadParentChannelBehavior.unsafeStartThread(
name: String,
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
type: ChannelType,
builder: StartThreadBuilder.() -> Unit
builder: StartThreadWithoutMessageBuilder.() -> Unit
): ThreadChannel {
val response =
kord.rest.channel.startThread(id, name, archiveDuration, type, builder)
val startBuilder = StartThreadWithoutMessageBuilder(name).apply(builder)
val response = kord.rest.channel.startThread(id, startBuilder.toRequest(), startBuilder.reason)
val data = ChannelData.from(response)

return Channel.from(data, kord) as ThreadChannel
Expand All @@ -142,12 +138,10 @@ internal suspend fun ThreadParentChannelBehavior.unsafeStartThread(
internal suspend fun ThreadParentChannelBehavior.unsafeStartPublicThreadWithMessage(
messageId: Snowflake,
name: String,
archiveDuration: ArchiveDuration = ArchiveDuration.Day,
reason: String? = null
builder: StartThreadWithMessageBuilder.() -> Unit = {}
): ThreadChannel {

val response =
kord.rest.channel.startThreadWithMessage(id, messageId, StartThreadRequest(name, archiveDuration), reason)
val request = StartThreadWithMessageBuilder(name).apply(builder).toRequest()
val response = kord.rest.channel.startThreadWithMessage(id, messageId, request)
val data = ChannelData.from(response)

return Channel.from(data, kord) as ThreadChannel
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/kotlin/cache/data/ChannelData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public data class ChannelData(
val defaultSortOrder: Optional<SortOrderType?> = Optional.Missing(),
val totalMessageSent: OptionalInt = OptionalInt.Missing,
val defaultForumLayout: Optional<ForumLayoutType> = Optional.Missing(),
val availableTags: Optional<List<DiscordForumTag>> = Optional.Missing(),
val appliedTags: Optional<List<Snowflake>> = Optional.Missing(),
val defaultReactionEmoji: Optional<DiscordDefaultReaction?> = Optional.Missing(),
val defaultThreadRateLimitPerUser: Optional<DurationInSeconds> = Optional.Missing(),
val message: Optional<MessageData> = Optional.Missing()
) {


Expand Down Expand Up @@ -78,6 +83,11 @@ public data class ChannelData(
defaultSortOrder,
totalMessageSent,
defaultForumLayout,
availableTags,
appliedTags,
defaultReactionEmoji,
defaultThreadRateLimitPerUser,
message.map { MessageData.from(it) }
lukellmann marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/kotlin/entity/channel/Channel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.kord.common.entity.Snowflake
import dev.kord.core.Kord
import dev.kord.core.behavior.channel.ChannelBehavior
import dev.kord.core.cache.data.ChannelData
import dev.kord.core.entity.channel.thread.ForumChannelThread
import dev.kord.core.entity.channel.thread.NewsChannelThread
import dev.kord.core.entity.channel.thread.TextChannelThread
import dev.kord.core.entity.channel.thread.ThreadChannel
Expand Down Expand Up @@ -55,7 +56,9 @@ public interface Channel : ChannelBehavior {
GuildNews -> NewsChannel(data, kord)
GuildForum -> ForumChannel(data, kord)
PublicNewsThread -> NewsChannelThread(data, kord)
PrivateThread, PublicGuildThread -> TextChannelThread(data, kord)
PrivateThread, PublicGuildThread -> {
luisfbl marked this conversation as resolved.
Show resolved Hide resolved
TextChannelThread(data, kord)
}

GuildDirectory, is Unknown -> {
if (data.threadMetadata.value == null) Channel(data, kord, strategy.supply(kord))
Expand Down
Loading