Ensure that commandMap is injected in

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

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

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

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

Loading…
Cancel
Save