Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Interaction Localization support #78

Merged
merged 10 commits into from
Oct 16, 2022
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<>();

OurTwinkie marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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 Down Expand Up @@ -505,4 +539,20 @@ else if(cooldownScope.equals(CooldownScope.GUILD) && event.getGuild()==null)
else
return front+" "+cooldownScope.errorSpecification+"!";
}

OurTwinkie marked this conversation as resolved.
Show resolved Hide resolved
/**
* Gets the specified localizations of slash command names.
* @return Slash command name localizations.
*/
public Map<DiscordLocale, String> getNameLocalization() {
return nameLocalization;
}

OurTwinkie marked this conversation as resolved.
Show resolved Hide resolved
/**
* Gets the specified localizations of slash command descriptions.
* @return Slash command description localizations.
*/
public Map<DiscordLocale, String> getDescriptionLocalization() {
return descriptionLocalization;
}
}