JDA
JDA copied to clipboard
Add common interface for interactions with a custom ID
Pull Request Etiquette
- [x] I have checked the PRs for upcoming features/bug fixes.
- [x] I have read the contributing guidelines.
Changes
- [ ] Internal code
- [x] Library interface (affecting end-user code)
- [ ] Documentation
- [ ] Other: _____
Closes Issue: N/A
Description
Creates a common interface, CustomIdInteraction for all interactions that support a user provided custom id, namely components and modals. In the Discord API, the id is always called custom_id, only JDA introduces a distinction between componentId and modalId.
Use case:
Assume an interaction framework that listens for GenericInteractionCreateEvent. Using pattern matching in switch we want to determine if we need a new event handler or use an existing one.
Currently this looks like this:
var handler = switch (genericInteractionCreateEvent) {
case SlashCommandInteractionEvent _, GenericContextInteractionEvent<?> _,
CommandAutoCompleteInteractionEvent _ ->
newHandler();
case GenericComponentInteractionCreateEvent event when handlerExists(event.getComponentId()) ->
getHandler(event.getComponentId());
case ModalInteractionEvent event when handlerExists(event.getModalId()) ->
getHandler(event.getComponentId());
case GenericComponentInteractionCreateEvent event when shouldCreate(event.getComponentId()) ->
newHandler();
case ModalInteractionEvent event when shouldCreate(event.getModalId()) ->
newHandler();
default -> throw new UnsupportedOperationException("Unsupported jda event: %s".formatted(genericInteractionCreateEvent));
};
With common interface:
var handler = switch (genericInteractionCreateEvent) {
case SlashCommandInteractionEvent _, GenericContextInteractionEvent<?> _,
CommandAutoCompleteInteractionEvent _ ->
newHandler();
case CustomIdInteraction interaction when handlerExists(interaction.getCustomId()) ->
getHandler(interaction.getCustomId());
case CustomIdInteraction interaction when shouldCreate(interaction.getCustomId()) ->
newHandler();
default -> throw new UnsupportedOperationException("Unsupported jda event: %s".formatted(genericInteractionCreateEvent));
};
Pros
- Alignment with Discord API
- Useful for pattern matching
Cons
- The current approach leaves us with two methods that do exactly the same thing, which can be confusing
- Alternatively, if we would deprecate the
getComponentIdandgetModalIdmethods we would break a lot of end user code
I'm up for discussions and aware that the use case might not affect some users, since JDA still uses Java 8