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 common interface for interactions with a custom ID #2778

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Kaktushose
Copy link
Contributor

Pull Request Etiquette

Changes

  • Internal code
  • 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 getComponentId and getModalId methods 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant