All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.alexpado.jda.interactions.impl.handlers.SinkResponseHandler Maven / Gradle / Ivy

package fr.alexpado.jda.interactions.impl.handlers;

import fr.alexpado.jda.interactions.entities.DispatchEvent;
import fr.alexpado.jda.interactions.interfaces.interactions.InteractionResponseHandler;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.interactions.Interaction;
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
import net.dv8tion.jda.api.interactions.commands.CommandAutoCompleteInteraction;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import net.dv8tion.jda.api.interactions.components.buttons.ButtonInteraction;
import org.jetbrains.annotations.Nullable;

/**
 * Class implementing the {@link InteractionResponseHandler} where its sole purpose is to handle null-responses.
 * 

* Although discouraged, null-responses are valid but will provide no feedback to the user. *

* This may even be more problematic when the response is from an {@link SlashCommandInteraction} where the user expect * an answer explicitly, just as Discord, so a default response will be sent instead. */ public class SinkResponseHandler implements InteractionResponseHandler { /** * Check if this {@link InteractionResponseHandler} can handle the provided response. * * @param event * The {@link DispatchEvent} source of the response. * @param response * The object representing the response given by an interaction. * * @return True if able to handle, false otherwise. */ @Override public boolean canHandle(DispatchEvent event, @Nullable Object response) { return response == null; } /** * Handle the response resulting from the {@link DispatchEvent} event provided. * * @param event * The {@link DispatchEvent} source of the response. * @param response * The {@link Object} to handle. */ @Override public void handleResponse(DispatchEvent event, @Nullable Object response) { //noinspection ChainOfInstanceofChecks if (event.getInteraction() instanceof SlashCommandInteraction slash) { MessageBuilder builder = new MessageBuilder(); builder.setContent("*Nothing to display*"); this.answer(slash, builder.build()); } else if (event.getInteraction() instanceof ButtonInteraction button) { if (!button.isAcknowledged()) { button.deferReply().complete(); } } else if (event.getInteraction() instanceof CommandAutoCompleteInteraction auto) { auto.replyChoices().complete(); } } private void answer(T interaction, Message embed) { if (interaction.isAcknowledged()) { interaction.getHook().editOriginal(embed).complete(); } else { interaction.reply(embed).setEphemeral(true).complete(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy