@@ -7,22 +7,49 @@ import io.github.freya022.botcommands.api.core.annotations.BEventListener
77import io.github.freya022.botcommands.api.core.requests.PriorityGlobalRestRateLimiter
88import io.github.freya022.botcommands.api.core.service.ClassGraphProcessor
99import io.github.freya022.botcommands.api.core.service.annotations.InjectedService
10- import io.github.freya022.botcommands.api.core.utils.enumSetOf
11- import io.github.freya022.botcommands.api.core.utils.loggerOf
12- import io.github.freya022.botcommands.api.core.utils.toImmutableList
13- import io.github.freya022.botcommands.api.core.utils.toImmutableSet
10+ import io.github.freya022.botcommands.api.core.utils.*
1411import io.github.freya022.botcommands.api.core.waiter.EventWaiter
1512import io.github.freya022.botcommands.internal.core.config.ConfigDSL
1613import io.github.freya022.botcommands.internal.core.config.ConfigurationValue
14+ import io.github.freya022.botcommands.internal.utils.putIfAbsentOrThrow
15+ import io.github.freya022.botcommands.internal.utils.throwInternal
1716import io.github.oshai.kotlinlogging.KotlinLogging
1817import net.dv8tion.jda.api.events.Event
1918import net.dv8tion.jda.api.requests.GatewayIntent
2019import net.dv8tion.jda.api.requests.RestRateLimiter
2120import net.dv8tion.jda.api.utils.messages.MessageCreateData
2221import org.intellij.lang.annotations.Language
22+ import kotlin.reflect.KClass
2323
2424@InjectedService
25- interface BConfig {
25+ interface BConfig : IConfig , BConfigProps {
26+ override val configType get() = BConfig ::class .java
27+
28+ val eventManagerConfig: BEventManagerConfig
29+ val serviceConfig: BServiceConfig
30+ val databaseConfig: BDatabaseConfig
31+ val localizationConfig: BLocalizationConfig
32+ val appEmojisConfig: BAppEmojisConfig
33+ val textConfig: BTextConfig
34+ val applicationConfig: BApplicationConfig
35+ val modalsConfig: BModalsConfig
36+ val componentsConfig: BComponentsConfig
37+ val coroutineScopesConfig: BCoroutineScopesConfig
38+
39+ /* *
40+ * An immutable collection of all [registered][BConfigBuilder.withConfig] configuration objects.
41+ */
42+ val configs: Collection <IConfig >
43+
44+ /* *
45+ * Gets a configuration of the provided type, or `null` if none were [registered][BConfigBuilder.withConfig].
46+ *
47+ * @param type The type of configuration to get, based on [configType].
48+ */
49+ fun <T : IConfig > getConfigOrNull (type : Class <T >): T ?
50+ }
51+
52+ interface BConfigProps {
2653 /* *
2754 * Predefined user IDs of the bot owners, allowing bypassing cooldowns, user permission checks,
2855 * and having [hidden commands][Hidden] shown.
@@ -106,21 +133,24 @@ interface BConfig {
106133 val ignoreRestRateLimiter: Boolean
107134
108135 val classGraphProcessors: List <ClassGraphProcessor >
109-
110- val eventManagerConfig: BEventManagerConfig
111- val serviceConfig: BServiceConfig
112- val databaseConfig: BDatabaseConfig
113- val localizationConfig: BLocalizationConfig
114- val appEmojisConfig: BAppEmojisConfig
115- val textConfig: BTextConfig
116- val applicationConfig: BApplicationConfig
117- val modalsConfig: BModalsConfig
118- val componentsConfig: BComponentsConfig
119- val coroutineScopesConfig: BCoroutineScopesConfig
120136}
121137
138+ /* *
139+ * Gets a configuration of the provided type, or `null` if none were [registered][BConfigBuilder.withConfig].
140+ *
141+ * @param type The type of configuration to get, based on [configType][BConfig.configType].
142+ */
143+ fun <T : IConfig > BConfig.getConfigOrNull (type : KClass <T >): T ? = getConfigOrNull(type.java)
144+
145+ /* *
146+ * Gets a configuration of the provided type, or `null` if none were [registered][BConfigBuilder.withConfig].
147+ *
148+ * @param T The type of configuration to get, based on [configType][BConfig.configType].
149+ */
150+ inline fun <reified T : IConfig > BConfig.getConfigOrNull (): T ? = getConfigOrNull(T ::class .java)
151+
122152@ConfigDSL
123- class BConfigBuilder : BConfig {
153+ class BConfigBuilder : BConfigProps {
124154 override val packages: MutableSet <String > = HashSet ()
125155 override val classes: MutableSet <Class <* >> = HashSet ()
126156
@@ -139,16 +169,18 @@ class BConfigBuilder : BConfig {
139169
140170 override val classGraphProcessors: MutableList <ClassGraphProcessor > = arrayListOf ()
141171
142- override val eventManagerConfig = BEventManagerConfigBuilder ()
143- override val serviceConfig = BServiceConfigBuilder ()
144- override val databaseConfig = BDatabaseConfigBuilder ()
145- override val localizationConfig = BLocalizationConfigBuilder ()
146- override val appEmojisConfig = BAppEmojisConfigBuilder ()
147- override val textConfig = BTextConfigBuilder ()
148- override val applicationConfig = BApplicationConfigBuilder ()
149- override val modalsConfig = BModalsConfigBuilder ()
150- override val componentsConfig = BComponentsConfigBuilder ()
151- override val coroutineScopesConfig = BCoroutineScopesConfigBuilder ()
172+ private val _configs : MutableMap <Class <out IConfig >, IConfig > = hashMapOf()
173+
174+ val eventManagerConfig = BEventManagerConfigBuilder ()
175+ val serviceConfig = BServiceConfigBuilder ()
176+ val databaseConfig = BDatabaseConfigBuilder ()
177+ val localizationConfig = BLocalizationConfigBuilder ()
178+ val appEmojisConfig = BAppEmojisConfigBuilder ()
179+ val textConfig = BTextConfigBuilder ()
180+ val applicationConfig = BApplicationConfigBuilder ()
181+ val modalsConfig = BModalsConfigBuilder ()
182+ val componentsConfig = BComponentsConfigBuilder ()
183+ val coroutineScopesConfig = BCoroutineScopesConfigBuilder ()
152184
153185 /* *
154186 * Predefined user IDs of the bot owners, allowing bypassing cooldowns, user permission checks,
@@ -278,6 +310,19 @@ class BConfigBuilder : BConfig {
278310 componentsConfig.apply (block)
279311 }
280312
313+ /* *
314+ * Registers the provided configuration, once configured, it cannot be modified or overwritten.
315+ *
316+ * @param newConfig The new configuration
317+ *
318+ * @throws IllegalStateException If a config of the same type was already registered
319+ */
320+ fun withConfig (newConfig : IConfig ) {
321+ _configs .putIfAbsentOrThrow(newConfig.configType, newConfig) { _ ->
322+ " Cannot reassign configuration of ${newConfig.configType.simpleNestedName} , please configure it entirely then assign once"
323+ }
324+ }
325+
281326 fun build (): BConfig {
282327 val logger = KotlinLogging .loggerOf<BConfig >()
283328 if (disableExceptionsInDMs)
@@ -303,6 +348,29 @@ class BConfigBuilder : BConfig {
303348 override val modalsConfig = this @BConfigBuilder.modalsConfig.build()
304349 override val componentsConfig = this @BConfigBuilder.componentsConfig.build()
305350 override val coroutineScopesConfig = this @BConfigBuilder.coroutineScopesConfig.build()
351+ private val _configs = (this @BConfigBuilder._configs + mapOf (
352+ this .configType to this ,
353+ eventManagerConfig.configType to eventManagerConfig,
354+ serviceConfig.configType to serviceConfig,
355+ databaseConfig.configType to databaseConfig,
356+ localizationConfig.configType to localizationConfig,
357+ appEmojisConfig.configType to appEmojisConfig,
358+ textConfig.configType to textConfig,
359+ applicationConfig.configType to applicationConfig,
360+ modalsConfig.configType to modalsConfig,
361+ componentsConfig.configType to componentsConfig,
362+ coroutineScopesConfig.configType to coroutineScopesConfig,
363+ )).unmodifiableView()
364+ override val configs get() = _configs .values
365+
366+ override fun <T : IConfig > getConfigOrNull (type : Class <T >): T ? {
367+ val config = _configs [type] ? : return null
368+ if (! type.isInstance(config)) {
369+ throwInternal(" $config (${config.javaClass.name} ) is not an instance of ${type.name} " )
370+ }
371+
372+ return type.cast(config)
373+ }
306374 }
307375 }
308376}
0 commit comments