From 24f0614c73ba1e0f884f9776d3afb4bcf157a2e0 Mon Sep 17 00:00:00 2001 From: ZephyrKaOne <74645571+ZephyrKaOne@users.noreply.github.com> Date: Sun, 16 Oct 2022 16:03:35 +0200 Subject: [PATCH] Add Interaction Localization support (#78) Co-authored-by: Chew --- .../jdautilities/command/ContextMenu.java | 24 ++++++ .../jdautilities/command/SlashCommand.java | 75 +++++++++++++++++-- 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java b/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java index ed5e98c9..55749354 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java @@ -17,11 +17,15 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.DiscordLocale; import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.Commands; +import java.util.HashMap; +import java.util.Map; + /** * Middleware for child context menu types. Anything that extends this class will inherit the following options. * @@ -46,6 +50,19 @@ public String getName() return name; } + /** + * Localization of menu names. Allows discord to change the language of the name of menu in the client. + */ + protected Map nameLocalization = new HashMap<>(); + + /** + * Gets the specified localizations of menu name. + * @return Menu name localizations. + */ + public Map getNameLocalization() { + return nameLocalization; + } + /** * Gets the type of context menu. * @@ -135,6 +152,13 @@ public CommandData buildCommandData() data.setGuildOnly(this.guildOnly); + //Check name localizations + if (!getNameLocalization().isEmpty()) + { + //Add localizations + data.setNameLocalizations(getNameLocalization()); + } + return data; } } diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java index ebbe5c47..554ab033 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java @@ -22,13 +22,9 @@ import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel; import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; +import net.dv8tion.jda.api.interactions.DiscordLocale; import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions; -import net.dv8tion.jda.api.interactions.commands.build.CommandData; -import net.dv8tion.jda.api.interactions.commands.build.Commands; -import net.dv8tion.jda.api.interactions.commands.build.OptionData; -import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; -import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; -import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData; +import net.dv8tion.jda.api.interactions.commands.build.*; import java.util.ArrayList; import java.util.HashMap; @@ -72,6 +68,30 @@ */ public abstract class SlashCommand extends Command { + /** + * Localization of slash command name. Allows discord to change the language of the name of slash commands in the client.
+ * Example:
+ *

+     *     public Command() {
+     *          this.name = "help"
+     *          this.nameLocalization = Map.of(DiscordLocale.GERMAN, "hilfe", DiscordLocale.RUSSIAN, "помощь");
+     *     }
+     *
+ */ + protected Map nameLocalization = new HashMap<>(); + + /** + * Localization of slash command description. Allows discord to change the language of the description of slash commands in the client.
+ * Example:
+ *

+     *     public Command() {
+     *          this.description = "all commands"
+     *          this.descriptionLocalization = Map.of(DiscordLocale.GERMAN, "alle Befehle", DiscordLocale.RUSSIAN, "все команды");
+     *     }
+     *
+ */ + protected Map descriptionLocalization = new HashMap<>(); + /** * This option is deprecated in favor of using Discord's permissions
* This deprecation can be ignored if you intend to support normal and slash commands. @@ -383,6 +403,20 @@ public CommandData buildCommandData() { data.addOptions(getOptions()); } + + //Check name localizations + if (!getNameLocalization().isEmpty()) + { + //Add localizations + data.setNameLocalizations(getNameLocalization()); + } + //Check description localizations + if (!getDescriptionLocalization().isEmpty()) + { + //Add localizations + data.setDescriptionLocalizations(getDescriptionLocalization()); + } + // Check for children if (children.length != 0) { @@ -398,6 +432,19 @@ public CommandData buildCommandData() subcommandData.addOptions(child.getOptions()); } + //Check child name localizations + if (!child.getNameLocalization().isEmpty()) + { + //Add localizations + subcommandData.setNameLocalizations(child.getNameLocalization()); + } + //Check child description localizations + if (!child.getDescriptionLocalization().isEmpty()) + { + //Add localizations + subcommandData.setDescriptionLocalizations(child.getDescriptionLocalization()); + } + // If there's a subcommand group if (child.getSubcommandGroup() != null) { @@ -505,4 +552,20 @@ else if(cooldownScope.equals(CooldownScope.GUILD) && event.getGuild()==null) else return front+" "+cooldownScope.errorSpecification+"!"; } + + /** + * Gets the specified localizations of slash command names. + * @return Slash command name localizations. + */ + public Map getNameLocalization() { + return nameLocalization; + } + + /** + * Gets the specified localizations of slash command descriptions. + * @return Slash command description localizations. + */ + public Map getDescriptionLocalization() { + return descriptionLocalization; + } }