Load commandMap with type ISlashCommand.java

java-rewrite
Hammy 3 years ago
parent cacc601ee7
commit 783eec7b50

@ -1,10 +1,10 @@
package me.goudham.command; package me.goudham.command;
import io.micronaut.inject.BeanDefinition;
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.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.CommandData;
public interface CommandLoader { public interface CommandLoader {
List<CommandData> loadIntoMapAndReturnCommands(Map<String, BeanDefinition<?>> commandMap); List<CommandData> loadIntoMapAndReturnCommands(Map<String, ISlashCommand> commandMap);
} }

@ -1,10 +1,10 @@
package me.goudham.command; package me.goudham.command;
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.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.annotation.Annotation;
@ -15,8 +15,8 @@ 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 me.goudham.command.annotation.Choice; import me.goudham.command.annotation.Choice;
import me.goudham.command.annotation.Command;
import me.goudham.command.annotation.Option; import me.goudham.command.annotation.Option;
import me.goudham.command.annotation.SubCommand; import me.goudham.command.annotation.SubCommand;
import me.goudham.command.annotation.SubCommandGroup; import me.goudham.command.annotation.SubCommandGroup;
@ -30,19 +30,22 @@ import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData;
@Introspected @Introspected
public class SlashCommandLoader implements CommandLoader { public class SlashCommandLoader implements CommandLoader {
private final BeanContext beanContext; private final BeanContext beanContext;
private final ApplicationContext applicationContext;
@Inject @Inject
public SlashCommandLoader(BeanContext beanContext) { public SlashCommandLoader(BeanContext beanContext, ApplicationContext applicationContext) {
this.beanContext = beanContext; this.beanContext = beanContext;
this.applicationContext = applicationContext;
} }
@Override @Override
public List<CommandData> loadIntoMapAndReturnCommands(Map<String, BeanDefinition<?>> commandMap) { public List<CommandData> loadIntoMapAndReturnCommands(Map<String, ISlashCommand> commandMap) {
Collection<BeanDefinition<?>> beanDefinitions = beanContext.getBeanDefinitions(Qualifiers.byStereotype(Command.class)); Collection<BeanDefinition<ISlashCommand>> beanDefinitions = beanContext.getBeanDefinitions(ISlashCommand.class);
List<CommandData> commandDataList = new ArrayList<>(); List<CommandData> commandDataList = new ArrayList<>();
for (BeanDefinition<?> beanDefinition : beanDefinitions) { for (BeanDefinition<ISlashCommand> beanDefinition : beanDefinitions) {
AnnotationValue<Annotation> slashCommand = beanDefinition.getDeclaredAnnotation("me.goudham.command.annotation.Command"); AnnotationValue<Annotation> slashCommand = beanDefinition.getDeclaredAnnotation("me.goudham.command.annotation.SlashCommand");
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();
@ -60,9 +63,9 @@ public class SlashCommandLoader implements CommandLoader {
if (optionData != null) commandData.addOptions(optionData); if (optionData != null) commandData.addOptions(optionData);
commandDataList.add(commandData); commandDataList.add(commandData);
commandMap.put(name, beanDefinition); commandMap.put(name, applicationContext.getBean(beanDefinition));
} else { } else {
throw new RuntimeException(); throw new RuntimeException("Slash Command Annotation For " + beanDefinition + " Was Null");
} }
} }

Loading…
Cancel
Save