Delegate loading of commands to CommandLoader.java

java-rewrite
Hammy 3 years ago
parent a2efe9dc69
commit 118a762d00

@ -1,56 +1,30 @@
package me.goudham.command; package me.goudham.command;
import io.micronaut.context.BeanContext; import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.AnnotationValue;
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.util.ArrayList;
import java.util.Collection;
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.command.annotation.Command; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.JDA;
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;
@Singleton @Singleton
@Introspected
public class CommandManager { public class CommandManager {
private final Map<String, ICommand> commandList; private final Map<String, BeanDefinition<?>> commandMap = new HashMap<>();
private final BeanContext beanContext; private final CommandLoader commandLoader;
private final JDA bot;
@Inject @Inject
public CommandManager(BeanContext beanContext, JDA jda) { public CommandManager(CommandLoader commandLoader) {
this.beanContext = beanContext; this.commandLoader = commandLoader;
this.bot = jda;
this.commandList = new HashMap<>();
} }
void registerSlashCommands() { public void registerSlashCommands(Guild guild) {
CommandListUpdateAction commands = bot.getGuildById("").updateCommands(); CommandListUpdateAction commands = guild.updateCommands();
List<CommandData> commandDataList = new ArrayList<>(); List<CommandData> commandDataList = commandLoader.loadIntoMapAndReturnCommands(commandMap);
commands.addCommands(commandDataList).queue();
Collection<BeanDefinition<?>> definitions = beanContext.getBeanDefinitions(Qualifiers.byStereotype(Command.class));
for (BeanDefinition<?> definition : definitions) {
AnnotationValue<Annotation> command = definition.getDeclaredAnnotation("me.goudham.command.annotation.Command");
String commandName = command.stringValue("name").get();
String commandDescription = command.stringValue("description").get();
boolean isVisible = command.booleanValue("isVisible").get();
CommandData commandData = new CommandData(commandName, commandDescription);
commandData.setDefaultEnabled(isVisible);
// commandData.
// commandDataList.add(new CommandData(commandName, ))
}
commands.addCommands();
} }
} }

Loading…
Cancel
Save