Concrete Command Definitions
See original GitHub issue- I have checked for similar issues.
- I have updated to the [latest JDA version][download].
- I have checked the branches or the maintainers’ PRs for upcoming features.
Feature Request
Define commands in a descriptive way that supports type safety when retrieving argument values. This would potentially create a high level abstraction over top of what JDA is forwarding from the Discord API, so I understand if this is rejected.
Reasoning
I’ve always been excited when I heard slash commands are coming with pre-defined parameters.
Now it’s awesome that JDA has them implemented but it’s not hitting that SPOT for me. When it comes to Java, I love the type safety. Code in a way which the compiler can verify that you haven’t made any typos, and have your code structured, easy to re-use, easy to profile and verify. More towards concrete typing, object oriented.
In the image above, two things there make me feel that the code isn’t really embracing what Java is about. And it’s more something you’d see in a dynamic language. I get it, it’s a quick example to show how slash commands can be implemented, but I don’t want it to be the only way because:
- Re-typing command names by hand can lead to typos that can’t be found until runtime,
- Forgetting what type your option is and choosing the wrong accessor is another bug you won’t find until runtime.
…And these two don’t make sense to have, especially in Java; because we’ve already spent time and effort defining our commands in the start of the example.
Possible To-Do:
- Add command skeleton interface
- Add functionality to register an implemented command skeleton
- Add type safety (type parameter implementation) to OptionData / OptionMapper
- Add the ability to deal with an incoming slash command usage by referencing the command skeleton
Possible Example of Type Safe Option
This original example code…
OptionMapping option = event.getOption("del_days");
if (option != null) // null = not provided
delDays = (int) Math.max(0, Math.min(7, option.getAsLong()));
… Could end up looking similar to …
process() {
int delDays = (int) Math.max(0, Math.min(7, this.getDeleteOption());
}
Original:
new OptionData(OptionType.USER, "user", "The user to ban")
After:
new UserOption("user", "The user to ban")
Where, UserOption extends OptionData<User>
CommandData would be a concrete class that defines the command and also is used as a command instance for processing.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
You are right. Most of the suggestions are out of scope for the API. I got mixed up when I read the Slashbot Example because I thought JDA was already trying to implement a command system. But after deeper analysis, JDA just wants to get the data from the programmer to Discord as efficiently as possible.
I really doubt anyone forgets the arguments they themself typed. And if you’re that forgetful should you perhaps take a break.
I can’t even see any real way here how commands and especially arguments, which allow pretty much any names possible, more type-safe.
What you ask for is that JDA remembers every single argument of every single command you created, which can be up to 100 commands in total. JDA does nothing more than sending the generated command info to Discord and patiently waits for Discord to send slash command events back, nothing more. Storing each command and their optionmappings as values in cache seems really pointless to have.
And you could for your own convenience just make a class with some static methods to retrieve data. Here is an example I use:
If you really fear to misstype a option name or similar, then how about making a final String in the class to use? Like here is an example with JDA-Chewtils’ SlashCommand setup:
If this is not what you were explaining would I appreciate some more info about this… I had a hard time really understanding your idea.