mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
add DiscordGuildCache
- add DiscordGuildCache - GET /discord endpoint, return guild information.
This commit is contained in:
parent
07af664b0d
commit
167278e5cf
@ -15,6 +15,7 @@ import space.mori.chzzk_bot.common.events.DiscordRegisterEvent
|
||||
import space.mori.chzzk_bot.common.services.UserService
|
||||
import space.mori.chzzk_bot.common.utils.getRandomString
|
||||
import space.mori.chzzk_bot.webserver.UserSession
|
||||
import space.mori.chzzk_bot.webserver.utils.DiscordGuildCache
|
||||
|
||||
fun Route.apiDiscordRoutes() {
|
||||
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||
@ -63,7 +64,8 @@ fun Route.apiDiscordRoutes() {
|
||||
call.respond(HttpStatusCode.BadRequest, "User does not exist")
|
||||
return@get
|
||||
}
|
||||
call.respond(HttpStatusCode.OK, session.discordGuildList)
|
||||
|
||||
call.respond(HttpStatusCode.OK, DiscordGuildCache.getCachedGuilds(session.discordGuildList))
|
||||
return@get
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,75 @@
|
||||
package space.mori.chzzk_bot.webserver.utils
|
||||
|
||||
import applicationHttpClient
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.http.*
|
||||
import kotlinx.serialization.Serializable
|
||||
import space.mori.chzzk_bot.webserver.DiscordGuildListAPI
|
||||
import space.mori.chzzk_bot.webserver.dotenv
|
||||
import java.time.Instant
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
object DiscordGuildCache {
|
||||
private val cache = ConcurrentHashMap<String, CachedGuilds>()
|
||||
private const val EXP_SECONDS = 600L
|
||||
|
||||
suspend fun getCachedGuilds(guildId: String): Guild? {
|
||||
val now = Instant.now()
|
||||
|
||||
return if(cache.isNotEmpty() && cache[guildId]?.timestamp?.plusSeconds(EXP_SECONDS)?.isAfter(now) == true) {
|
||||
cache[guildId]?.guild
|
||||
} else {
|
||||
fetchAllGuilds()
|
||||
cache[guildId]?.guild
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getCachedGuilds(guildId: List<String>): List<Guild> {
|
||||
return guildId.mapNotNull { getCachedGuilds(it) }
|
||||
}
|
||||
|
||||
private suspend fun fetchGuilds(beforeGuildId: String? = null, limit: Int = 100): List<DiscordGuildListAPI> {
|
||||
val result = applicationHttpClient.get("https://discord.com/api/users/@me/guilds") {
|
||||
headers {
|
||||
append(HttpHeaders.Authorization, "Bot ${dotenv["DISCORD_TOKEN"]}")
|
||||
}
|
||||
parameter("limit", limit)
|
||||
if (beforeGuildId != null) {
|
||||
parameter("before", beforeGuildId)
|
||||
}
|
||||
}
|
||||
|
||||
return result.body<List<DiscordGuildListAPI>>()
|
||||
}
|
||||
|
||||
private suspend fun fetchAllGuilds() {
|
||||
var lastGuildId: String? = null
|
||||
while (true) {
|
||||
val guilds = fetchGuilds(lastGuildId)
|
||||
if (guilds.isEmpty()) {
|
||||
break
|
||||
}
|
||||
|
||||
guilds.forEach {
|
||||
cache[it.id] = CachedGuilds(
|
||||
Guild(it.id, it.name, it.icon, it.banner)
|
||||
)
|
||||
}
|
||||
lastGuildId = guilds.last().id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class CachedGuilds(
|
||||
val guild: Guild,
|
||||
val timestamp: Instant = Instant.now()
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class Guild(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val icon: String?,
|
||||
val banner: String?,
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user