|
|
@ -3,31 +3,37 @@ package me.goudham.command;
|
|
|
|
import io.micronaut.context.ApplicationContext;
|
|
|
|
import io.micronaut.context.ApplicationContext;
|
|
|
|
import io.micronaut.context.BeanContext;
|
|
|
|
import io.micronaut.context.BeanContext;
|
|
|
|
import io.micronaut.core.annotation.AnnotationValue;
|
|
|
|
import io.micronaut.core.annotation.AnnotationValue;
|
|
|
|
import io.micronaut.core.annotation.Introspected;
|
|
|
|
|
|
|
|
import io.micronaut.inject.BeanDefinition;
|
|
|
|
import io.micronaut.inject.BeanDefinition;
|
|
|
|
|
|
|
|
import io.micronaut.inject.qualifiers.Qualifiers;
|
|
|
|
import jakarta.inject.Inject;
|
|
|
|
import jakarta.inject.Inject;
|
|
|
|
import jakarta.inject.Singleton;
|
|
|
|
import jakarta.inject.Singleton;
|
|
|
|
import java.lang.annotation.Annotation;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collection;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.OptionalDouble;
|
|
|
|
import java.util.OptionalDouble;
|
|
|
|
import java.util.OptionalInt;
|
|
|
|
import java.util.OptionalInt;
|
|
|
|
import me.goudham.bot.command.ISlashCommand;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import me.goudham.command.annotation.Choice;
|
|
|
|
import me.goudham.command.annotation.Choice;
|
|
|
|
import me.goudham.command.annotation.Option;
|
|
|
|
import me.goudham.command.annotation.Option;
|
|
|
|
|
|
|
|
import me.goudham.command.annotation.SlashCommand;
|
|
|
|
import me.goudham.command.annotation.SubCommand;
|
|
|
|
import me.goudham.command.annotation.SubCommand;
|
|
|
|
import me.goudham.command.annotation.SubCommandGroup;
|
|
|
|
import me.goudham.command.annotation.SubCommandGroup;
|
|
|
|
|
|
|
|
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
|
|
|
|
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.Command;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData;
|
|
|
|
|
|
|
|
import net.dv8tion.jda.internal.utils.tuple.ImmutablePair;
|
|
|
|
|
|
|
|
import net.dv8tion.jda.internal.utils.tuple.Pair;
|
|
|
|
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
@Singleton
|
|
|
|
@Introspected
|
|
|
|
|
|
|
|
public class SlashCommandLoader implements CommandLoader {
|
|
|
|
public class SlashCommandLoader implements CommandLoader {
|
|
|
|
private final BeanContext beanContext;
|
|
|
|
private final BeanContext beanContext;
|
|
|
|
private final ApplicationContext applicationContext;
|
|
|
|
private final ApplicationContext applicationContext;
|
|
|
@ -40,80 +46,126 @@ public class SlashCommandLoader implements CommandLoader {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public List<CommandData> loadIntoMapAndReturnCommands(Map<String, ISlashCommand> commandMap) {
|
|
|
|
public List<CommandData> loadIntoMapAndReturnCommands(Map<String, Pair<Object, Method>> commandMap) {
|
|
|
|
Collection<BeanDefinition<ISlashCommand>> beanDefinitions = beanContext.getBeanDefinitions(ISlashCommand.class);
|
|
|
|
Collection<BeanDefinition<?>> slashCommandDefinitions = beanContext.getBeanDefinitions(Qualifiers.byStereotype(SlashCommand.class));
|
|
|
|
|
|
|
|
Collection<BeanDefinition<?>> subCommandGroupDefinitions = beanContext.getBeanDefinitions(Qualifiers.byStereotype(SubCommandGroup.class));
|
|
|
|
|
|
|
|
Collection<BeanDefinition<?>> subCommandDefinitions = beanContext.getBeanDefinitions(Qualifiers.byStereotype(SubCommand.class));
|
|
|
|
|
|
|
|
Map<String, CommandData> commandDataMap = new HashMap<>();
|
|
|
|
List<CommandData> commandDataList = new ArrayList<>();
|
|
|
|
List<CommandData> commandDataList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
for (BeanDefinition<ISlashCommand> beanDefinition : beanDefinitions) {
|
|
|
|
for (BeanDefinition<?> slashCommandDefinition : slashCommandDefinitions) {
|
|
|
|
AnnotationValue<Annotation> slashCommand = beanDefinition.getDeclaredAnnotation("me.goudham.command.annotation.SlashCommand");
|
|
|
|
AnnotationValue<SlashCommand> slashCommand = slashCommandDefinition.getDeclaredAnnotation(SlashCommand.class);
|
|
|
|
if (slashCommand != null) {
|
|
|
|
if (slashCommand != null) {
|
|
|
|
String name = slashCommand.stringValue("name").orElseThrow();
|
|
|
|
String name = slashCommand.stringValue("name").orElseThrow();
|
|
|
|
String description = slashCommand.stringValue("description").orElseThrow();
|
|
|
|
String description = slashCommand.stringValue("description").orElseThrow();
|
|
|
|
boolean isVisible = slashCommand.booleanValue("isVisible").orElseThrow();
|
|
|
|
boolean isVisible = slashCommand.booleanValue("isVisible").orElseThrow();
|
|
|
|
|
|
|
|
String[] subCommandGroups = slashCommand.stringValues("subCommandGroups");
|
|
|
|
|
|
|
|
String[] subCommands = slashCommand.stringValues("subCommands");
|
|
|
|
|
|
|
|
|
|
|
|
CommandData commandData = new CommandData(name, description);
|
|
|
|
CommandData commandData = new CommandData(name, description);
|
|
|
|
commandData.setDefaultEnabled(isVisible);
|
|
|
|
commandData.setDefaultEnabled(isVisible);
|
|
|
|
|
|
|
|
|
|
|
|
List<SubcommandGroupData> subcommandGroupDataList = loadSubCommandGroups(slashCommand);
|
|
|
|
if (subCommandGroups.length == 0 && subCommands.length == 0) {
|
|
|
|
List<SubcommandData> subcommandData = loadSubCommands(slashCommand);
|
|
|
|
List<OptionData> optionData = loadOptions(slashCommand);
|
|
|
|
List<OptionData> optionData = loadOptions(slashCommand);
|
|
|
|
if (optionData != null) commandData.addOptions(optionData);
|
|
|
|
|
|
|
|
|
|
|
|
if (subcommandGroupDataList != null) commandData.addSubcommandGroups(subcommandGroupDataList);
|
|
|
|
storeIntoCommandMap(commandMap, slashCommandDefinition, name, "handle");
|
|
|
|
if (subcommandData != null) commandData.addSubcommands(subcommandData);
|
|
|
|
commandDataMap.put(name, commandData);
|
|
|
|
if (optionData != null) commandData.addOptions(optionData);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Arrays.stream(subCommandGroups)
|
|
|
|
|
|
|
|
.map(subCommandGroup -> name + "/" + subCommandGroup)
|
|
|
|
|
|
|
|
.forEach(subCommandGroupName -> commandDataMap.put(subCommandGroupName, commandData));
|
|
|
|
|
|
|
|
Arrays.stream(subCommands)
|
|
|
|
|
|
|
|
.map(subCommand -> name + "/" + subCommand)
|
|
|
|
|
|
|
|
.forEach(subCommandName -> commandDataMap.put(subCommandName, commandData));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
commandDataList.add(commandData);
|
|
|
|
commandDataList.add(commandData);
|
|
|
|
commandMap.put(name, applicationContext.getBean(beanDefinition));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException("Slash Command Annotation For " + beanDefinition + " Was Null");
|
|
|
|
throw new RuntimeException("Slash Command Annotation For " + slashCommandDefinition + " Was Null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return commandDataList;
|
|
|
|
for (BeanDefinition<?> subCommandGroupDefinition : subCommandGroupDefinitions) {
|
|
|
|
}
|
|
|
|
AnnotationValue<SubCommandGroup> subCommandGroup = subCommandGroupDefinition.getDeclaredAnnotation(SubCommandGroup.class);
|
|
|
|
|
|
|
|
if (subCommandGroup != null) {
|
|
|
|
private List<SubcommandGroupData> loadSubCommandGroups(AnnotationValue<?> slashCommand) {
|
|
|
|
String parent = subCommandGroup.stringValue("parent").orElseThrow();
|
|
|
|
if (slashCommand.contains("subCommandGroups")) {
|
|
|
|
|
|
|
|
List<SubcommandGroupData> subcommandGroupDataList = new ArrayList<>();
|
|
|
|
|
|
|
|
List<AnnotationValue<SubCommandGroup>> subCommandGroupAnnotations = slashCommand.getAnnotations("subCommandGroups", SubCommandGroup.class);
|
|
|
|
|
|
|
|
for (AnnotationValue<SubCommandGroup> subCommandGroup : subCommandGroupAnnotations) {
|
|
|
|
|
|
|
|
String name = subCommandGroup.stringValue("name").orElseThrow();
|
|
|
|
String name = subCommandGroup.stringValue("name").orElseThrow();
|
|
|
|
String description = subCommandGroup.stringValue("description").orElseThrow();
|
|
|
|
String description = subCommandGroup.stringValue("description").orElseThrow();
|
|
|
|
|
|
|
|
String commandPath = parent + "/" + name;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CommandData commandData = commandDataMap.get(commandPath);
|
|
|
|
SubcommandGroupData subcommandGroupData = new SubcommandGroupData(name, description);
|
|
|
|
SubcommandGroupData subcommandGroupData = new SubcommandGroupData(name, description);
|
|
|
|
List<SubcommandData> subcommandData = loadSubCommands(subCommandGroup);
|
|
|
|
|
|
|
|
if (subcommandData != null) subcommandGroupData.addSubcommands(subcommandData);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subcommandGroupDataList.add(subcommandGroupData);
|
|
|
|
Class<?> beanType = subCommandGroupDefinition.getBeanType();
|
|
|
|
}
|
|
|
|
List<SubcommandData> subCommandList = new ArrayList<>();
|
|
|
|
|
|
|
|
List<Method> subCommands = Arrays.stream(beanType.getDeclaredMethods())
|
|
|
|
|
|
|
|
.filter(method -> method.isAnnotationPresent(SubCommand.class))
|
|
|
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (Method subCommand : subCommands) {
|
|
|
|
|
|
|
|
SubCommand annotation = subCommand.getDeclaredAnnotation(SubCommand.class);
|
|
|
|
|
|
|
|
String subCommandName = annotation.name();
|
|
|
|
|
|
|
|
String subCommandDescription = annotation.description();
|
|
|
|
|
|
|
|
List<OptionData> optionDataList = Arrays.stream(annotation.options())
|
|
|
|
|
|
|
|
.map(option -> {
|
|
|
|
|
|
|
|
OptionData optionData = new OptionData(option.optionType(), option.name(), option.description(), option.isRequired());
|
|
|
|
|
|
|
|
List<Command.Choice> choiceDataList = Arrays.stream(option.choices())
|
|
|
|
|
|
|
|
.map(choice -> {
|
|
|
|
|
|
|
|
int intValue = (int) choice.intValue();
|
|
|
|
|
|
|
|
double doubleValue = choice.doubleValue();
|
|
|
|
|
|
|
|
String stringValue = choice.stringValue();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Command.Choice choiceData = null;
|
|
|
|
|
|
|
|
if (intValue != 0) {
|
|
|
|
|
|
|
|
choiceData = new Command.Choice(name, intValue);
|
|
|
|
|
|
|
|
} else if (!Double.isNaN(doubleValue)) {
|
|
|
|
|
|
|
|
choiceData = new Command.Choice(name, doubleValue);
|
|
|
|
|
|
|
|
} else if (!stringValue.isBlank()) {
|
|
|
|
|
|
|
|
choiceData = new Command.Choice(name, stringValue);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return choiceData;
|
|
|
|
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
|
|
return optionData.addChoices(choiceDataList);
|
|
|
|
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SubcommandData subcommandData = new SubcommandData(subCommandName, subCommandDescription);
|
|
|
|
|
|
|
|
subcommandData.addOptions(optionDataList);
|
|
|
|
|
|
|
|
subCommandList.add(subcommandData);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String subCommandPath = parent + "/" + name + "/" + subCommandName;
|
|
|
|
|
|
|
|
storeIntoCommandMap(commandMap, subCommandGroupDefinition, subCommandPath, subCommandName);
|
|
|
|
|
|
|
|
commandDataMap.put(subCommandPath, commandData);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return subcommandGroupDataList;
|
|
|
|
subcommandGroupData.addSubcommands(subCommandList);
|
|
|
|
|
|
|
|
commandData.addSubcommandGroups(subcommandGroupData);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new RuntimeException("Sub Command Annotation For " + subCommandGroupDefinition + " Was Null");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
for (BeanDefinition<?> subCommandDefinition : subCommandDefinitions) {
|
|
|
|
}
|
|
|
|
AnnotationValue<SubCommand> subCommand = subCommandDefinition.getDeclaredAnnotation(SubCommand.class);
|
|
|
|
|
|
|
|
if (subCommand != null) {
|
|
|
|
private List<SubcommandData> loadSubCommands(AnnotationValue<?> slashCommand) {
|
|
|
|
String commandParent = subCommand.stringValue("commandParent").orElseThrow();
|
|
|
|
if (slashCommand.contains("subCommands")) {
|
|
|
|
|
|
|
|
List<SubcommandData> subcommandDataList = new ArrayList<>();
|
|
|
|
|
|
|
|
List<AnnotationValue<SubCommand>> subCommandAnnotations = slashCommand.getAnnotations("subCommands", SubCommand.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (AnnotationValue<SubCommand> subCommand : subCommandAnnotations) {
|
|
|
|
|
|
|
|
String name = subCommand.stringValue("name").orElseThrow();
|
|
|
|
String name = subCommand.stringValue("name").orElseThrow();
|
|
|
|
String description = subCommand.stringValue("description").orElseThrow();
|
|
|
|
String description = subCommand.stringValue("description").orElseThrow();
|
|
|
|
|
|
|
|
String commandPath = commandParent + "/" + name;
|
|
|
|
|
|
|
|
|
|
|
|
SubcommandData subCommandData = new SubcommandData(name, description);
|
|
|
|
CommandData commandData = commandDataMap.get(commandPath);
|
|
|
|
|
|
|
|
SubcommandData subcommandData = new SubcommandData(name, description);
|
|
|
|
List<OptionData> optionData = loadOptions(subCommand);
|
|
|
|
List<OptionData> optionData = loadOptions(subCommand);
|
|
|
|
if (optionData != null) subCommandData.addOptions(optionData);
|
|
|
|
if (optionData != null) subcommandData.addOptions(optionData);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
subcommandDataList.add(subCommandData);
|
|
|
|
storeIntoCommandMap(commandMap, subCommandDefinition, commandPath, "handle");
|
|
|
|
|
|
|
|
commandData.addSubcommands(subcommandData);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new RuntimeException("Sub Command Annotation For " + subCommandDefinition + " Was Null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return subcommandDataList;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return commandDataList;
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<OptionData> loadOptions(AnnotationValue<?> slashCommand) {
|
|
|
|
private List<OptionData> loadOptions(AnnotationValue<?> slashCommand) {
|
|
|
@ -131,7 +183,7 @@ public class SlashCommandLoader implements CommandLoader {
|
|
|
|
List<net.dv8tion.jda.api.interactions.commands.Command.Choice> choiceList = loadChoices(option);
|
|
|
|
List<net.dv8tion.jda.api.interactions.commands.Command.Choice> choiceList = loadChoices(option);
|
|
|
|
if (choiceList != null) optionData.addChoices(choiceList);
|
|
|
|
if (choiceList != null) optionData.addChoices(choiceList);
|
|
|
|
|
|
|
|
|
|
|
|
optionList.add(optionData);
|
|
|
|
optionList.add(optionData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return optionList;
|
|
|
|
return optionList;
|
|
|
@ -168,7 +220,7 @@ public class SlashCommandLoader implements CommandLoader {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (intValue != 0) {
|
|
|
|
if (intValue != 0) {
|
|
|
|
choiceData = new net.dv8tion.jda.api.interactions.commands.Command.Choice(name, intValue);
|
|
|
|
choiceData = new net.dv8tion.jda.api.interactions.commands.Command.Choice(name, intValue);
|
|
|
|
} else if (!Double.isNaN(doubleValue)) {
|
|
|
|
} else if (!Double.isNaN(doubleValue)) {
|
|
|
|
choiceData = new net.dv8tion.jda.api.interactions.commands.Command.Choice(name, doubleValue);
|
|
|
|
choiceData = new net.dv8tion.jda.api.interactions.commands.Command.Choice(name, doubleValue);
|
|
|
|
} else if (!stringValue.isBlank()) {
|
|
|
|
} else if (!stringValue.isBlank()) {
|
|
|
@ -183,4 +235,18 @@ public class SlashCommandLoader implements CommandLoader {
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void storeIntoCommandMap(Map<String, Pair<Object, Method>> commandMap, BeanDefinition<?> beanDefinition, String commandPath, String methodName) {
|
|
|
|
|
|
|
|
Class<?> clazz = beanDefinition.getBeanType();
|
|
|
|
|
|
|
|
Object bean = applicationContext.getBean(clazz);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Method method = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
method = clazz.getMethod(methodName, SlashCommandEvent.class);
|
|
|
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
commandMap.put(commandPath, new ImmutablePair<>(bean, method));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|