Skip to content

Commit

Permalink
Add cache flags to JDAService
Browse files Browse the repository at this point in the history
Preparing for discord-jda/JDA#2558
  • Loading branch information
freya022 committed Oct 27, 2023
1 parent 6816b91 commit a5cd08b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.dv8tion.jda.api.hooks.IEventManager
import net.dv8tion.jda.api.requests.GatewayIntent
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder
import net.dv8tion.jda.api.sharding.ShardManager
import net.dv8tion.jda.api.utils.cache.CacheFlag
import java.util.*

/**
Expand Down Expand Up @@ -51,6 +52,15 @@ abstract class JDAService {
*/
abstract val intents: Set<GatewayIntent>

/**
* The cache flags used by your bot,
* must at least be a subset of the cache flags your bot will use.
*
* To make sure JDA uses these flags,
* you can pass these to [JDABuilder.enableCache] / [DefaultShardManagerBuilder.enableCache].
*/
abstract val cacheFlags: Set<CacheFlag>

/**
* Creates a [JDA] or [ShardManager] instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ internal object JDAServiceMismatchChecker {
""".trimIndent()
}
}

val jdaCacheFlags = event.jda.cacheFlags
val jdaServiceCacheFlags = jdaService.cacheFlags
if (jdaCacheFlags.containsAll(jdaServiceCacheFlags)) {
logger.warn {
"""
The cache flags given in JDAService should at least be a subset of the JDA cache flags!
JDA intents: $jdaIntents
JDAService intents: $jdaServiceIntents
Hint: you should pass ${JDAService::cacheFlags.reference} to your builder
""".trimIndent()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.freya022.botcommands.test.services
import io.github.freya022.botcommands.api.core.JDAService
import io.github.freya022.botcommands.api.core.events.BReadyEvent
import io.github.freya022.botcommands.api.core.service.annotations.BService
import io.github.freya022.botcommands.api.core.utils.enumSetOf
import io.github.freya022.botcommands.test.config.Config
import net.dv8tion.jda.api.entities.Activity
import net.dv8tion.jda.api.hooks.IEventManager
Expand All @@ -16,9 +17,12 @@ class Bot(private val config: Config) : JDAService() {
override val intents: Set<GatewayIntent> =
defaultIntents + GatewayIntent.GUILD_MEMBERS + GatewayIntent.MESSAGE_CONTENT

override val cacheFlags: Set<CacheFlag>
get() = enumSetOf(CacheFlag.FORUM_TAGS, CacheFlag.VOICE_STATE)

override fun createJDA(event: BReadyEvent, eventManager: IEventManager) {
DefaultShardManagerBuilder.createLight(config.token, intents).apply {
enableCache(CacheFlag.FORUM_TAGS, CacheFlag.VOICE_STATE)
enableCache(cacheFlags)
setMemberCachePolicy(MemberCachePolicy.VOICE)
setActivityProvider { Activity.playing("coroutines go brrr #$it") }
setEventManagerProvider { eventManager }
Expand Down

0 comments on commit a5cd08b

Please sign in to comment.