From e42dde8911bb2808fb8bb84089bb60b61da9ec4b Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Tue, 3 Sep 2024 09:40:54 +0100 Subject: [PATCH] Update deps, remove double launch, add Kord event filter --- gradle/libs.versions.toml | 4 ++-- .../main/kotlin/dev/kordex/core/ExtensibleBot.kt | 14 ++++++++++++-- .../kordex/core/builders/ExtensibleBotBuilder.kt | 12 ++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3519c871bc..e4404c6918 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -14,10 +14,10 @@ junit = "5.11.0" kaml = "0.61.0" koin = "3.5.6" kord = "0.15.0-SNAPSHOT" -ksp = "2.0.20-RC2-1.0.24" +ksp = "2.0.20-1.0.24" ktor = "2.3.12" kx-coro = "1.8.1" -kx-ser = "1.7.1" +kx-ser = "1.7.2" linkie = "1.0.121" logback = "1.5.7" logback-groovy = "1.14.5" diff --git a/kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt b/kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt index 305e8245eb..e088120c48 100644 --- a/kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt +++ b/kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt @@ -136,10 +136,20 @@ public open class ExtensibleBot( settings.cacheBuilder.dataCacheBuilder.invoke(kord, kord.cache) - kord.on { - kord.launch { + if (settings.kordEventFilter == null) { + logger.debug { "Kord event filter predicate not set." } + + kord.on { send(this@on) } + } else { + logger.debug { "Kord event filter predicate set, filtering Kord events." } + + kord.on { + if (settings.kordEventFilter!!(this@on)) { + send(this@on) + } + } } addDefaultExtensions() diff --git a/kord-extensions/src/main/kotlin/dev/kordex/core/builders/ExtensibleBotBuilder.kt b/kord-extensions/src/main/kotlin/dev/kordex/core/builders/ExtensibleBotBuilder.kt index f8d88377b3..73b7908211 100644 --- a/kord-extensions/src/main/kotlin/dev/kordex/core/builders/ExtensibleBotBuilder.kt +++ b/kord-extensions/src/main/kotlin/dev/kordex/core/builders/ExtensibleBotBuilder.kt @@ -17,6 +17,7 @@ import dev.kord.core.behavior.UserBehavior import dev.kord.core.behavior.channel.ChannelBehavior import dev.kord.core.builder.kord.KordBuilder import dev.kord.core.entity.interaction.Interaction +import dev.kord.core.event.Event import dev.kord.gateway.Intent import dev.kord.gateway.Intents import dev.kord.gateway.NON_PRIVILEGED @@ -139,6 +140,9 @@ public open class ExtensibleBotBuilder { @OptIn(InternalAPI::class) public var dataCollectionMode: DataCollection = DATA_COLLECTION + /** @suppress Builder that shouldn't be set directly by the user. **/ + public var kordEventFilter: (suspend Event.() -> Boolean)? = null + /** @suppress Builder that shouldn't be set directly by the user. **/ public open val extensionsBuilder: ExtensionsBuilder = ExtensionsBuilder() @@ -197,6 +201,14 @@ public open class ExtensibleBotBuilder { /** Logging level Koin should use, defaulting to ERROR. **/ public var koinLogLevel: Level = Level.ERROR + /** + * Set an event-filtering predicate, which may selectively prevent Kord events from being processed by returning + * `false`. + */ + public fun eventFilter(predicate: suspend Event.() -> Boolean) { + kordEventFilter = predicate + } + /** * DSL function used to configure information about the bot. *