Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package fr.alexpado.jda.interactions.impl;
import fr.alexpado.jda.interactions.annotations.Param;
import fr.alexpado.jda.interactions.entities.DispatchEvent;
import fr.alexpado.jda.interactions.exceptions.InteractionDeclarationException;
import fr.alexpado.jda.interactions.exceptions.InteractionInjectionException;
import fr.alexpado.jda.interactions.interfaces.interactions.Injection;
import fr.alexpado.jda.interactions.interfaces.interactions.InteractionResponseHandler;
import fr.alexpado.jda.interactions.interfaces.interactions.InteractionTarget;
import fr.alexpado.jda.interactions.interfaces.interactions.MetaContainer;
import fr.alexpado.jda.interactions.meta.InteractionMeta;
import net.dv8tion.jda.api.interactions.Interaction;
import net.dv8tion.jda.api.interactions.callbacks.IMessageEditCallback;
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
/**
* Class implementing the default behavior of an {@link InteractionTarget}.
*
* @param
* The type of the interaction
*/
public class InteractionTargetImpl implements InteractionTarget, MetaContainer {
private static final Map, Class>> PRIMITIVE_MAP = new HashMap<>() {{
this.put(long.class, Long.class);
this.put(boolean.class, Boolean.class);
this.put(double.class, Double.class);
}};
private final static Logger LOGGER = LoggerFactory.getLogger(InteractionTargetImpl.class);
private final Object instance;
private final Method method;
private final InteractionMeta meta;
/**
* Create a new {@link InteractionTargetImpl} implementation instance.
*
* @param instance
* The instance object within which the interaction exists.
* @param method
* The method to use when executing the interaction.
* @param meta
* The meta representing this {@link InteractionTargetImpl}.
*/
public InteractionTargetImpl(Object instance, Method method, InteractionMeta meta) {
this.instance = instance;
this.method = method;
this.meta = meta;
}
/**
* Run this {@link InteractionTarget}.
*
* @param event
* The event responsible for this execution.
* @param mapping
* Map of dependencies used for the parameter injection.
*
* @return An {@link Object} representing the result of the execution. The result can be used directly without any
* result handler.
*
* @throws Exception
* If the execution could not occur, or due to an userland exception defined in the interaction.
* @see InteractionResponseHandler
*/
@Override
public Object execute(DispatchEvent event, Map, Injection, ?>> mapping) throws Exception {
if (this.getMeta().isDeferred()) {
event.getTimedAction().action("deferring", "Deferring the interaction");
boolean reply = this.getMeta().shouldReply();
Interaction interaction = event.getInteraction();
if (reply && interaction instanceof IReplyCallback callback) {
callback.deferReply(this.getMeta().isHidden()).complete();
} else if (!reply && interaction instanceof IMessageEditCallback callback) {
callback.deferEdit().complete();
} else {
throw new UnsupportedOperationException("Couldn't pre-handle deferred request");
}
event.getTimedAction().endAction();
}
event.getTimedAction().action("injection", "Injecting parameters");
Collection