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

Top channels #353

Merged
merged 12 commits into from
Aug 2, 2021
12 changes: 8 additions & 4 deletions core/src/main/kotlin/Unsafe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ class Unsafe(private val kord: Kord) {
fun messageChannel(id: Snowflake): MessageChannelBehavior =
MessageChannelBehavior(id, kord)

fun guildChannel(guildId: Snowflake, id: Snowflake): TopGuildChannelBehavior =
fun topGuildChannel(guildId: Snowflake, id: Snowflake): TopGuildChannelBehavior =
TopGuildChannelBehavior(guildId = guildId, id = id, kord = kord)

fun guildMessageChannel(guildId: Snowflake, id: Snowflake): TopGuildMessageChannelBehavior =
fun topGuildMessageChannel(guildId: Snowflake, id: Snowflake): TopGuildMessageChannelBehavior =
TopGuildMessageChannelBehavior(guildId = guildId, id = id, kord = kord)

fun guildChannel(guildId: Snowflake, id: Snowflake): GuildChannelBehavior =
GuildChannelBehavior(guildId, id, kord)

fun guildMessageChannel(guildId: Snowflake, id: Snowflake): GuildMessageChannelBehavior =
GuildMessageChannelBehavior(guildId, id, kord)

fun newsChannel(guildId: Snowflake, id: Snowflake): NewsChannelBehavior =
NewsChannelBehavior(guildId = guildId, id = id, kord = kord)

Expand All @@ -53,11 +59,9 @@ class Unsafe(private val kord: Kord) {
fun storeChannel(guildId: Snowflake, id: Snowflake): StoreChannelBehavior =
StoreChannelBehavior(guildId = guildId, id = id, kord = kord)


fun publicThreadParent(guildId: Snowflake, id: Snowflake): ThreadParentChannelBehavior =
ThreadParentChannelBehavior(guildId, id, kord)


fun privateThreadParent(guildId: Snowflake, id: Snowflake): PrivateThreadParentChannelBehavior =
PrivateThreadParentChannelBehavior(guildId, id, kord)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ internal fun GuildMessageChannelBehavior(
override val kord: Kord = kord
override val supplier: EntitySupplier = strategy.supply(kord)

override fun equals(other: Any?): Boolean = when (other) {
is GuildChannelBehavior -> other.id == id && other.guildId == guildId
is ChannelBehavior -> other.id == id
else -> false
}

HopeBaron marked this conversation as resolved.
Show resolved Hide resolved
override fun toString(): String {
return "GuildMessageChannelBehavior(id=$id, guildId=$guildId, kord=$kord, supplier=$supplier)"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* The behavior of a Discord channel associated to a [guild].
* The behavior of a Discord non-thread channel associated to a [guild].
* [TopGuildChannelBehavior] incapsulates the behavior we have known pre-threads such as
* [NewsChannelBehavior] and [dev.kord.core.behavior.channel.TextChannelBehavior].
*
* for the behaviors supported for all guild message channels please see [dev.kord.core.behavior.channel.GuildMessageChannelBehavior].
*
HopeBaron marked this conversation as resolved.
Show resolved Hide resolved
*/
interface TopGuildChannelBehavior : GuildChannelBehavior {

Expand Down Expand Up @@ -98,7 +103,7 @@ interface TopGuildChannelBehavior : GuildChannelBehavior {

}

fun TopGuildChannelBehavior(
internal fun TopGuildChannelBehavior(
guildId: Snowflake,
id: Snowflake,
kord: Kord,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* The behavior of a Discord message channel associated to a [guild].
* The behavior of a Discord non-thread message channel associated to a [guild].
* [TopGuildChannelBehavior] encapsulates the behavior we have known pre-threads such as
* [VoiceChannelBehavior] and [TextChannelBehavior].
*
* for the behaviors supported for all guild channels please see [GuildChannelBehavior].
HopeBaron marked this conversation as resolved.
Show resolved Hide resolved
*
*/
interface TopGuildMessageChannelBehavior : TopGuildChannelBehavior, GuildMessageChannelBehavior {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.ThreadParentChannel
import dev.kord.core.entity.channel.thread.ThreadChannel
import dev.kord.core.entity.channel.thread.ThreadMember
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.core.supplier.getChannelOf
Expand Down Expand Up @@ -91,10 +92,22 @@ interface ThreadChannelBehavior : GuildMessageChannelBehavior {
super.delete(reason)
}

/**
* Requests to get this channel's [ThreadParentChannel].
*
* @throws [RequestException] if something went wrong during the request.
* @throws [EntityNotFoundException] if the thread parent wasn't present.
*/
suspend fun getParent(): ThreadParentChannel {
return supplier.getChannelOf(parentId)
}

/**
* Requests to get this channel's [ThreadParentChannel],
* returns null if the thread parent isn't present.
*
* @throws [RequestException] if something went wrong during the request.
*/
suspend fun getParentOrNull(): ThreadParentChannel? {
return supplier.getChannelOfOrNull(parentId)
}
Expand Down
74 changes: 69 additions & 5 deletions core/src/main/kotlin/entity/channel/thread/DeletedThreadChannel.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,84 @@
package dev.kord.core.entity.channel.thread

import dev.kord.common.entity.ChannelType
import dev.kord.common.entity.Snowflake
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.behavior.GuildBehavior
import dev.kord.core.behavior.channel.GuildChannelBehavior
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.Guild
import dev.kord.core.entity.Strategizable
import dev.kord.core.entity.channel.GuildChannel
import dev.kord.core.entity.channel.ThreadParentChannel
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.core.supplier.getChannelOf
import dev.kord.core.supplier.getChannelOfOrNull

class DeletedThreadChannel(
override val data: ChannelData,
override val kord: Kord,
val data: ChannelData,
val kord: Kord,
override val supplier: EntitySupplier = kord.defaultSupplier
) : Channel {
) : Strategizable {

val id: Snowflake
get() = data.id

val type: ChannelType get() = data.type

val guildId: Snowflake
get() = data.guildId.value!!
get() = data.guildId.value!!

val guild: GuildBehavior get() = GuildBehavior(guildId, kord)

val parentId: Snowflake get() = data.parentId!!.value!!

val parent: ThreadParentChannelBehavior get() = ThreadParentChannelBehavior(guildId, id, kord)

/**
* Requests to get this channel's [Guild].
*
* @throws [RequestException] if something went wrong during the request.
* @throws [EntityNotFoundException] if the guild wasn't present.
*/
suspend fun getGuild(): Guild = supplier.getGuild(guildId)

/**
* Requests to get this channel's [Guild],
* returns null if the guild isn't present.
*
* @throws [RequestException] if something went wrong during the request.
*/
suspend fun getGuildOrNull(): Guild? = supplier.getGuildOrNull(guildId)


/**
* Requests to get this channel's [ThreadParentChannel].
*
* @throws [RequestException] if something went wrong during the request.
* @throws [EntityNotFoundException] if the thread parent wasn't present.
*/
suspend fun getParent(): ThreadParentChannel {
return supplier.getChannelOf(parentId)
}

/**
* Requests to get this channel's [ThreadParentChannel],
* returns null if the thread parent isn't present.
*
* @throws [RequestException] if something went wrong during the request.
*/
suspend fun getParentOrNull(): ThreadParentChannel? {
return supplier.getChannelOfOrNull(parentId)
}


override fun withStrategy(strategy: EntitySupplyStrategy<*>): DeletedThreadChannel {
return DeletedThreadChannel(data, kord, strategy.supply(kord))
}


}
15 changes: 11 additions & 4 deletions core/src/main/kotlin/event/channel/thread/ThreadDeleteEvent.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package dev.kord.core.event.channel.thread

import dev.kord.core.Kord
import dev.kord.core.entity.channel.thread.DeletedThreadChannel
import dev.kord.core.entity.channel.thread.NewsChannelThread
import dev.kord.core.entity.channel.thread.TextChannelThread
import dev.kord.core.entity.channel.thread.ThreadChannel
import dev.kord.core.event.Event
import dev.kord.core.event.channel.ChannelCreateEvent
import dev.kord.core.event.channel.ChannelDeleteEvent

sealed interface ThreadChannelDeleteEvent : ChannelDeleteEvent {
override val channel: DeletedThreadChannel
sealed interface ThreadChannelDeleteEvent : Event {
val channel: DeletedThreadChannel

val old: ThreadChannel?

override val kord: Kord
get() = channel.kord

}


Expand Down Expand Up @@ -37,9 +44,9 @@ class NewsChannelThreadDeleteEvent(

class UnknownChannelThreadDeleteEvent(
override val channel: DeletedThreadChannel,
old: ThreadChannel?,
override val old: ThreadChannel?,
override val shard: Int
) : ChannelCreateEvent {
) : ThreadChannelDeleteEvent {
override fun toString(): String {
return "UnknownChannelThreadDeleteEvent(channel=$channel, shard=$shard)"
}
Expand Down