From d07199eb04161c808f7ef2ac4f95a665dd4805c2 Mon Sep 17 00:00:00 2001 From: Matyrobbrt <65940752+Matyrobbrt@users.noreply.github.com> Date: Tue, 11 Jun 2024 02:40:45 +0300 Subject: [PATCH 1/3] Fix slash command children in groups matching children with the same name (#81) --- .../jagrosh/jdautilities/command/impl/CommandClientImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/impl/CommandClientImpl.java b/command/src/main/java/com/jagrosh/jdautilities/command/impl/CommandClientImpl.java index 2aa8bb4b..87c83523 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/impl/CommandClientImpl.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/impl/CommandClientImpl.java @@ -969,13 +969,13 @@ private SlashCommand findSlashCommand(String path) case 2: // Slash command with children // child check for(SlashCommand cmd: command.getChildren()) - if(cmd.isCommandFor(parts[1])) + if(cmd.isCommandFor(parts[1]) && cmd.getSubcommandGroup() == null) return cmd; return null; case 3: // Slash command with a group and a child for(SlashCommand cmd: command.getChildren()) - if(cmd.isCommandFor(parts[2]) && cmd.getSubcommandGroup().getName().equals(parts[1])) + if(cmd.isCommandFor(parts[2]) && cmd.getSubcommandGroup() != null && cmd.getSubcommandGroup().getName().equals(parts[1])) return cmd; return null; From 9dd9857fdf33e79e289afc39330cf0ea69acebca Mon Sep 17 00:00:00 2001 From: Mathias Sand Jahren Date: Tue, 18 Jun 2024 00:26:09 +0200 Subject: [PATCH 2/3] fix ButtonEmbedPaginator finalAction (#85) * Bugfix ButtonEmbedPaginator finalAction * Fix typo --- .../com/jagrosh/jdautilities/menu/ButtonEmbedPaginator.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/menu/src/main/java/com/jagrosh/jdautilities/menu/ButtonEmbedPaginator.java b/menu/src/main/java/com/jagrosh/jdautilities/menu/ButtonEmbedPaginator.java index eb68ef3e..e70e5d5d 100644 --- a/menu/src/main/java/com/jagrosh/jdautilities/menu/ButtonEmbedPaginator.java +++ b/menu/src/main/java/com/jagrosh/jdautilities/menu/ButtonEmbedPaginator.java @@ -263,7 +263,9 @@ private void handleButtonInteraction(ButtonInteractionEvent event, Message messa } } } else if (emoji.equals(STOP)) { - finalAction.accept(message); + event.deferEdit().queue( + interactionHook -> finalAction.accept(message) + ); return; } From 69edb0978fbf4b909bf9de62fd0355f6067fbc89 Mon Sep 17 00:00:00 2001 From: Olivia Date: Mon, 17 Jun 2024 17:26:54 -0500 Subject: [PATCH 3/3] feat: add a translate util file (#87) --- .../commons/utils/TranslateUtil.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 commons/src/main/java/com/jagrosh/jdautilities/commons/utils/TranslateUtil.java diff --git a/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/TranslateUtil.java b/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/TranslateUtil.java new file mode 100644 index 00000000..d12d85d2 --- /dev/null +++ b/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/TranslateUtil.java @@ -0,0 +1,100 @@ +package com.jagrosh.jdautilities.commons.utils; + +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.interactions.DiscordLocale; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * Very basic translation lib that accepts properties of translations for different locales. + *
+ * If you would like to use this for interactions, the locale MUST Be loaded before the CommandClient is built. + * + * @author Olivia + */ +public class TranslateUtil { + public static final Map LOCALES = new HashMap<>(); + private static DiscordLocale DEFAULT = DiscordLocale.ENGLISH_US; + + // Prevent instantiation + private TranslateUtil() {} + + /** + * Sets the default locale for the bot. By default, this is {@link DiscordLocale#ENGLISH_US}. + * + * @param locale The default locale + */ + public static void setDefaultLocale(DiscordLocale locale) { + DEFAULT = locale; + } + + /** + * Loads a locale file from the given path and adds it to the locale map. + * The properties file should be loaded already. + * + * @param locale The locale to load + * @param prop The loaded properties file + */ + public static void addLocale(DiscordLocale locale, Properties prop) { + LOCALES.put(locale, prop); + } + + /** + * Translates a string into the given server's locale. + * Recommended for non-ephemeral responses. For ephemeral responses, use {@link #t(DiscordLocale, String)}. + * + * @param key The key to translate + * @param server The server to translate for + * @return The translated string + */ + public static String t(Guild server, String key) { + DiscordLocale locale = server.getLocale(); + + String def = LOCALES.get(DEFAULT).getProperty(key, "No translation provided"); + + String translated = LOCALES.getOrDefault(locale, LOCALES.get(DEFAULT)).getProperty(key, def); + + if (translated.equals(def)) { + LoggerFactory.getLogger(TranslateUtil.class).warn("No translation provided for key {} in locale {}", key, locale); + } + + return translated; + } + + /** + * Converts the following key to the provided locale. Will default to {@code DEFAULT} if no locale.
+ * Recommended use: {@code t("MY_KEY", event.getUserLocale())} on interactions. + * + * @param locale The locale to convert to + * @param key The key to convert + * @return The translated string + */ + public static String t(DiscordLocale locale, String key) { + return LOCALES.getOrDefault(locale, LOCALES.get(DEFAULT)).getProperty(key, LOCALES.get(DEFAULT).getProperty(key, "No translation provided")); + } + + /** + * Builds a locale map for the given key, used for command descriptions and options. + * If there is no provided translation for a locale, it will not be included in the map. + * + * @param key The key to use for the command description + * @return A map of locales to their respective descriptions + */ + public static Map buildLocaleMap(String key) { + HashMap locales = new HashMap<>(); + // default + locales.put(DEFAULT, LOCALES.get(DEFAULT).getProperty(key, "No translation provided")); + + for (DiscordLocale locale : LOCALES.keySet()) { + String translation = LOCALES.get(locale).getProperty(key); + if (translation != null) { + locales.put(locale, translation); + } + } + + return locales; + } +}