mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
commit
3d2e9a8cb0
@ -132,7 +132,7 @@ val server = embeddedServer(Netty, port = 8080, ) {
|
|||||||
DiscordGuildCache.addGuild(guilds.associate {
|
DiscordGuildCache.addGuild(guilds.associate {
|
||||||
println("${it.id} ${it.name}")
|
println("${it.id} ${it.name}")
|
||||||
|
|
||||||
it.id to Guild(it.id, it.name, it.icon, it.banner, it.roles ?: emptyList())
|
it.id to Guild(it.id, it.name, it.icon, it.banner, it.roles ?: emptyList(), emptyList())
|
||||||
})
|
})
|
||||||
|
|
||||||
redirects[principal.state]?.let { redirect ->
|
redirects[principal.state]?.let { redirect ->
|
||||||
@ -296,6 +296,29 @@ data class GuildRole(
|
|||||||
val mentionable: Boolean,
|
val mentionable: Boolean,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
enum class ChannelType(val value: Int) {
|
||||||
|
GUILD_TEXT(0),
|
||||||
|
DM(1),
|
||||||
|
GUILD_VOICE(2),
|
||||||
|
GROUP_DM(3),
|
||||||
|
GUILD_CATEGORY(4),
|
||||||
|
GUILD_ANNOUNCEMENT(5),
|
||||||
|
ANNOUNCEMENT_THREAD(10),
|
||||||
|
PUBLIC_THREAD(11),
|
||||||
|
PRIVATE_THREAD(12),
|
||||||
|
GUILD_STAGE_VOICE(13),
|
||||||
|
GUILD_DIRECTORY(14),
|
||||||
|
GUILD_FORUM(15),
|
||||||
|
GUILD_MEDIA(16)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class GuildChannel(
|
||||||
|
val id: String,
|
||||||
|
val type: ChannelType,
|
||||||
|
val name: String?
|
||||||
|
)
|
||||||
|
|
||||||
suspend fun getDiscordUser(accessToken: String): DiscordMeAPI? {
|
suspend fun getDiscordUser(accessToken: String): DiscordMeAPI? {
|
||||||
if(DiscordRatelimits.isLimited()) {
|
if(DiscordRatelimits.isLimited()) {
|
||||||
delay(DiscordRatelimits.getRateReset())
|
delay(DiscordRatelimits.getRateReset())
|
||||||
|
@ -9,9 +9,7 @@ import kotlinx.coroutines.sync.Mutex
|
|||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import space.mori.chzzk_bot.common.utils.logger
|
import space.mori.chzzk_bot.common.utils.logger
|
||||||
import space.mori.chzzk_bot.webserver.DiscordGuildListAPI
|
import space.mori.chzzk_bot.webserver.*
|
||||||
import space.mori.chzzk_bot.webserver.GuildRole
|
|
||||||
import space.mori.chzzk_bot.webserver.dotenv
|
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
@ -32,6 +30,9 @@ object DiscordGuildCache {
|
|||||||
if(guild?.guild?.roles?.isEmpty() == true) {
|
if(guild?.guild?.roles?.isEmpty() == true) {
|
||||||
guild.guild.roles = fetchGuildRoles(guildId)
|
guild.guild.roles = fetchGuildRoles(guildId)
|
||||||
}
|
}
|
||||||
|
if(guild?.guild?.channel?.isEmpty() == true) {
|
||||||
|
guild.guild.channel = fetchGuildChannels(guildId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cache[guildId]?.guild
|
return cache[guildId]?.guild
|
||||||
@ -83,6 +84,25 @@ object DiscordGuildCache {
|
|||||||
return result.body<List<GuildRole>>()
|
return result.body<List<GuildRole>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun fetchGuildChannels(guildId: String): List<GuildChannel> {
|
||||||
|
if(DiscordRatelimits.isLimited()) {
|
||||||
|
delay(DiscordRatelimits.getRateReset())
|
||||||
|
}
|
||||||
|
val result = applicationHttpClient.get("https://discord.com/api/guilds/${guildId}/channels") {
|
||||||
|
headers {
|
||||||
|
append(HttpHeaders.Authorization, "Bot ${dotenv["DISCORD_TOKEN"]}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val rateLimit = result.headers["X-RateLimit-Limit"]?.toIntOrNull()
|
||||||
|
val remaining = result.headers["X-RateLimit-Remaining"]?.toIntOrNull()
|
||||||
|
val resetAfter = result.headers["X-RateLimit-Reset-After"]?.toDoubleOrNull()?.toLong()?.plus(1L)
|
||||||
|
|
||||||
|
DiscordRatelimits.setRateLimit(rateLimit, remaining, resetAfter)
|
||||||
|
|
||||||
|
return result.body<List<GuildChannel>>().filter { it.type == ChannelType.GUILD_TEXT }
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun fetchAllGuilds() {
|
private suspend fun fetchAllGuilds() {
|
||||||
var lastGuildId: String? = null
|
var lastGuildId: String? = null
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -94,7 +114,7 @@ object DiscordGuildCache {
|
|||||||
|
|
||||||
guilds.forEach {
|
guilds.forEach {
|
||||||
cache[it.id] = CachedGuilds(
|
cache[it.id] = CachedGuilds(
|
||||||
Guild(it.id, it.name, it.icon, it.banner, it.roles ?: emptyList()),
|
Guild(it.id, it.name, it.icon, it.banner, it.roles ?: emptyList(), emptyList()),
|
||||||
Instant.now().plusSeconds(EXP_SECONDS),
|
Instant.now().plusSeconds(EXP_SECONDS),
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
@ -127,5 +147,6 @@ data class Guild(
|
|||||||
val name: String,
|
val name: String,
|
||||||
val icon: String?,
|
val icon: String?,
|
||||||
val banner: String?,
|
val banner: String?,
|
||||||
var roles: List<GuildRole> = emptyList(),
|
var roles: List<GuildRole>,
|
||||||
|
var channel: List<GuildChannel>
|
||||||
)
|
)
|
Loading…
x
Reference in New Issue
Block a user