Skip to content

Commit a5cd08b

Browse files
committed
Add cache flags to JDAService
Preparing for discord-jda/JDA#2558
1 parent 6816b91 commit a5cd08b

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/main/kotlin/io/github/freya022/botcommands/api/core/JDAService.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import net.dv8tion.jda.api.hooks.IEventManager
1212
import net.dv8tion.jda.api.requests.GatewayIntent
1313
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder
1414
import net.dv8tion.jda.api.sharding.ShardManager
15+
import net.dv8tion.jda.api.utils.cache.CacheFlag
1516
import java.util.*
1617

1718
/**
@@ -51,6 +52,15 @@ abstract class JDAService {
5152
*/
5253
abstract val intents: Set<GatewayIntent>
5354

55+
/**
56+
* The cache flags used by your bot,
57+
* must at least be a subset of the cache flags your bot will use.
58+
*
59+
* To make sure JDA uses these flags,
60+
* you can pass these to [JDABuilder.enableCache] / [DefaultShardManagerBuilder.enableCache].
61+
*/
62+
abstract val cacheFlags: Set<CacheFlag>
63+
5464
/**
5565
* Creates a [JDA] or [ShardManager] instance.
5666
*

src/main/kotlin/io/github/freya022/botcommands/internal/core/JDAServiceMismatchChecker.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ internal object JDAServiceMismatchChecker {
2828
""".trimIndent()
2929
}
3030
}
31+
32+
val jdaCacheFlags = event.jda.cacheFlags
33+
val jdaServiceCacheFlags = jdaService.cacheFlags
34+
if (jdaCacheFlags.containsAll(jdaServiceCacheFlags)) {
35+
logger.warn {
36+
"""
37+
The cache flags given in JDAService should at least be a subset of the JDA cache flags!
38+
JDA intents: $jdaIntents
39+
JDAService intents: $jdaServiceIntents
40+
Hint: you should pass ${JDAService::cacheFlags.reference} to your builder
41+
""".trimIndent()
42+
}
43+
}
3144
}
3245
}
3346
}

src/test/kotlin/io/github/freya022/botcommands/test/services/Bot.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.github.freya022.botcommands.test.services
33
import io.github.freya022.botcommands.api.core.JDAService
44
import io.github.freya022.botcommands.api.core.events.BReadyEvent
55
import io.github.freya022.botcommands.api.core.service.annotations.BService
6+
import io.github.freya022.botcommands.api.core.utils.enumSetOf
67
import io.github.freya022.botcommands.test.config.Config
78
import net.dv8tion.jda.api.entities.Activity
89
import net.dv8tion.jda.api.hooks.IEventManager
@@ -16,9 +17,12 @@ class Bot(private val config: Config) : JDAService() {
1617
override val intents: Set<GatewayIntent> =
1718
defaultIntents + GatewayIntent.GUILD_MEMBERS + GatewayIntent.MESSAGE_CONTENT
1819

20+
override val cacheFlags: Set<CacheFlag>
21+
get() = enumSetOf(CacheFlag.FORUM_TAGS, CacheFlag.VOICE_STATE)
22+
1923
override fun createJDA(event: BReadyEvent, eventManager: IEventManager) {
2024
DefaultShardManagerBuilder.createLight(config.token, intents).apply {
21-
enableCache(CacheFlag.FORUM_TAGS, CacheFlag.VOICE_STATE)
25+
enableCache(cacheFlags)
2226
setMemberCachePolicy(MemberCachePolicy.VOICE)
2327
setActivityProvider { Activity.playing("coroutines go brrr #$it") }
2428
setEventManagerProvider { eventManager }

0 commit comments

Comments
 (0)