Add Helper methods for easier command registration

This commit is contained in:
tueem 2024-10-26 19:43:31 +02:00
parent df94601816
commit ae7e66d06d
No known key found for this signature in database
GPG Key ID: 819A0F7C36B9CF07

@ -1,10 +1,13 @@
package net.tomatentum.marinara.interaction.commands; package net.tomatentum.marinara.interaction.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup;
public class SlashCommandDefinition { public class SlashCommandDefinition {
private List<ExecutableSlashCommandDefinition> executableDefinitons; private List<ExecutableSlashCommandDefinition> executableDefinitons;
@ -50,4 +53,30 @@ public class SlashCommandDefinition {
executableDefinitons.forEach(set::add); executableDefinitons.forEach(set::add);
return set.toArray(new ExecutableSlashCommandDefinition[0]); return set.toArray(new ExecutableSlashCommandDefinition[0]);
} }
public SubCommandGroup[] getSubCommandGroups() {
HashSet<SubCommandGroup> subCommandGroups = new HashSet<>();
for (ExecutableSlashCommandDefinition execDef : getUniqueExecutableDefinitions()) {
String currName = execDef.subCommandGroup().name();
if (execDef.subCommandGroup() == null)
continue;
SubCommandGroup[] matching = (SubCommandGroup[]) subCommandGroups.stream().filter((x) -> x.name().equals(currName)).toArray();
if (matching.length < 1)
subCommandGroups.add(execDef.subCommandGroup());
else if (matching[0].description().isBlank() && execDef.subCommandGroup().description().isBlank()) {
subCommandGroups.removeIf((x) -> x.name().equals(currName));
subCommandGroups.add(execDef.subCommandGroup());
}
}
return subCommandGroups.toArray(new SubCommandGroup[0]);
}
public SubCommand[] getSubCommands(SubCommandGroup group) {
if (group == null)
return (SubCommand[]) Arrays.asList(getUniqueExecutableDefinitions()).stream().filter((x) -> x.subCommandGroup() == null && x.subCommand() != null).toArray();
else
return (SubCommand[]) Arrays.asList(getUniqueExecutableDefinitions()).stream().filter((x) -> x.subCommandGroup().name().equals(group.name()) && x.subCommand() != null).toArray();
}
} }