Skip to content

Commit

Permalink
Fix threads cache (#634)
Browse files Browse the repository at this point in the history
* Cache threads from Guild events

* Replace `Guild.threads` with `GuildBehavior.cachedThreads`

* Fix compilation
  • Loading branch information
lukellmann authored Jul 10, 2022
1 parent 009b0ec commit 09d7cae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
17 changes: 15 additions & 2 deletions core/src/main/kotlin/behavior/GuildBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,26 @@ public interface GuildBehavior : KordEntity, Strategizable {
* Returns all active public and private threads in this guild
* Threads are ordered by their id, in descending order.
*
* The returned flow is lazily executed, any [RequestException] will be thrown on
* The returned flow is lazily executed, any [RequestException] will be thrown on
* [terminal operators](https://kotlinlang.org/docs/reference/coroutines/flow.html#terminal-flow-operators) instead.
*/
public val activeThreads: Flow<ThreadChannel>
get() = supplier.getActiveThreads(id)

/**
* Requests to get all threads in this guild that are present in [cache][Kord.cache].
*
* This property is not resolvable through REST and will always use [Kord.cache] instead.
*
* The returned flow is lazily executed, any [RequestException] will be thrown on
* [terminal operators](https://kotlinlang.org/docs/reference/coroutines/flow.html#terminal-flow-operators) instead.
*/
public val cachedThreads: Flow<ThreadChannel>
get() = kord.cache
.query<ChannelData> { idEq(ChannelData::guildId, this@GuildBehavior.id) }
.asFlow()
.mapNotNull { Channel.from(it, kord) as? ThreadChannel }

/**
* Requests to get all present webhooks for this guild.
*
Expand Down
1 change: 1 addition & 0 deletions core/src/main/kotlin/entity/Guild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class Guild(
public val afkChannel: VoiceChannelBehavior?
get() = afkChannelId?.let { VoiceChannelBehavior(guildId = id, id = it, kord = kord) }

@Deprecated("Use 'cachedThreads' instead.", ReplaceWith("cachedThreads"))
public val threads: Flow<ThreadChannel>
get() = flow {
data.threads.mapList {
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/kotlin/gateway/handler/GuildEventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ internal class GuildEventHandler(
cache.put(ChannelData.from(channel.copy(guildId = this.id.optionalSnowflake()))) //guild id always empty
}

for (thread in threads.orEmpty()) {
cache.put(ChannelData.from(thread.copy(guildId = this.id.optionalSnowflake()))) //guild id always empty
}

for (presence in presences.orEmpty()) {
cache.put(PresenceData.from(id, presence))
}

for (voiceState in voiceStates.orEmpty()) {
cache.put(VoiceStateData.from(id, voiceState))
}

for (emoji in emojis) {
cache.put(EmojiData.from(id, emoji.id!!, emoji))
}
Expand Down

0 comments on commit 09d7cae

Please sign in to comment.