Ensure that commandMap is injected in

java-rewrite
Hammy 3 years ago
parent 45709726e3
commit 720761e7a0

@ -1,11 +1,8 @@
package me.goudham.command;
import io.micronaut.inject.ExecutableMethod;
import java.util.List;
import java.util.Map;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.internal.utils.tuple.Pair;
public interface CommandLoader {
List<CommandData> loadIntoMapAndReturnCommands(Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap);
List<CommandData> loadCommands();
}

@ -1,6 +1,5 @@
package me.goudham.command;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.BeanContext;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.beans.BeanIntrospection;
@ -34,15 +33,17 @@ import net.dv8tion.jda.internal.utils.tuple.Pair;
@Singleton
public class SlashCommandLoader implements CommandLoader {
private final Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap;
private final BeanContext beanContext;
@Inject
public SlashCommandLoader(BeanContext beanContext, ApplicationContext applicationContext) {
public SlashCommandLoader(Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap, BeanContext beanContext) {
this.commandMap = commandMap;
this.beanContext = beanContext;
}
@Override
public List<CommandData> loadIntoMapAndReturnCommands(Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap) {
public List<CommandData> loadCommands() {
Map<String, CommandData> commandDataMap = new HashMap<>();
List<CommandData> commandDataList = new ArrayList<>();
Collection<BeanIntrospection<Object>> slashCommandIntrospections = BeanIntrospector.forClassLoader(ClassLoader.getSystemClassLoader()).findIntrospections(SlashCommand.class);
@ -71,7 +72,7 @@ public class SlashCommandLoader implements CommandLoader {
if (subCommandGroups.length < 1 && !noHandleMethod) {
List<OptionData> optionData = loadOptions(slashCommand);
if (optionData != null) commandData.addOptions(optionData);
storeIntoCommandMap(commandMap, slashCommandIntrospection, name, "handle");
storeIntoCommandMap(slashCommandIntrospection, name, "handle");
} else {
List<SubcommandData> subCommandList = new ArrayList<>();
for (BeanMethod<Object, Object> subCommandMethod : subCommands) {
@ -81,7 +82,7 @@ public class SlashCommandLoader implements CommandLoader {
subCommandList.add(subcommandData);
String subCommandPath = name + "/" + subcommandData.getName();
storeIntoCommandMap(commandMap, slashCommandIntrospection, subCommandPath, subCommandMethod.getName());
storeIntoCommandMap(slashCommandIntrospection, subCommandPath, subCommandMethod.getName());
}
}
commandData.addSubcommands(subCommandList);
@ -113,7 +114,7 @@ public class SlashCommandLoader implements CommandLoader {
subCommandList.add(subcommandData);
String subCommandPath = parent + "/" + name + "/" + subcommandData.getName();
storeIntoCommandMap(commandMap, subCommandGroupIntrospection, subCommandPath, subCommandMethod.getName());
storeIntoCommandMap(subCommandGroupIntrospection, subCommandPath, subCommandMethod.getName());
}
}
subcommandGroupData.addSubcommands(subCommandList);
@ -123,6 +124,7 @@ public class SlashCommandLoader implements CommandLoader {
}
}
commandDataMap.clear();
return commandDataList;
}
@ -205,7 +207,7 @@ public class SlashCommandLoader implements CommandLoader {
return null;
}
private void storeIntoCommandMap(Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap, BeanIntrospection<Object> beanIntrospection, String commandPath, String methodName) {
private void storeIntoCommandMap(BeanIntrospection<Object> beanIntrospection, String commandPath, String methodName) {
Class<Object> clazz = beanIntrospection.getBeanType();
Object beanInstance = beanContext.getBean(clazz);

@ -4,7 +4,6 @@ import io.micronaut.core.annotation.Introspected;
import io.micronaut.inject.ExecutableMethod;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.dv8tion.jda.api.entities.Guild;
@ -16,18 +15,19 @@ import net.dv8tion.jda.internal.utils.tuple.Pair;
@Singleton
@Introspected
public class SlashCommandManager implements CommandManager {
private final Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap = new HashMap<>();
private final Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap;
private final CommandLoader commandLoader;
@Inject
public SlashCommandManager(CommandLoader commandLoader) {
public SlashCommandManager(Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap, CommandLoader commandLoader) {
this.commandMap = commandMap;
this.commandLoader = commandLoader;
}
@Override
public void registerSlashCommands(Guild guild) {
CommandListUpdateAction commands = guild.updateCommands();
List<CommandData> commandDataList = commandLoader.loadIntoMapAndReturnCommands(commandMap);
List<CommandData> commandDataList = commandLoader.loadCommands();
commands.addCommands(commandDataList).queue();
}

@ -4,9 +4,12 @@ import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Value;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.Order;
import io.micronaut.inject.ExecutableMethod;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.security.auth.login.LoginException;
import me.goudham.listener.OnReadyListener;
import net.dv8tion.jda.api.JDA;
@ -15,6 +18,7 @@ import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import net.dv8tion.jda.internal.utils.tuple.Pair;
@Factory
@Introspected
@ -22,6 +26,11 @@ public class BotConfig {
private final String token;
private final String guildId;
@Singleton
public Map<String, Pair<Object, ExecutableMethod<Object, Object>>> commandMap() {
return new HashMap<>();
}
@Inject
public BotConfig(@Value("${bot.token}") String token,
@Value("${bot.guild.id}") String guildId) {

Loading…
Cancel
Save