Skip to content

Commit

Permalink
Fix sonhos transfer interaction button not working on DMs
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowerGamerBR committed Oct 11, 2024
1 parent ac51781 commit 82369b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package net.perfectdreams.loritta.cinnamon.discord.utils

import net.dv8tion.jda.api.interactions.DiscordLocale
import net.perfectdreams.loritta.common.locale.LanguageManager

object I18nContextUtils {
/**
* Converts a [DiscordLocale] to a i18nContext, if there isn't a matched language, returns null
*
* This should be updated every time a new language is introduced to Loritta
*
* @param languageManager the language manager
* @param jdaLocale the discord locale
* @return the [I18nContext] or, if it is not present, null
*/
fun convertDiscordLocaleToI18nContext(languageManager: LanguageManager, jdaLocale: DiscordLocale) = when (jdaLocale) {
DiscordLocale.ENGLISH_US -> languageManager.getI18nContextById("en")
DiscordLocale.ENGLISH_UK -> languageManager.getI18nContextById("en")
DiscordLocale.PORTUGUESE_BRAZILIAN -> languageManager.getI18nContextById("pt")
else -> null
}

/**
* Converts a [languageId] (example: "pt") to a JDA [DiscordLocale]
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import net.dv8tion.jda.api.hooks.ListenerAdapter
import net.dv8tion.jda.api.interactions.components.buttons.Button
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle
import net.perfectdreams.loritta.cinnamon.discord.interactions.commands.styled
import net.perfectdreams.loritta.cinnamon.discord.utils.I18nContextUtils
import net.perfectdreams.loritta.cinnamon.discord.utils.SonhosUtils
import net.perfectdreams.loritta.cinnamon.emotes.Emotes
import net.perfectdreams.loritta.cinnamon.pudding.tables.PaymentSonhosTransactionResults
Expand All @@ -32,16 +33,20 @@ import java.time.Instant

class SonhosTransferInteractionsListener(val loritta: LorittaBot) : ListenerAdapter() {
override fun onButtonInteraction(event: ButtonInteractionEvent) {
val guild = event.guild ?: return
val guild = event.guild

if (event.componentId.startsWith(SonhosPayExecutor.SONHOS_TRANSFER_ACCEPT_COMPONENT_PREFIX + ":")) {
val dbId = event.componentId.substringAfter(":").toLong()

GlobalScope.launch {
val deferredReply = event.interaction.deferEdit().await()

val serverConfig = loritta.getOrCreateServerConfig(guild.idLong, true)
val i18nContext = loritta.languageManager.getI18nContextByLegacyLocaleId(serverConfig.localeId)
val i18nContext = if (guild != null) {
val serverConfig = loritta.getOrCreateServerConfig(guild.idLong, true)
loritta.languageManager.getI18nContextByLegacyLocaleId(serverConfig.localeId)
} else {
I18nContextUtils.convertDiscordLocaleToI18nContext(loritta.languageManager, event.interaction.userLocale) ?: loritta.languageManager.defaultI18nContext
}

val result = loritta.transaction {
val now = Instant.now()
Expand Down

0 comments on commit 82369b7

Please sign in to comment.