Skip to content

Commit

Permalink
Add a separate event filter for events submitted to the bot directly,…
Browse files Browse the repository at this point in the history
… rather than from Kord.
  • Loading branch information
gdude2002 committed Dec 31, 2024
1 parent 41ba65c commit d8a8aec
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
23 changes: 17 additions & 6 deletions kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ public open class ExtensibleBot(
logger.debug { "Kord event filter predicate not set." }

kord.on<Event> {
send(this@on)
sendKord(this@on)
}
} else {
logger.debug { "Kord event filter predicate set, filtering Kord events." }

kord.on<Event> {
if (settings.kordEventFilter!!(this@on)) {
send(this@on)
sendKord(this@on)
}
}
}
Expand Down Expand Up @@ -275,8 +275,8 @@ public open class ExtensibleBot(
GuildJoinRequestUpdateEvent(data)
}

else -> null
} ?: return@on
else -> return@on
}

send(eventObj)
} catch (e: Exception) {
Expand Down Expand Up @@ -432,12 +432,23 @@ public open class ExtensibleBot(
.launchIn(kordRef)

/**
* @suppress
* @suppress Internal function used to process Kord events. Don't call this yourself.
*/
public suspend inline fun send(event: Event) {
protected suspend inline fun sendKord(event: Event) {
eventPublisher.emit(event)
}

/**
* Submit an event, triggering any relevant event handlers.
*
* Events submitted using this function are filtered by [ExtensibleBotBuilder.kordEventFilter].
*/
public suspend inline fun send(event: Event, filter: Boolean = true) {
if (!filter || settings.kordExEventFilter?.invoke(event) != false) {
eventPublisher.emit(event)
}
}

/**
* Install an [Extension] to this bot.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public open class ExtensibleBotBuilder {
/** @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 var kordExEventFilter: (suspend Event.() -> Boolean)? = null

/** @suppress Builder that shouldn't be set directly by the user. **/
public open val extensionsBuilder: ExtensionsBuilder = ExtensionsBuilder()

Expand Down Expand Up @@ -201,11 +204,41 @@ public open class ExtensibleBotBuilder {
/**
* Set an event-filtering predicate, which may selectively prevent Kord events from being processed by returning
* `false`.
*
* This only filters events created by Kord.
* For events submitted by Kord Extensions or loaded extensions, see [kordExEventFilter].
*/
@Deprecated(
level = DeprecationLevel.ERROR,
message = "Disambiguation: Renamed to kordEventFilter.",
replaceWith = ReplaceWith("kordEventFilter"),
)
public fun eventFilter(predicate: suspend Event.() -> Boolean) {
kordEventFilter = predicate
}

/**
* Set an event-filtering predicate, which may selectively prevent Kord-created events from being processed by
* returning `false`.
*
* This only filters events created by Kord.
* For events submitted by Kord Extensions or loaded extensions, see [kordExEventFilter].
*/
public fun kordEventFilter(predicate: suspend Event.() -> Boolean) {
kordEventFilter = predicate
}

/**
* Set an event-filtering predicate, which may selectively prevent KordEx-created events from being processed by
* returning `false`.
*
* This only filters events submitted by Kord Extensions or loaded extensions.
* For events created by Kord, see [kordEventFilter].
*/
public fun kordExEventFilter(predicate: suspend Event.() -> Boolean) {
kordExEventFilter = predicate
}

/**
* DSL function used to configure information about the bot.
*
Expand Down

0 comments on commit d8a8aec

Please sign in to comment.