From 82369b7256988c6325b958666ea6e021ac963e67 Mon Sep 17 00:00:00 2001 From: MrPowerGamerBR Date: Thu, 10 Oct 2024 22:59:49 -0300 Subject: [PATCH] Fix sonhos transfer interaction button not working on DMs --- .../cinnamon/discord/utils/I18nContextUtils.kt | 17 +++++++++++++++++ .../SonhosTransferInteractionsListener.kt | 11 ++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/loritta-bot-discord/src/main/kotlin/net/perfectdreams/loritta/cinnamon/discord/utils/I18nContextUtils.kt b/loritta-bot-discord/src/main/kotlin/net/perfectdreams/loritta/cinnamon/discord/utils/I18nContextUtils.kt index 204526bf98..1af16613bd 100644 --- a/loritta-bot-discord/src/main/kotlin/net/perfectdreams/loritta/cinnamon/discord/utils/I18nContextUtils.kt +++ b/loritta-bot-discord/src/main/kotlin/net/perfectdreams/loritta/cinnamon/discord/utils/I18nContextUtils.kt @@ -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] * diff --git a/loritta-bot-discord/src/main/kotlin/net/perfectdreams/loritta/morenitta/listeners/SonhosTransferInteractionsListener.kt b/loritta-bot-discord/src/main/kotlin/net/perfectdreams/loritta/morenitta/listeners/SonhosTransferInteractionsListener.kt index 61a294a33c..b5dda55417 100644 --- a/loritta-bot-discord/src/main/kotlin/net/perfectdreams/loritta/morenitta/listeners/SonhosTransferInteractionsListener.kt +++ b/loritta-bot-discord/src/main/kotlin/net/perfectdreams/loritta/morenitta/listeners/SonhosTransferInteractionsListener.kt @@ -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 @@ -32,7 +33,7 @@ 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() @@ -40,8 +41,12 @@ class SonhosTransferInteractionsListener(val loritta: LorittaBot) : ListenerAdap 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()