Invoke method on bean for SlashCommand

java-rewrite
Hammy 3 years ago
parent cfef147fe7
commit 3204bff58d

@ -3,19 +3,21 @@ package me.goudham.command;
import io.micronaut.core.annotation.Introspected; import io.micronaut.core.annotation.Introspected;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import jakarta.inject.Singleton; import jakarta.inject.Singleton;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import me.goudham.bot.command.ISlashCommand;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
import net.dv8tion.jda.internal.utils.tuple.Pair;
@Singleton @Singleton
@Introspected @Introspected
public class SlashCommandManager implements CommandManager { public class SlashCommandManager implements CommandManager {
private final Map<String, ISlashCommand> commandMap = new HashMap<>(); private final Map<String, Pair<Object, Method>> commandMap = new HashMap<>();
private final CommandLoader commandLoader; private final CommandLoader commandLoader;
@Inject @Inject
@ -32,8 +34,15 @@ public class SlashCommandManager implements CommandManager {
@Override @Override
public void handleSlashCommandEvent(SlashCommandEvent slashCommandEvent) { public void handleSlashCommandEvent(SlashCommandEvent slashCommandEvent) {
String name = slashCommandEvent.getName(); String commandPath = slashCommandEvent.getCommandPath();
ISlashCommand slashCommand = commandMap.get(name); Pair<Object, Method> slashCommandPair = commandMap.get(commandPath);
slashCommand.handle(slashCommandEvent); Object bean = slashCommandPair.getLeft();
Method method = slashCommandPair.getRight();
try {
method.invoke(bean, slashCommandEvent);
} catch (IllegalAccessException | InvocationTargetException exp) {
exp.printStackTrace();
}
} }
} }

Loading…
Cancel
Save