JDA icon indicating copy to clipboard operation
JDA copied to clipboard

Add methods to overwrite or delete SubcommandGroups/Subcommands/Options from SlashCommandData.

Open risu729 opened this issue 1 year ago • 10 comments

General Troubleshooting

  • [X] I have checked for similar issues on the Issue-tracker.
  • [X] I have updated to the latest JDA version
  • [x] I have checked the branches or the maintainers' PRs for upcoming features.

Feature Request

There are no ways to overwrite or delete SubcommandGroups/Subcommands/Options from SlashCommandData. Since other fields (name, description, default permission, etc...) are modifiable, I consider adding clear methods to SlashCommandData a good solution for consistency.

If I use SlashCommandData.addOptions to overwrite an option, the exception has thrown.

Exception in thread "main" java.lang.IllegalArgumentException: Cannot have multiple options with the same name. Name: "test" appeared 2 times!
	at net.dv8tion.jda.internal.utils.Checks.checkUnique(Checks.java:222)
	at net.dv8tion.jda.api.interactions.commands.build.SubcommandData.addOptions(SubcommandData.java:253)

Example Use-Case

SlashCommandData command = Commands.slash("test", "This command is for test")
    .addOption(OptionType.BOOLEAN, "option", "description1")
    .clearOptions();

risu729 avatar Sep 19 '22 10:09 risu729

I don't really see how this is a valid use case tbh

freya022 avatar Sep 19 '22 10:09 freya022

I didn't have a simple example, sorry.

I'd like to modify options after creation like this.

public SlashCommandData setOptionsDefaultDescription(SlashCommandData command, LocalizationFunction function) {
  List<OptionData> options = command.getOptions().stream()
      .map(option -> option.setDescription(function.apply("command." + option.getName() + ".description").get(DiscordLocale.ENGLISH_US)))
      .toList();
  return command.clearOptions().addOptions(options);
}

risu729 avatar Sep 19 '22 10:09 risu729

Why not have the options right on the first time ? you could do this with an intermediate list

freya022 avatar Sep 19 '22 10:09 freya022

I'd like to create an option data with dummy description text first because I'd like to get the default description from a resource bundle but the parent command name is unknown at that moment. I know this is a rare use case, however, there are setName or setDescription, which are also not so useful.

risu729 avatar Sep 19 '22 10:09 risu729

Well honestly i've been able to do without it

Also you can use the native localization support in JDA instead of doing it when building commands, look in LocalizationFunction

freya022 avatar Sep 19 '22 10:09 freya022

I have checked LocalizationFunction but it seemed there is no support for the default locale. I'd like to get a Japanese description from command_ja.properties and a default description, I mean for all other languages, from command.properties. Is this possible with LocalizationFunction?

risu729 avatar Sep 19 '22 10:09 risu729

The "default" description is the one you set on #setDescription, for any other languages (i.e other than english) you put them in the resource bundle

by the way there's ResourceBundleLocalizationFunction

freya022 avatar Sep 19 '22 10:09 freya022

I'm sorry for my unclear comments. I mean, I'd like to use ResourceBundleLocalizationFunction for the default description too like the following.

var command = Commands.slash("name", "empty").setLocalizationFunction(resourceBundleLocalization);

where, commands.properties contains name.description="This is the default description." and commands.ja_properties contains name.description="This is the Japanese description."

I understand setting commands.properties for any other languages in ResourceBundleLocalizationFunction can be a solution, but it's not clean.

risu729 avatar Sep 19 '22 11:09 risu729

I've added something to remove options and the likes in #2258

MinnDevelopment avatar Sep 22 '22 11:09 MinnDevelopment

Thank you both so much. That looks good.

risu729 avatar Sep 23 '22 08:09 risu729