Skip to content

Added ComponentIdInteractor delegate #588

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

Merged
merged 6 commits into from
Oct 13, 2022

Conversation

Zabuzard
Copy link
Member

@Zabuzard Zabuzard commented Sep 29, 2022

Overview

This adds a new class called ComponentIdInteractor, which effectively is a delegate-pattern solution to provide component-ID generation based out of a class that can be used as delegate for composition, instead of relying on the existing BotCommandAdapter inheritance.

Essentially, the existing code to generate component IDs has been moved out of BotCommandAdapter into this class instead. BotCommandAdapter now delegates to ComponentIdInteractor for this functionality.

Advantage

The benefit is that non-BotCommand classes, in particular Routines can now easily access this functionality as well. For example, a routine might now easily create buttons like this:

public class FooRoutine implements Routine, UserInteractor {
  // Create the delegate
  private final ComponentIdInteractor componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName());

  // Stuff from Routine
  @Override
  public Schedule createSchedule() { ... }

  @Override
  public void runRoutine(JDA jda) {
    ...
    // Create some button using the delegate, and later attach it to some message
    Button button = Button.success(
      componentIdInteractor.generateComponentId("John"),
      "click me");
    ...
  }

  // Stuff from UserInteractor
  @Override
  public String getName() {
    return "foo";
  }

  @Override
  public UserInteractionType getInteractionType() {
    return UserInteractionType.OTHER;
  }

  @Override
  public void acceptComponentIdGenerator(ComponentIdGenerator generator) {
    componentIdInteractor.acceptComponentIdGenerator(generator); // delegate
  }

  @Override
  public void onButtonClick(ButtonInteractionEvent event, List<String> args) {
    System.out.println("Hello " + args.get(0)); // Hello John
  }

  @Override
  public void onSelectionMenu(SelectMenuInteractionEvent event, List<String> args) {
    // Not interested in
  }
}

Redesign prefix

While at it, the whole topic with UserInteractorPrefix was redesigned. It is now essentially renamed to UserInteractionType and will not only be used for prefixes, but essentailly just represents the type of interaction.

This is then used by BotCore for prefix-based-forwarding. Implementations of UserInteractor now have to explicitly tell their type instead of the system being based on the classes and reflection.

This gives maximum flexibility. At the same time, it is still convenient though, since everything is hidden behind the adapters for 99% of the use cases. BotCommandAdapter now auto-infers the type based on the JDA command types, for the commands its responsible for (slash, user context, message context).

Checklist

@Zabuzard Zabuzard added enhancement New feature or request priority: normal labels Sep 29, 2022
@Zabuzard Zabuzard self-assigned this Sep 29, 2022
@Zabuzard Zabuzard requested review from a team as code owners September 29, 2022 07:35
@Zabuzard
Copy link
Member Author

(FYI @SquidXTV )

@Zabuzard Zabuzard force-pushed the feature/simplify_user_interactor_without_adapter branch from d6050af to f9fa20d Compare September 30, 2022 13:16
@Zabuzard Zabuzard requested a review from Tais993 September 30, 2022 13:21
@Zabuzard Zabuzard force-pushed the feature/simplify_user_interactor_without_adapter branch from f9fa20d to de388e7 Compare October 4, 2022 10:20
Copy link
Member

@Tais993 Tais993 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with the prefix enum before was that the component ID generation needed the prefixed name.

But that still is the case, so what have you actually changed?

@Zabuzard Zabuzard requested a review from Tais993 October 7, 2022 08:07
@Zabuzard Zabuzard force-pushed the feature/simplify_user_interactor_without_adapter branch from ae179f9 to 0f0832c Compare October 8, 2022 19:19
@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 8, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

@Zabuzard Zabuzard merged commit 7d9f16e into develop Oct 13, 2022
@Zabuzard Zabuzard deleted the feature/simplify_user_interactor_without_adapter branch October 13, 2022 08:01
@Zabuzard Zabuzard mentioned this pull request Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request priority: normal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants