Skip to content

Commit b9b047a

Browse files
committed
Adding UserInteractor to the core system
it basically moves getName(), onButtonClick() and onSelectionMenu() from SlashCommand one level higher - so that also non-slash-commands can use it
1 parent e5cd2c2 commit b9b047a

File tree

7 files changed

+156
-116
lines changed

7 files changed

+156
-116
lines changed
Lines changed: 16 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package org.togetherjava.tjbot.commands;
22

33
import net.dv8tion.jda.api.entities.Emoji;
4-
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
5-
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
6-
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
4+
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
5+
import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent;
6+
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
77
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
8-
import net.dv8tion.jda.api.interactions.commands.build.Commands;
9-
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
8+
import net.dv8tion.jda.api.interactions.components.ButtonStyle;
109
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
11-
import net.dv8tion.jda.api.interactions.components.buttons.Button;
12-
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
1310
import org.jetbrains.annotations.NotNull;
1411
import org.togetherjava.tjbot.commands.componentids.ComponentId;
1512
import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator;
@@ -31,32 +28,19 @@
3128
* is then to be returned by {@link #getData()} where the system will then pick it up from.
3229
* <p>
3330
* After registration, the system will notify a command whenever one of its corresponding slash
34-
* commands ({@link #onSlashCommand(SlashCommandInteractionEvent)}), buttons
35-
* ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
36-
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
31+
* commands ({@link #onSlashCommand(SlashCommandEvent)}), buttons
32+
* ({@link #onButtonClick(ButtonClickEvent, List)}) or menus
33+
* ({@link #onSelectionMenu(SelectionMenuEvent, List)}) have been triggered.
3734
* <p>
3835
* <p>
3936
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
4037
*/
41-
public interface SlashCommand extends Feature {
42-
43-
/**
44-
* Gets the name of the command.
45-
* <p>
46-
* Requirements for this are documented in {@link Commands#slash(String, String)}.
47-
* <p>
48-
* <p>
49-
* After registration of the command, the name must not change anymore.
50-
*
51-
* @return the name of the command
52-
*/
53-
@NotNull
54-
String getName();
38+
public interface SlashCommand extends UserInteractor {
5539

5640
/**
5741
* Gets the description of the command.
5842
* <p>
59-
* Requirements for this are documented in {@link Commands#slash(String, String)}.
43+
* Requirements for this are documented in {@link CommandData#CommandData(String, String)}.
6044
* <p>
6145
* <p>
6246
* After registration of the command, the description must not change anymore.
@@ -92,7 +76,7 @@ public interface SlashCommand extends Feature {
9276
* @return the command data of this command
9377
*/
9478
@NotNull
95-
SlashCommandData getData();
79+
CommandData getData();
9680

9781
/**
9882
* Triggered by the core system when a slash command corresponding to this implementation (based
@@ -107,9 +91,9 @@ public interface SlashCommand extends Feature {
10791
* <p>
10892
* Buttons or menus have to be created with a component ID (see
10993
* {@link ComponentInteraction#getComponentId()},
110-
* {@link Button#of(ButtonStyle, String, Emoji)}}) in a very specific format, otherwise the core
111-
* system will fail to identify the command that corresponded to the button or menu click event
112-
* and is unable to route it back.
94+
* {@link net.dv8tion.jda.api.interactions.components.Button#of(ButtonStyle, String, Emoji)}) in
95+
* a very specific format, otherwise the core system will fail to identify the command that
96+
* corresponded to the button or menu click event and is unable to route it back.
11397
* <p>
11498
* The component ID has to be a UUID-string (see {@link java.util.UUID}), which is associated to
11599
* a specific database entry, containing meta information about the command being executed. Such
@@ -118,8 +102,8 @@ public interface SlashCommand extends Feature {
118102
* given to {@link #acceptComponentIdGenerator(ComponentIdGenerator)} during system setup. The
119103
* required {@link ComponentId} instance accepts optional extra arguments, which, if provided,
120104
* can be picked up during the corresponding event (see
121-
* {@link #onButtonClick(ButtonInteractionEvent, List)},
122-
* {@link #onSelectionMenu(SelectMenuInteractionEvent, List)}).
105+
* {@link #onButtonClick(ButtonClickEvent, List)},
106+
* {@link #onSelectionMenu(SelectionMenuEvent, List)}).
123107
* <p>
124108
* Alternatively, if {@link SlashCommandAdapter} has been extended, it also offers a handy
125109
* {@link SlashCommandAdapter#generateComponentId(String...)} method to ease the flow.
@@ -132,57 +116,5 @@ public interface SlashCommand extends Feature {
132116
*
133117
* @param event the event that triggered this
134118
*/
135-
void onSlashCommand(@NotNull SlashCommandInteractionEvent event);
136-
137-
/**
138-
* Triggered by the core system when a button corresponding to this implementation (based on
139-
* {@link #getData()}) has been clicked.
140-
* <p>
141-
* This method may be called multi-threaded. In particular, there are no guarantees that it will
142-
* be executed on the same thread repeatedly or on the same thread that other event methods have
143-
* been called on.
144-
* <p>
145-
* Details are available in the given event and the event also enables implementations to
146-
* respond to it.
147-
* <p>
148-
* This method will be called in a multi-threaded context and the event may not be hold valid
149-
* forever.
150-
*
151-
* @param event the event that triggered this
152-
* @param args the arguments transported with the button, see
153-
* {@link #onSlashCommand(SlashCommandInteractionEvent)} for details on how these are
154-
* created
155-
*/
156-
void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List<String> args);
157-
158-
/**
159-
* Triggered by the core system when a selection menu corresponding to this implementation
160-
* (based on {@link #getData()}) has been clicked.
161-
* <p>
162-
* This method may be called multi-threaded. In particular, there are no guarantees that it will
163-
* be executed on the same thread repeatedly or on the same thread that other event methods have
164-
* been called on.
165-
* <p>
166-
* Details are available in the given event and the event also enables implementations to
167-
* respond to it.
168-
* <p>
169-
* This method will be called in a multi-threaded context and the event may not be hold valid
170-
* forever.
171-
*
172-
* @param event the event that triggered this
173-
* @param args the arguments transported with the selection menu, see
174-
* {@link #onSlashCommand(SlashCommandInteractionEvent)} for details on how these are
175-
* created
176-
*/
177-
void onSelectionMenu(@NotNull SelectMenuInteractionEvent event, @NotNull List<String> args);
178-
179-
/**
180-
* Triggered by the core system during its setup phase. It will provide the command a component
181-
* id generator through this method, which can be used to generate component ids, as used for
182-
* button or selection menus. See {@link #onSlashCommand(SlashCommandInteractionEvent)} for
183-
* details on how to use this.
184-
*
185-
* @param generator the provided component id generator
186-
*/
187-
void acceptComponentIdGenerator(@NotNull ComponentIdGenerator generator);
119+
void onSlashCommand(@NotNull SlashCommandEvent event);
188120
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.togetherjava.tjbot.commands;
2+
3+
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
4+
import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent;
5+
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
6+
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator;
9+
10+
import java.util.List;
11+
12+
/**
13+
* Represents a feature that can interact with users. The most important implementation is
14+
* {@link SlashCommand}.
15+
* <p>
16+
* An interactor must have a unique name and can react to button clicks and selection menu actions.
17+
*/
18+
public interface UserInteractor extends Feature {
19+
20+
/**
21+
* Gets the name of the interactor.
22+
* <p>
23+
* Requirements for this are documented in {@link CommandData#CommandData(String, String)}.
24+
* <p>
25+
* <p>
26+
* After registration of the interactor, the name must not change anymore.
27+
*
28+
* @return the name of the interactor
29+
*/
30+
@NotNull
31+
String getName();
32+
33+
/**
34+
* Triggered by the core system when a button corresponding to this implementation (based on
35+
* {@link #getName()}) has been clicked.
36+
* <p>
37+
* This method may be called multi-threaded. In particular, there are no guarantees that it will
38+
* be executed on the same thread repeatedly or on the same thread that other event methods have
39+
* been called on.
40+
* <p>
41+
* Details are available in the given event and the event also enables implementations to
42+
* respond to it.
43+
* <p>
44+
* This method will be called in a multi-threaded context and the event may not be hold valid
45+
* forever.
46+
*
47+
* @param event the event that triggered this
48+
* @param args the arguments transported with the button, see
49+
* {@link SlashCommand#onSlashCommand(SlashCommandEvent)} for details on how these are
50+
* created
51+
*/
52+
void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List<String> args);
53+
54+
/**
55+
* Triggered by the core system when a selection menu corresponding to this implementation
56+
* (based on {@link #getName()}) has been clicked.
57+
* <p>
58+
* This method may be called multi-threaded. In particular, there are no guarantees that it will
59+
* be executed on the same thread repeatedly or on the same thread that other event methods have
60+
* been called on.
61+
* <p>
62+
* Details are available in the given event and the event also enables implementations to
63+
* respond to it.
64+
* <p>
65+
* This method will be called in a multi-threaded context and the event may not be hold valid
66+
* forever.
67+
*
68+
* @param event the event that triggered this
69+
* @param args the arguments transported with the selection menu, see
70+
* {@link SlashCommand#onSlashCommand(SlashCommandEvent)} for details on how these are
71+
* created
72+
*/
73+
void onSelectionMenu(@NotNull SelectionMenuEvent event, @NotNull List<String> args);
74+
75+
/**
76+
* Triggered by the core system during its setup phase. It will provide the interactor a
77+
* component id generator through this method, which can be used to generate component ids, as
78+
* used for button or selection menus. See
79+
* {@link SlashCommand#onSlashCommand(SlashCommandEvent)} for details on how to use this.
80+
*
81+
* @param generator the provided component id generator
82+
*/
83+
void acceptComponentIdGenerator(@NotNull ComponentIdGenerator generator);
84+
}

application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentId.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* {@link org.togetherjava.tjbot.commands.SlashCommand#onSlashCommand(SlashCommandInteractionEvent)}
1010
* for its usages.
1111
*
12-
* @param commandName the name of the command that handles the event associated to this component
13-
* ID, when triggered
12+
* @param userInteractorName the name of the user interactor that handles the event associated to
13+
* this component ID, when triggered
1414
* @param elements the additional elements to carry along this component ID, empty if not desired
1515
*/
16-
public record ComponentId(@NotNull String commandName, @NotNull List<String> elements) {
16+
public record ComponentId(@NotNull String userInteractorName, @NotNull List<String> elements) {
1717
}

application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ private void evictDatabase() {
266266
recordToDelete.delete();
267267
evictedCounter.getAndIncrement();
268268
logger.debug(
269-
"Evicted component id with uuid '{}' from command '{}', last used '{}'",
270-
uuid, componentId.commandName(), lastUsed);
269+
"Evicted component id with uuid '{}' from user interactor '{}', last used '{}'",
270+
uuid, componentId.userInteractorName(), lastUsed);
271271

272272
// Remove them from the cache if still in there
273273
storeCache.invalidate(uuid);

0 commit comments

Comments
 (0)