Skip to content

Commit

Permalink
Add Interaction Localization support (#78)
Browse files Browse the repository at this point in the history
Co-authored-by: Chew <[email protected]>
  • Loading branch information
OurTwinkie and Chew authored Oct 16, 2022
1 parent 07b3371 commit 24f0614
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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<DiscordLocale, String> nameLocalization = new HashMap<>();

/**
* Gets the specified localizations of menu name.
* @return Menu name localizations.
*/
public Map<DiscordLocale, String> getNameLocalization() {
return nameLocalization;
}

/**
* Gets the type of context menu.
*
Expand Down Expand Up @@ -135,6 +152,13 @@ public CommandData buildCommandData()

data.setGuildOnly(this.guildOnly);

//Check name localizations
if (!getNameLocalization().isEmpty())
{
//Add localizations
data.setNameLocalizations(getNameLocalization());
}

return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.<br>
* Example:<br>
*<pre><code>
* public Command() {
* this.name = "help"
* this.nameLocalization = Map.of(DiscordLocale.GERMAN, "hilfe", DiscordLocale.RUSSIAN, "помощь");
* }
*</code></pre>
*/
protected Map<DiscordLocale, String> nameLocalization = new HashMap<>();

/**
* Localization of slash command description. Allows discord to change the language of the description of slash commands in the client.<br>
* Example:<br>
*<pre><code>
* public Command() {
* this.description = "all commands"
* this.descriptionLocalization = Map.of(DiscordLocale.GERMAN, "alle Befehle", DiscordLocale.RUSSIAN, "все команды");
* }
*</code></pre>
*/
protected Map<DiscordLocale, String> descriptionLocalization = new HashMap<>();

/**
* This option is deprecated in favor of using Discord's permissions<br>
* This deprecation can be ignored if you intend to support normal and slash commands.
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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<DiscordLocale, String> getNameLocalization() {
return nameLocalization;
}

/**
* Gets the specified localizations of slash command descriptions.
* @return Slash command description localizations.
*/
public Map<DiscordLocale, String> getDescriptionLocalization() {
return descriptionLocalization;
}
}

0 comments on commit 24f0614

Please sign in to comment.