JDA
JDA copied to clipboard
Add methods to overwrite or delete SubcommandGroups/Subcommands/Options from SlashCommandData.
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();
I don't really see how this is a valid use case tbh
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);
}
Why not have the options right on the first time ? you could do this with an intermediate list
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.
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
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?
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
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.
I've added something to remove options and the likes in #2258
Thank you both so much. That looks good.