From 85d728df8ad3bdf26e92ccc6e23d5a3ccbc1e495 Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Thu, 15 Aug 2024 06:11:48 +0900 Subject: [PATCH] get bot's guilds. --- .../mori/chzzk_bot/webserver/utils/DiscordGuildCache.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/DiscordGuildCache.kt b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/DiscordGuildCache.kt index 5ee9f5a..b339c84 100644 --- a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/DiscordGuildCache.kt +++ b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/DiscordGuildCache.kt @@ -22,10 +22,11 @@ object DiscordGuildCache { suspend fun getCachedGuilds(guildId: String): Guild? { val now = Instant.now() + val guild = cache[guildId] - if(cache.isEmpty() || !cache.containsKey(guildId) || cache[guildId]!!.timestamp.plusSeconds(EXP_SECONDS).isBefore(now)) { + if(guild == null || guild.timestamp.plusSeconds(EXP_SECONDS).isBefore(now) || !guild.isBotAvailable) { mutex.withLock { - if(cache.isEmpty() || !cache.containsKey(guildId) || cache[guildId]!!.timestamp.plusSeconds(EXP_SECONDS).isBefore(now)) { + if(guild == null || guild.timestamp.plusSeconds(EXP_SECONDS).isBefore(now) || !guild.isBotAvailable) { fetchAllGuilds() } } @@ -73,6 +74,7 @@ object DiscordGuildCache { cache[it.id] = CachedGuilds( Guild(it.id, it.name, it.icon, it.banner, it.roles), Instant.now().plusSeconds(EXP_SECONDS), + true ) } lastGuildId = guilds.last().id @@ -93,7 +95,8 @@ object DiscordGuildCache { data class CachedGuilds( val guild: Guild, - val timestamp: Instant = Instant.now() + val timestamp: Instant = Instant.now(), + val isBotAvailable: Boolean = false, ) @Serializable