Introduce discrete event handlers (`InteractionHandlers`, `GuildLifecycleHandler`, `MessageReceivedHandler`, and more) to replace centralized logic in `EventsHandler`. Simplify handler registration, improve separation of concerns, and enable better testability. Added unit tests to verify isolated execution and failure resilience of handlers.
61 lines
2.4 KiB
Java
61 lines
2.4 KiB
Java
package me.bobert.netneutrality.hooks;
|
|
|
|
/*
|
|
* Copyright (c) anthorob
|
|
*
|
|
* https://discord.gg/qUa6zNFZsd
|
|
*/
|
|
|
|
import me.bobert.netneutrality.context.command.MessageMenuContext;
|
|
import me.bobert.netneutrality.context.command.SlashCommandContext;
|
|
import me.bobert.netneutrality.context.command.UserMenuContext;
|
|
import me.bobert.netneutrality.context.tasks.ButtonTaskContext;
|
|
import me.bobert.netneutrality.context.tasks.EmojiTaskContext;
|
|
import me.bobert.netneutrality.context.tasks.ModalTaskContext;
|
|
import me.bobert.netneutrality.core.context.Context;
|
|
import me.bobert.netneutrality.utils.SettingsProvider;
|
|
import net.dv8tion.jda.api.events.Event;
|
|
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
|
|
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
|
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
|
import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent;
|
|
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
|
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
|
|
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.function.Function;
|
|
|
|
/**
|
|
* Slash, menu, modal, button and reaction events, each mapped to its context.
|
|
*/
|
|
public final class InteractionHandlers {
|
|
private InteractionHandlers() {
|
|
}
|
|
|
|
public static List<EventListenerHandler<?>> create(SettingsProvider settings) {
|
|
Objects.requireNonNull(settings, "settings");
|
|
return List.of(
|
|
of(UserContextInteractionEvent.class, e -> new UserMenuContext(e, settings)),
|
|
of(MessageContextInteractionEvent.class, e -> new MessageMenuContext(e, settings)),
|
|
of(SlashCommandInteractionEvent.class, e -> new SlashCommandContext(e, settings)),
|
|
of(ModalInteractionEvent.class, ModalTaskContext::new),
|
|
of(ButtonInteractionEvent.class, ButtonTaskContext::new),
|
|
of(MessageReactionAddEvent.class, EmojiTaskContext::new));
|
|
}
|
|
|
|
private static <T extends Event> ContextEventHandler<T> of(Class<T> type, Function<T, Context> mapper) {
|
|
return new ContextEventHandler<>() {
|
|
@Override
|
|
public Class<T> eventType() {
|
|
return type;
|
|
}
|
|
|
|
@Override
|
|
public Context toContext(T event) {
|
|
return mapper.apply(event);
|
|
}
|
|
};
|
|
}
|
|
}
|