Skip to content

Commit 7d9f16e

Browse files
authored
Added ComponentIdInteractor delegate (#588)
* Added ComponentIdInteractor as delegate * use delegate in adapter * minor adjustment * renamed getType() to getInteractionType() * CR improvements (Tais) * spotless
1 parent e4d428d commit 7d9f16e

File tree

13 files changed

+241
-254
lines changed

13 files changed

+241
-254
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/BotCommand.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
44
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
5-
import net.dv8tion.jda.api.interactions.commands.Command;
65
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
76

87
import java.util.List;
@@ -25,16 +24,6 @@
2524
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
2625
*/
2726
public interface BotCommand extends UserInteractor {
28-
29-
/**
30-
* Gets the type of this command.
31-
* <p>
32-
* After registration of the command, the type must not change anymore.
33-
*
34-
* @return the type of the command
35-
*/
36-
Command.Type getType();
37-
3827
/**
3928
* Gets the visibility of this command.
4029
* <p>

application/src/main/java/org/togetherjava/tjbot/commands/BotCommandAdapter.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
88
import org.jetbrains.annotations.Contract;
99

10-
import org.togetherjava.tjbot.commands.componentids.ComponentId;
1110
import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator;
11+
import org.togetherjava.tjbot.commands.componentids.ComponentIdInteractor;
1212
import org.togetherjava.tjbot.commands.componentids.Lifespan;
1313

14-
import java.util.Arrays;
1514
import java.util.List;
1615
import java.util.Objects;
1716

@@ -24,26 +23,23 @@
2423
* {@link #onSelectionMenu(SelectMenuInteractionEvent, List)} can be overridden if desired. The
2524
* default implementation is empty, the adapter will not react to such events.
2625
* <p>
27-
* <p>
2826
* The adapter manages some getters for you, you've to create the {@link CommandData} yourself. See
2927
* {@link #BotCommandAdapter(CommandData, CommandVisibility)}} for more info on that. Minimal
3028
* modifications can be done on the {@link CommandData} returned by {@link #getData()}.
3129
* <p>
32-
* <p>
3330
* If implementations want to add buttons or selection menus, it is highly advised to use component
3431
* IDs generated by {@link #generateComponentId(String...)}, which will automatically create IDs
3532
* that are valid per {@link SlashCommand#onSlashCommand(SlashCommandInteractionEvent)}.
3633
* <p>
37-
* <p>
3834
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
3935
* Registration of commands can be done in {@link Features}.
4036
*/
4137
public abstract class BotCommandAdapter implements BotCommand {
4238
private final String name;
43-
private final Command.Type type;
39+
private final UserInteractionType interactionType;
4440
private final CommandVisibility visibility;
4541
private final CommandData data;
46-
private ComponentIdGenerator componentIdGenerator;
42+
private final ComponentIdInteractor componentIdInteractor;
4743

4844
/**
4945
* Creates a new adapter with the given data.
@@ -55,8 +51,19 @@ protected BotCommandAdapter(CommandData data, CommandVisibility visibility) {
5551
this.data = data.setGuildOnly(visibility == CommandVisibility.GUILD);
5652
this.visibility = Objects.requireNonNull(visibility, "The visibility shouldn't be null");
5753

58-
this.name = data.getName();
59-
this.type = data.getType();
54+
name = data.getName();
55+
interactionType = commandTypeToInteractionType(data.getType());
56+
componentIdInteractor = new ComponentIdInteractor(interactionType, name);
57+
}
58+
59+
private static UserInteractionType commandTypeToInteractionType(Command.Type type) {
60+
return switch (type) {
61+
case SLASH -> UserInteractionType.SLASH_COMMAND;
62+
case USER -> UserInteractionType.USER_CONTEXT_COMMAND;
63+
case MESSAGE -> UserInteractionType.MESSAGE_CONTEXT_COMMAND;
64+
case UNKNOWN -> throw new IllegalArgumentException(
65+
"Can not infer the type of user interaction for this command, it is unknown.");
66+
};
6067
}
6168

6269
@Override
@@ -65,8 +72,8 @@ public final String getName() {
6572
}
6673

6774
@Override
68-
public Command.Type getType() {
69-
return type;
75+
public final UserInteractionType getInteractionType() {
76+
return interactionType;
7077
}
7178

7279
@Override
@@ -82,8 +89,7 @@ public CommandData getData() {
8289
@Override
8390
@Contract(mutates = "this")
8491
public final void acceptComponentIdGenerator(ComponentIdGenerator generator) {
85-
componentIdGenerator =
86-
Objects.requireNonNull(generator, "The given generator cannot be null");
92+
componentIdInteractor.acceptComponentIdGenerator(generator);
8793
}
8894

8995
@SuppressWarnings("NoopMethodInAbstractClass")
@@ -114,7 +120,7 @@ public void onSelectionMenu(SelectMenuInteractionEvent event, List<String> args)
114120
*/
115121
@SuppressWarnings("OverloadedVarargsMethod")
116122
protected final String generateComponentId(String... args) {
117-
return generateComponentId(Lifespan.REGULAR, args);
123+
return componentIdInteractor.generateComponentId(args);
118124
}
119125

120126
/**
@@ -131,8 +137,6 @@ protected final String generateComponentId(String... args) {
131137
*/
132138
@SuppressWarnings({"OverloadedVarargsMethod", "WeakerAccess"})
133139
protected final String generateComponentId(Lifespan lifespan, String... args) {
134-
return componentIdGenerator
135-
.generate(new ComponentId(UserInteractorPrefix.getPrefixedNameFromInstance(this),
136-
Arrays.asList(args)), lifespan);
140+
return componentIdInteractor.generateComponentId(lifespan, args);
137141
}
138142
}

application/src/main/java/org/togetherjava/tjbot/commands/MessageContextCommand.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
* {@link BotCommandAdapter} available that implemented most methods already. A new command can then
2323
* be registered by adding it to {@link Features}.
2424
* <p>
25-
* <p>
2625
* Context commands can either be visible globally in Discord or just to specific guilds. Minor
2726
* adjustments can be made via {@link CommandData}, which is then to be returned by
2827
* {@link #getData()} where the system will then pick it up from.
@@ -32,11 +31,9 @@
3231
* ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
3332
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
3433
* <p>
35-
* <p>
3634
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
3735
*/
3836
public interface MessageContextCommand extends BotCommand {
39-
4037
/**
4138
* Triggered by the core system when a message context-command corresponding to this
4239
* implementation (based on {@link #getData()}) has been triggered.

application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
4141
*/
4242
public interface SlashCommand extends BotCommand {
43-
4443
/**
4544
* Gets the description of the command.
4645
* <p>

application/src/main/java/org/togetherjava/tjbot/commands/UserContextCommand.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* {@link BotCommandAdapter} available that implemented most methods already. A new command can then
2424
* be registered by adding it to {@link Features}.
2525
* <p>
26-
* <p>
2726
* Context commands can either be visible globally in Discord or just to specific guilds. Minor
2827
* adjustments can be made via {@link CommandData}, which is then to be returned by
2928
* {@link #getData()} where the system will then pick it up from.
@@ -33,11 +32,9 @@
3332
* ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
3433
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
3534
* <p>
36-
* <p>
3735
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
3836
*/
3937
public interface UserContextCommand extends BotCommand {
40-
4138
/**
4239
* Triggered by the core system when a user context-command corresponding to this implementation
4340
* (based on {@link #getData()}) has been triggered.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.togetherjava.tjbot.commands;
2+
3+
/**
4+
* Different types of user interactions.
5+
* <p>
6+
* Also allows user interactors of different type to have the same name.
7+
*/
8+
public enum UserInteractionType {
9+
/**
10+
* User types a command leaded by a slash, such as {@code /ping}.
11+
*/
12+
SLASH_COMMAND(SlashCommand.class, "s-"),
13+
/**
14+
* User right-clicks a message and selects a command.
15+
*/
16+
MESSAGE_CONTEXT_COMMAND(MessageContextCommand.class, "mc-"),
17+
/**
18+
* User right-clicks a user and selects a command.
19+
*/
20+
USER_CONTEXT_COMMAND(UserContextCommand.class, "uc-"),
21+
/**
22+
* Other types of interactions, for example a routine that offers buttons to click on.
23+
*/
24+
OTHER(UserInteractor.class, "");
25+
26+
private final Class<? extends UserInteractor> classType;
27+
private final String prefix;
28+
29+
UserInteractionType(Class<? extends UserInteractor> classType, final String prefix) {
30+
this.classType = classType;
31+
this.prefix = prefix;
32+
}
33+
34+
/**
35+
* The prefix for the command
36+
*
37+
* @return the command's prefix
38+
*/
39+
public String getPrefix() {
40+
return prefix;
41+
}
42+
43+
/**
44+
* Returns the name, attached with the prefix in front of it.
45+
*
46+
* @param name the name
47+
* @return the name, with the prefix in front of it.
48+
*/
49+
public String getPrefixedName(String name) {
50+
return prefix + name;
51+
}
52+
53+
/**
54+
* The class type that should receive the prefix
55+
*
56+
* @return a {@link Class} instance of the type
57+
*/
58+
public Class<? extends UserInteractor> getClassType() {
59+
return classType;
60+
}
61+
}

application/src/main/java/org/togetherjava/tjbot/commands/UserInteractor.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,31 @@
1616
* <p>
1717
* An interactor can react to button clicks and selection menu actions. This is done based on the
1818
* given {@link #getName()}, because of this names have to be unique. But, names can be complicated
19-
* if their type is different, all the types can be seen in {@link UserInteractorPrefix}
19+
* if their type is different, all the types can be seen in {@link UserInteractionType}
2020
*/
2121
public interface UserInteractor extends Feature {
2222

2323
/**
2424
* Gets the name of the interactor.
2525
* <p>
26-
* You cannot start the name with any of the prefixes found in {@link UserInteractorPrefix}
26+
* You cannot start the name with any of the prefixes found in {@link UserInteractionType}
2727
* <p>
2828
* After registration of the interactor, the name must not change anymore.
2929
*
3030
* @return the name of the interactor
3131
*/
3232
String getName();
3333

34+
35+
/**
36+
* Gets the type of interactors this interactor allows.
37+
* <p>
38+
* After registration of the interactor, the type must not change anymore.
39+
*
40+
* @return the type of the interaction allowed by this interactor
41+
*/
42+
UserInteractionType getInteractionType();
43+
3444
/**
3545
* Triggered by the core system when a button corresponding to this implementation (based on
3646
* {@link #getName()}) has been clicked.

application/src/main/java/org/togetherjava/tjbot/commands/UserInteractorPrefix.java

Lines changed: 0 additions & 118 deletions
This file was deleted.

0 commit comments

Comments
 (0)