mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
asdf13
This commit is contained in:
parent
ab32fcc7dc
commit
f13412d77a
@ -289,7 +289,7 @@ data class DiscordGuildListAPI(
|
||||
)
|
||||
|
||||
suspend fun getDiscordUser(accessToken: String): DiscordMeAPI? {
|
||||
while(DiscordRatelimits.isLimited()) {
|
||||
if(DiscordRatelimits.isLimited()) {
|
||||
delay(DiscordRatelimits.getRateReset())
|
||||
}
|
||||
|
||||
@ -309,7 +309,7 @@ suspend fun getDiscordUser(accessToken: String): DiscordMeAPI? {
|
||||
}
|
||||
|
||||
suspend fun getUserGuilds(accessToken: String): List<DiscordGuildListAPI> {
|
||||
while(DiscordRatelimits.isLimited()) {
|
||||
if(DiscordRatelimits.isLimited()) {
|
||||
delay(DiscordRatelimits.getRateReset())
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ object DiscordGuildCache {
|
||||
|
||||
private suspend fun fetchGuilds(beforeGuildId: String? = null): List<DiscordGuildListAPI> {
|
||||
if(DiscordRatelimits.isLimited()) {
|
||||
delay(DiscordRatelimits.getRateReset().takeIf { it > 1000L } ?: 3000L)
|
||||
delay(DiscordRatelimits.getRateReset())
|
||||
}
|
||||
val result = applicationHttpClient.get("https://discord.com/api/users/@me/guilds") {
|
||||
headers {
|
||||
@ -57,7 +57,7 @@ object DiscordGuildCache {
|
||||
while (true) {
|
||||
try {
|
||||
val guilds = fetchGuilds(lastGuildId)
|
||||
if (guilds.isEmpty() || guilds.size <= 200) {
|
||||
if (guilds.isEmpty()) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -68,6 +68,7 @@ object DiscordGuildCache {
|
||||
)
|
||||
}
|
||||
lastGuildId = guilds.last().id
|
||||
if(guilds.size <= 200) break
|
||||
} catch(e: Exception) {
|
||||
logger.info("Exception in discord caches. ${e.stackTraceToString()}")
|
||||
return
|
||||
|
@ -1,25 +1,36 @@
|
||||
package space.mori.chzzk_bot.webserver.utils
|
||||
|
||||
import java.time.Duration
|
||||
import java.time.Instant
|
||||
|
||||
object DiscordRatelimits {
|
||||
private var rateLimit = RateLimit(0, 5, 0L)
|
||||
private var rateLimit = RateLimit(0, 5, Instant.now())
|
||||
|
||||
fun isLimited(): Boolean {
|
||||
return rateLimit.remainin == 0
|
||||
}
|
||||
|
||||
fun getRateReset() = (rateLimit.resetAfter * 1000) + 300L
|
||||
fun getRateReset(): Long {
|
||||
val now = Instant.now()
|
||||
val resetInstant = rateLimit.resetAfter
|
||||
return if (resetInstant.isAfter(now)) {
|
||||
Duration.between(now, resetInstant).toMillis()
|
||||
} else {
|
||||
0L // 이미 Rate Limit이 해제된 경우, 대기 시간은 0
|
||||
}
|
||||
}
|
||||
|
||||
private fun setRateLimit(rateLimit: RateLimit) {
|
||||
this.rateLimit = rateLimit
|
||||
}
|
||||
|
||||
fun setRateLimit(limit: Int?, remaining: Int?, resetAfter: Long?) {
|
||||
return setRateLimit(RateLimit(limit ?: 0, remaining ?: 0, resetAfter ?: 0L))
|
||||
return setRateLimit(RateLimit(limit ?: 0, remaining ?: 0, Instant.now().plusSeconds(resetAfter ?: 0L)))
|
||||
}
|
||||
}
|
||||
|
||||
data class RateLimit(
|
||||
val limit: Int,
|
||||
val remainin: Int,
|
||||
val resetAfter: Long,
|
||||
val resetAfter: Instant,
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user