Skip to content

Commit

Permalink
Hotfix for user apps and better logging for low-level event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Sep 17, 2024
1 parent f509bf2 commit 6ec1f6f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
17 changes: 14 additions & 3 deletions kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public open class ExtensibleBot(
* @param scope Coroutine scope to run the body of your callback under.
* @param consumer The callback to run when the event is fired.
*/
@Suppress("TooGenericExceptionCaught", "StringLiteralDuplication")
public inline fun <reified T : Event> on(
launch: Boolean = true,
scope: CoroutineScope = kordRef,
Expand All @@ -372,9 +373,19 @@ public open class ExtensibleBot(
.filterIsInstance<T>()
.onEach {
runCatching {
if (launch) scope.launch { consumer(it) } else consumer(it)
}.onFailure { logger.catching(it) }
}.catch { logger.catching(it) }
if (launch) {
scope.launch {
try {
consumer(it)
} catch (t: Throwable) {
logger.error(t) { "Error thrown from low-level event handler: $consumer" }
}
}
} else {
consumer(it)
}
}.onFailure { logger.error(it) { "Error thrown from low-level event handler: $consumer" } }
}.catch { logger.error(it) { "Error thrown from low-level event handler: $consumer" } }
.launchIn(kordRef)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package dev.kordex.core.utils

import dev.kord.core.entity.channel.GuildChannel
import dev.kord.core.entity.interaction.GuildInteraction
import dev.kord.core.event.Event
import dev.kord.core.event.interaction.InteractionCreateEvent
import dev.kord.core.event.message.MessageCreateEvent
Expand Down Expand Up @@ -54,15 +54,12 @@ public suspend fun InteractionCreateEvent.getLocale(): Locale {
var result = bot.settings.i18nBuilder.defaultLocale

for (resolver in bot.settings.i18nBuilder.localeResolvers) {
val channel = interaction.channel.asChannelOrNull()

val guild = if (channel is GuildChannel) {
channel.guild
} else {
null
}

val resolved = resolver(guild, interaction.channel, interaction.user, interaction)
val resolved = resolver(
(interaction as? GuildInteraction)?.guild,
interaction.channel,
interaction.user,
interaction
)

if (resolved != null) {
result = resolved
Expand Down

0 comments on commit 6ec1f6f

Please sign in to comment.