Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* <p>
* After registration, the system will notify a command whenever one of its corresponding command
* method, buttons ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
* ({@link #onSelectMenuSelection(SelectMenuInteractionEvent, List)}) have been triggered.
* <p>
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.togetherjava.tjbot.commands;

import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
Expand All @@ -20,8 +21,8 @@
* {@link Features}.
* <p>
* Further, {@link #onButtonClick(ButtonInteractionEvent, List)} and
* {@link #onSelectionMenu(SelectMenuInteractionEvent, List)} can be overridden if desired. The
* default implementation is empty, the adapter will not react to such events.
* {@link #onSelectMenuSelection(SelectMenuInteractionEvent, List)} can be overridden if desired.
* The default implementation is empty, the adapter will not react to such events.
* <p>
* The adapter manages some getters for you, you've to create the {@link CommandData} yourself. See
* {@link #BotCommandAdapter(CommandData, CommandVisibility)}} for more info on that. Minimal
Expand Down Expand Up @@ -100,7 +101,13 @@ public void onButtonClick(ButtonInteractionEvent event, List<String> args) {

@SuppressWarnings("NoopMethodInAbstractClass")
@Override
public void onSelectionMenu(SelectMenuInteractionEvent event, List<String> args) {
public void onSelectMenuSelection(SelectMenuInteractionEvent event, List<String> args) {
// Adapter does not react by default, subclasses may change this behavior
}

@SuppressWarnings("NoopMethodInAbstractClass")
@Override
public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
// Adapter does not react by default, subclasses may change this behavior
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* After registration, the system will notify a command whenever one of its corresponding message
* context-commands ({@link #onMessageContext(MessageContextInteractionEvent)}), buttons
* ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
* ({@link #onSelectMenuSelection(SelectMenuInteractionEvent, List)}) have been triggered.
* <p>
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* After registration, the system will notify a command whenever one of its corresponding slash
* commands ({@link #onSlashCommand(SlashCommandInteractionEvent)}), buttons
* ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
* ({@link #onSelectMenuSelection(SelectMenuInteractionEvent, List)}) have been triggered.
* <p>
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* adding it to {@link Features}.
* <p>
* Further, {@link #onButtonClick(ButtonInteractionEvent, List)} and
* {@link #onSelectionMenu(SelectMenuInteractionEvent, List)} can be overridden if desired. The
* default implementation is empty, the adapter will not react to such events.
* {@link #onSelectMenuSelection(SelectMenuInteractionEvent, List)} can be overridden if desired.
* The default implementation is empty, the adapter will not react to such events.
* <p>
* The adapter manages all command related data itself, which can be provided during construction
* (see {@link #SlashCommandAdapter(String, String, CommandVisibility)}). In order to add options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* After registration, the system will notify a command whenever one of its corresponding user
* context-commands ({@link #onUserContext(UserContextInteractionEvent)}), buttons
* ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
* ({@link #onSelectMenuSelection(SelectMenuInteractionEvent, List)}) have been triggered.
* <p>
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.togetherjava.tjbot.commands;

import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
Expand Down Expand Up @@ -75,7 +76,26 @@ public interface UserInteractor extends Feature {
* {@link SlashCommand#onSlashCommand(SlashCommandInteractionEvent)} for details on how
* these are created
*/
void onSelectionMenu(SelectMenuInteractionEvent event, List<String> args);
void onSelectMenuSelection(SelectMenuInteractionEvent event, List<String> args);


/**
* Triggered by the core system when a modal corresponding to this implementation (based on
* {@link #getName()}) has been clicked.
* <p>
* This method may be called multithreaded. In particular, there are no guarantees that it will
* be executed on the same thread repeatedly or on the same thread that other event methods have
* been called on.
* <p>
* Details are available in the given event and the event also enables implementations to
* respond to it.
*
* @param event the event that triggered this
* @param args the arguments transported with the modal, see
* {@link SlashCommand#onSlashCommand(SlashCommandInteractionEvent)} for details on how
* these are created
*/
void onModalSubmitted(ModalInteractionEvent event, List<String> args);

/**
* Triggered by the core system during its setup phase. It will provide the command a component
Expand All @@ -90,7 +110,7 @@ public interface UserInteractor extends Feature {
* given to this method during system setup. The required {@link ComponentId} instance accepts
* optional extra arguments, which, if provided, can be picked up during the corresponding event
* (see {@link #onButtonClick(ButtonInteractionEvent, List)},
* {@link #onSelectionMenu(SelectMenuInteractionEvent, List)}).
* {@link #onSelectMenuSelection(SelectMenuInteractionEvent, List)}).
* <p>
* Alternatively, if {@link BotCommandAdapter} has been extended, it also offers a handy
* {@link BotCommandAdapter#generateComponentId(String...)} method to ease the flow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private static SelectOption mapToSelectOption(Role role) {
}

@Override
public void onSelectionMenu(SelectMenuInteractionEvent event, List<String> args) {
public void onSelectMenuSelection(SelectMenuInteractionEvent event, List<String> args) {
Guild guild = event.getGuild();
List<Role> selectedRoles = event.getSelectedOptions()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
Expand Down Expand Up @@ -97,7 +98,12 @@ public UserInteractionType getInteractionType() {
}

@Override
public void onSelectionMenu(SelectMenuInteractionEvent event, List<String> args) {
public void onSelectMenuSelection(SelectMenuInteractionEvent event, List<String> args) {
throw new UnsupportedOperationException("Not used");
}

@Override
public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
throw new UnsupportedOperationException("Not used");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
Expand All @@ -11,6 +12,7 @@
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
import org.jetbrains.annotations.Unmodifiable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -256,7 +258,29 @@ public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
logger.debug("Received selection menu event '{}' (#{}) on guild '{}'",
event.getComponentId(), event.getId(), event.getGuild());
COMMAND_SERVICE
.execute(() -> forwardComponentCommand(event, UserInteractor::onSelectionMenu));
.execute(() -> forwardComponentCommand(event, UserInteractor::onSelectMenuSelection));
}

@Override
public void onModalInteraction(final ModalInteractionEvent event) {
logger.debug("Received modal event '{}' (#{}) on guild '{}'", event.getModalId(),
event.getId(), event.getGuild());
COMMAND_SERVICE.execute(() -> {
Optional<ComponentId> componentIdOptional =
handleParseComponentId(event, event.getModalId());

if (componentIdOptional.isEmpty()) {
return;
}

ComponentId componentId = componentIdOptional.orElseThrow();

UserInteractor interactor =
requireUserInteractor(componentId.userInteractorName(), UserInteractor.class);
logger.trace("Routing a modal event with id '{}' back to user interactor '{}'",
event.getModalId(), interactor.getName());
interactor.onModalSubmitted(event, componentId.elements());
});
}

@Override
Expand All @@ -281,6 +305,42 @@ public void onUserContextInteraction(final UserContextInteractionEvent event) {
UserContextCommand.class).onUserContext(event));
}

/**
* Returns the {@link ComponentId} instace of the given componentId. If the component has
* expired, the user gets a message stating that.
*
* @param event the {@link IReplyCallback event} to reply to
* @param componentId the component's ID
* @return the relating {@link ComponentId}
*/
public Optional<ComponentId> handleParseComponentId(IReplyCallback event, String componentId) {
Optional<ComponentId> componentIdOpt;
try {
componentIdOpt = componentIdParser.parse(componentId);
} catch (InvalidComponentIdFormatException | IllegalArgumentException e) {
logger.error(
"""
Unable to route event (#{}) back to its corresponding {}}.
The component ID was in an unexpected format. All button and menu events have to use a component ID created in a specific format
(refer to the documentation of {}}). Component ID was: {}
{}
""",
event.getId(), UserInteractor.class.getSimpleName(),
UserInteractor.class.getSimpleName(), componentId, e);
// Unable to forward, simply fade out the event
return Optional.empty();
}

if (componentIdOpt.isEmpty()) {
logger.warn("The event (#{}) has an expired component ID, which was: {}.",
event.getId(), componentId);
event.reply("Sorry, but this event has expired. You can not use it anymore.")
.setEphemeral(true)
.queue();
}

return componentIdOpt;
}

/**
* Forwards the given component event to the associated user interactor.
Expand All @@ -300,27 +360,15 @@ public void onUserContextInteraction(final UserContextInteractionEvent event) {
*/
private <T extends ComponentInteraction> void forwardComponentCommand(T event,
TriConsumer<? super UserInteractor, ? super T, ? super List<String>> interactorArgumentConsumer) {
Optional<ComponentId> componentIdOpt;
try {
componentIdOpt = componentIdParser.parse(event.getComponentId());
} catch (InvalidComponentIdFormatException e) {
logger
.error("Unable to route event (#{}) back to its corresponding user interactor. The component ID was in an unexpected format."
+ " All button and menu events have to use a component ID created in a specific format"
+ " (refer to the documentation of UserInteractor). Component ID was: {}",
event.getId(), event.getComponentId(), e);
// Unable to forward, simply fade out the event
return;
}
if (componentIdOpt.isEmpty()) {
logger.warn("The event (#{}) has an expired component ID, which was: {}.",
event.getId(), event.getComponentId());
event.reply("Sorry, but this event has expired. You can not use it anymore.")
.setEphemeral(true)
.queue();

Optional<ComponentId> componentIdOptional =
handleParseComponentId(event, event.getComponentId());

if (componentIdOptional.isEmpty()) {
return;
}
ComponentId componentId = componentIdOpt.orElseThrow();

ComponentId componentId = componentIdOptional.orElseThrow();

UserInteractor interactor =
requireUserInteractor(componentId.userInteractorName(), UserInteractor.class);
Expand All @@ -334,8 +382,8 @@ private <T extends ComponentInteraction> void forwardComponentCommand(T event,
*
* @param prefixedName the prefixed name of the interactor
* @param typeToken a token of the type to expect
* @return the user interactor with the given name
* @param <T> the type to expect the user interactor to be of
* @return the user interactor with the given name
*/
private <T extends UserInteractor> T requireUserInteractor(String prefixedName,
Class<T> typeToken) {
Expand Down