package net.tomatentum.marinara.wrapper; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.slf4j.Logger; import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier; import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier; import net.tomatentum.marinara.util.LoggerUtil; public class CommandConverter { public static CommandConverter of(Spec spec) { return new CommandConverter<>(spec); } private Logger logger = LoggerUtil.getLogger(getClass()); private Spec spec; CommandConverter(Spec spec) { this.spec = spec; } public A convert(SlashCommandDefinition def) { logger.debug("Converting command {}", def); List options = new ArrayList<>(); if (!def.isRootCommand()) { Arrays.stream(def.getSubCommands()).map(this::convertSubCommand).forEach(options::add); Arrays.stream(def.getSubCommandGroups()).map(x -> this.convertSubCommandGroup(def, x)).forEach(options::add); }else Arrays.stream(def.rootIdentifier().options()).map(this::convertOption).forEach(options::add); return spec.convertCommand(def.rootIdentifier(), options); } private O convertSubCommandGroup(SlashCommandDefinition def, InteractionIdentifier identifier) { logger.debug("Converting subCommandGroup {} of {}", identifier, def); SlashCommandIdentifier[] subCommands = def.getSubCommands(identifier.name()); List convertedSubCommands = Arrays.stream(subCommands).map(this::convertSubCommand).toList(); return spec.convertSubCommandGroup(identifier, convertedSubCommands); } private O convertSubCommand(SlashCommandIdentifier identifier) { logger.debug("Converting subCommand {}", identifier); List options = Arrays.stream(identifier.options()).map(this::convertOption).toList(); return spec.convertSubCommand(identifier, options); } private O convertOption(SlashCommandOption option) { logger.debug("Converting option {}", option); List choices = Arrays.stream(SlashCommandDefinition.getActualChoices(option)).map(spec::convertChoice).toList(); return spec.convertOption(option, choices); } public static interface Spec { public A convertCommand(RootCommandIdentifier rootIdentifier, List options); public O convertSubCommandGroup(InteractionIdentifier identifier, List subCommands); public O convertSubCommand(InteractionIdentifier identifier, List options); public O convertOption(SlashCommandOption option, List choices); public C convertChoice(SlashCommandOptionChoice choice); } }