add CommandConverter logic and wrapper implementations
All checks were successful
github-mirror / push-github (push) Successful in 3s
Build / Gradle-Build (push) Successful in 37s
Test / Gradle-Test (push) Successful in 48s

This commit is contained in:
Tueem 2025-03-03 23:32:25 +01:00
parent 823402e0cd
commit 56b668851b
Signed by: tueem
GPG Key ID: F2CE0513D231AD7A
5 changed files with 207 additions and 157 deletions

View File

@ -1,23 +1,60 @@
package net.tomatentum.marinara.interaction.commands;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup;
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;
public class CommandConverter<A extends Object> {
public class CommandConverter<A extends Object, O extends Object, C extends Object> {
private Spec<A, ?, ?> spec;
public static <A, O, C> CommandConverter<A, O, C> of(Spec<A, O, C> spec) {
return new CommandConverter<>(spec);
}
private Spec<A, O, C> spec;
CommandConverter(Spec<A, O, C> spec) {
this.spec = spec;
}
public A convert(SlashCommandDefinition def) {
return null;
List<O> 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) {
SlashCommandIdentifier[] subCommands = def.getSubCommands(identifier.name());
List<O> convertedSubCommands = Arrays.stream(subCommands).map(this::convertSubCommand).toList();
return spec.convertSubCommandGroup(identifier, convertedSubCommands);
}
private O convertSubCommand(SlashCommandIdentifier identifier) {
List<O> options = Arrays.stream(identifier.options()).map(this::convertOption).toList();
return spec.convertSubCommand(identifier, options);
}
private O convertOption(SlashCommandOption option) {
List<C> choices = Arrays.stream(SlashCommandDefinition.getActualChoices(option)).map(spec::convertChoice).toList();
return spec.convertOption(option, choices);
}
public static interface Spec<A extends Object, O extends Object, C extends Object> {
public A convertCommand(SlashCommandDefinition def);
public O convertSubCommand(SubCommand def, O[] options);
public O convertSubCommandGroup(SubCommandGroup def, O[] options);
public O convertOption(SlashCommandOption option, C[] choices);
public C[] convertChoice(SlashCommandOption option);
public A convertCommand(RootCommandIdentifier rootIdentifier, List<O> options);
public O convertSubCommandGroup(InteractionIdentifier identifier, List<O> subCommands);
public O convertSubCommand(InteractionIdentifier identifier, List<O> options);
public O convertOption(SlashCommandOption option, List<C> choices);
public C convertChoice(SlashCommandOptionChoice choice);
}
}

View File

@ -0,0 +1,75 @@
package net.tomatentum.marinara.wrapper.discord4j;
import java.util.List;
import discord4j.core.object.command.ApplicationCommandOption.Type;
import discord4j.discordjson.json.ApplicationCommandOptionChoiceData;
import discord4j.discordjson.json.ApplicationCommandOptionData;
import discord4j.discordjson.json.ApplicationCommandRequest;
import net.tomatentum.marinara.interaction.commands.CommandConverter;
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;
public class Discord4JConverterSpec implements CommandConverter.Spec<ApplicationCommandRequest, ApplicationCommandOptionData, ApplicationCommandOptionChoiceData> {
@Override
public ApplicationCommandRequest convertCommand(RootCommandIdentifier rootIdentifier,
List<ApplicationCommandOptionData> options) {
return ApplicationCommandRequest.builder()
.name(rootIdentifier.name())
.description(rootIdentifier.description())
.options(options)
.build();
}
@Override
public ApplicationCommandOptionData convertSubCommandGroup(InteractionIdentifier identifier,
List<ApplicationCommandOptionData> subCommands) {
return ApplicationCommandOptionData.builder()
.type(Type.SUB_COMMAND_GROUP.getValue())
.name(identifier.name())
.description(identifier.description())
.options(subCommands)
.build();
}
@Override
public ApplicationCommandOptionData convertSubCommand(InteractionIdentifier identifier,
List<ApplicationCommandOptionData> options) {
return ApplicationCommandOptionData.builder()
.type(Type.SUB_COMMAND_GROUP.getValue())
.name(identifier.name())
.description(identifier.description())
.options(options)
.build();
}
@Override
public ApplicationCommandOptionData convertOption(SlashCommandOption option,
List<ApplicationCommandOptionChoiceData> choices) {
Type type = Type.of(option.type().getValue());
return ApplicationCommandOptionData.builder()
.type(type.getValue())
.name(option.name())
.description(option.description())
.required(option.required())
.autocomplete(option.autocomplete())
.choices(choices)
.build();
}
@Override
public ApplicationCommandOptionChoiceData convertChoice(SlashCommandOptionChoice choice) {
var builder = ApplicationCommandOptionChoiceData.builder().name(choice.name());
if (choice.longValue() != Long.MAX_VALUE)
builder.value(choice.longValue());
if (choice.doubleValue() != Double.MAX_VALUE)
builder.value(choice.doubleValue());
if (!choice.stringValue().isEmpty())
builder.value(choice.stringValue());
return builder.build();
}
}

View File

@ -1,7 +1,6 @@
package net.tomatentum.marinara.wrapper.discord4j;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.function.Function;
@ -20,12 +19,9 @@ import discord4j.discordjson.json.ApplicationCommandOptionData;
import discord4j.discordjson.json.ApplicationCommandRequest;
import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.commands.CommandConverter;
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;
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
import net.tomatentum.marinara.wrapper.LibraryWrapper;
@ -44,12 +40,15 @@ public class Discord4JWrapper extends LibraryWrapper {
private GatewayDiscordClient api;
private Discord4JContextObjectProvider contextObjectProvider;
private CommandConverter<ApplicationCommandRequest, ApplicationCommandOptionData, ApplicationCommandOptionChoiceData> commandConverter;
private Logger logger = LoggerUtil.getLogger(getClass());
public Discord4JWrapper(GatewayDiscordClient api) {
this.api = api;
this.contextObjectProvider = new Discord4JContextObjectProvider();
this.commandConverter = CommandConverter.of(new Discord4JConverterSpec());
if (api != null)
api.on(InteractionCreateEvent.class)
.subscribe(event -> handleInteraction(event));
@ -66,7 +65,7 @@ public class Discord4JWrapper extends LibraryWrapper {
long applicationId = api.getRestClient().getApplicationId().block();
for (SlashCommandDefinition slashCommandDefinition : defs) {
ApplicationCommandRequest request = convertSlashCommand(slashCommandDefinition);
ApplicationCommandRequest request = this.commandConverter.convert(slashCommandDefinition);
if (slashCommandDefinition.rootIdentifier().serverIds().length > 0) {
for (long serverId : slashCommandDefinition.rootIdentifier().serverIds()) {
serverCommands.putIfAbsent(serverId, new ArrayList<>());
@ -128,72 +127,6 @@ public class Discord4JWrapper extends LibraryWrapper {
return last;
}
private ApplicationCommandRequest convertSlashCommand(SlashCommandDefinition def) {
List<ApplicationCommandOptionData> options = new ArrayList<>();
RootCommandIdentifier cmd = def.rootIdentifier();
if (!def.isRootCommand()) {
Arrays.stream(def.getSubCommands(null)).map(this::convertSubCommandDef).forEach(options::add);
Arrays.stream(def.getSubCommandGroups()).map((x) -> convertSubCommandGroupDef(def, x)).forEach(options::add);
}else {
Arrays.stream(cmd.options()).map(this::convertOptionDef).forEach(options::add);
}
return ApplicationCommandRequest.builder()
.name(cmd.name())
.description(cmd.description())
.options(options)
.build();
}
private ApplicationCommandOptionData convertSubCommandGroupDef(SlashCommandDefinition def, SlashCommandIdentifier subGroup) {
SlashCommandIdentifier[] subCommands = def.getSubCommands(subGroup.name());
List<ApplicationCommandOptionData> convertedSubCommands = Arrays.stream(subCommands).map(this::convertSubCommandDef).toList();
return ApplicationCommandOptionData.builder()
.type(Type.SUB_COMMAND_GROUP.getValue())
.name(subGroup.name())
.description(subGroup.description())
.options(convertedSubCommands)
.build();
}
private ApplicationCommandOptionData convertSubCommandDef(SlashCommandIdentifier sub) {
List<ApplicationCommandOptionData> convertedOptions = Arrays.stream(sub.options()).map(this::convertOptionDef).toList();
return ApplicationCommandOptionData.builder()
.type(Type.SUB_COMMAND_GROUP.getValue())
.name(sub.name())
.description(sub.description())
.options(convertedOptions)
.build();
}
private ApplicationCommandOptionData convertOptionDef(SlashCommandOption option) {
Type type = Enum.valueOf(Type.class, option.type().toString());
return ApplicationCommandOptionData.builder()
.type(type.getValue())
.name(option.name())
.description(option.description())
.required(option.required())
.autocomplete(option.autocomplete())
.choices(convertChoices(option))
.build();
}
private List<ApplicationCommandOptionChoiceData> convertChoices(SlashCommandOption option) {
List<ApplicationCommandOptionChoiceData> convertedChoices = new ArrayList<>();
for (SlashCommandOptionChoice choice : SlashCommandDefinition.getActualChoices(option)) {
var builder = ApplicationCommandOptionChoiceData.builder();
builder.name(choice.name());
if (choice.longValue() != Long.MAX_VALUE)
builder.value(choice.longValue());
if (choice.doubleValue() != Double.MAX_VALUE)
builder.value(choice.doubleValue());
if (!choice.stringValue().isEmpty())
builder.value(choice.stringValue());
convertedChoices.add(builder.build());
}
return convertedChoices;
}
@Override
public ContextObjectProvider getContextObjectProvider() {
return this.contextObjectProvider;

View File

@ -0,0 +1,73 @@
package net.tomatentum.marinara.wrapper.javacord;
import java.util.List;
import org.javacord.api.interaction.SlashCommand;
import org.javacord.api.interaction.SlashCommandBuilder;
import org.javacord.api.interaction.SlashCommandOption;
import org.javacord.api.interaction.SlashCommandOptionBuilder;
import org.javacord.api.interaction.SlashCommandOptionChoice;
import org.javacord.api.interaction.SlashCommandOptionChoiceBuilder;
import org.javacord.api.interaction.SlashCommandOptionType;
import net.tomatentum.marinara.interaction.commands.CommandConverter;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
public class JavacordConverterSpec implements CommandConverter.Spec<SlashCommandBuilder, SlashCommandOption, SlashCommandOptionChoice> {
@Override
public SlashCommandBuilder convertCommand(RootCommandIdentifier rootIdentifier, List<SlashCommandOption> options) {
return SlashCommand.with(rootIdentifier.name(), rootIdentifier.description(), options);
}
@Override
public SlashCommandOption convertSubCommandGroup(InteractionIdentifier identifier,
List<SlashCommandOption> subCommands) {
return SlashCommandOption.createWithOptions(
SlashCommandOptionType.SUB_COMMAND_GROUP,
identifier.name(),
identifier.description(),
subCommands);
}
@Override
public SlashCommandOption convertSubCommand(InteractionIdentifier identifier, List<SlashCommandOption> options) {
return SlashCommandOption.createWithOptions(
SlashCommandOptionType.SUB_COMMAND,
identifier.name(),
identifier.description(),
options);
}
@Override
public SlashCommandOption convertOption(
net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption option,
List<SlashCommandOptionChoice> choices) {
SlashCommandOptionType type = SlashCommandOptionType.fromValue(option.type().getValue());
return new SlashCommandOptionBuilder()
.setType(type)
.setName(option.name())
.setDescription(option.description())
.setRequired(option.required())
.setAutocompletable(option.autocomplete())
.setChoices(choices)
.build();
}
@Override
public SlashCommandOptionChoice convertChoice(
net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice choice) {
SlashCommandOptionChoiceBuilder builder = new SlashCommandOptionChoiceBuilder().setName(choice.name());
if (choice.longValue() != Long.MAX_VALUE)
builder.setValue(choice.longValue());
/*
not yet available
if (choice.doubleValue() != Double.MAX_VALUE)
builder.setValue(choice.doubleValue());
*/
if (!choice.stringValue().isEmpty())
builder.setValue(choice.stringValue());
return builder.build();
}
}

View File

@ -1,7 +1,5 @@
package net.tomatentum.marinara.wrapper.javacord;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -14,17 +12,13 @@ import org.javacord.api.interaction.ButtonInteraction;
import org.javacord.api.interaction.SlashCommandBuilder;
import org.javacord.api.interaction.SlashCommandInteraction;
import org.javacord.api.interaction.SlashCommandInteractionOption;
import org.javacord.api.interaction.SlashCommandOptionBuilder;
import org.javacord.api.interaction.SlashCommandOptionChoiceBuilder;
import org.javacord.api.interaction.SlashCommandOptionType;
import org.javacord.api.interaction.SlashCommandOption;
import org.javacord.api.interaction.SlashCommandOptionChoice;
import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.commands.CommandConverter;
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.wrapper.ContextObjectProvider;
import net.tomatentum.marinara.util.LoggerUtil;
import net.tomatentum.marinara.wrapper.LibraryWrapper;
@ -33,12 +27,15 @@ public class JavacordWrapper extends LibraryWrapper {
private DiscordApi api;
private JavacordContextObjectProvider contextObjectProvider;
private CommandConverter<SlashCommandBuilder, SlashCommandOption, SlashCommandOptionChoice> commandConverter;
private Logger logger = LoggerUtil.getLogger(getClass());
public JavacordWrapper(DiscordApi api) {
this.api = api;
this.contextObjectProvider = new JavacordContextObjectProvider();
this.commandConverter = CommandConverter.of(new JavacordConverterSpec());
if (api != null)
api.addInteractionCreateListener((e) -> handleInteraction(e.getInteraction()));
else
@ -51,7 +48,7 @@ public class JavacordWrapper extends LibraryWrapper {
HashMap<Long, Set<SlashCommandBuilder>> serverCommands = new HashMap<>();
Set<SlashCommandBuilder> globalCommands = new HashSet<>();
for (SlashCommandDefinition slashCommandDefinition : defs) {
SlashCommandBuilder builder = convertSlashCommand(slashCommandDefinition);
SlashCommandBuilder builder = commandConverter.convert(slashCommandDefinition);
if (slashCommandDefinition.rootIdentifier().serverIds().length > 0) {
for (long serverId : slashCommandDefinition.rootIdentifier().serverIds()) {
serverCommands.putIfAbsent(serverId, new HashSet<>());
@ -111,71 +108,6 @@ public class JavacordWrapper extends LibraryWrapper {
return lastIdentifier;
}
private SlashCommandBuilder convertSlashCommand(SlashCommandDefinition def) {
List<org.javacord.api.interaction.SlashCommandOption> options = new ArrayList<>();
RootCommandIdentifier cmd = def.rootIdentifier();
if (!def.isRootCommand()) {
Arrays.stream(def.getSubCommands(null)).map(this::convertSubCommandDef).forEach(options::add);
Arrays.stream(def.getSubCommandGroups()).map((x) -> convertSubCommandGroupDef(def, x)).forEach(options::add);
}else {
Arrays.stream(cmd.options()).map(this::convertOptionDef).forEach(options::add);
}
return org.javacord.api.interaction.SlashCommand.with(cmd.name(), cmd.description(), options);
}
private org.javacord.api.interaction.SlashCommandOption convertSubCommandGroupDef(SlashCommandDefinition def, SlashCommandIdentifier subGroup) {
SlashCommandIdentifier[] subCommands = def.getSubCommands(subGroup.name());
List<org.javacord.api.interaction.SlashCommandOption> convertedSubCommands = Arrays.stream(subCommands).map(this::convertSubCommandDef).toList();
return org.javacord.api.interaction.SlashCommandOption.createWithOptions(
org.javacord.api.interaction.SlashCommandOptionType.SUB_COMMAND_GROUP,
subGroup.name(),
subGroup.description(),
convertedSubCommands);
}
private org.javacord.api.interaction.SlashCommandOption convertSubCommandDef(SlashCommandIdentifier sub) {
List<org.javacord.api.interaction.SlashCommandOption> convertedOptions = Arrays.stream(sub.options()).map(this::convertOptionDef).toList();
return org.javacord.api.interaction.SlashCommandOption.createWithOptions(
org.javacord.api.interaction.SlashCommandOptionType.SUB_COMMAND,
sub.name(),
sub.description(),
convertedOptions);
}
private org.javacord.api.interaction.SlashCommandOption convertOptionDef(SlashCommandOption option) {
SlashCommandOptionType type = SlashCommandOptionType.fromValue(option.type().getValue());
SlashCommandOptionBuilder builder = new SlashCommandOptionBuilder();
builder
.setType(type)
.setName(option.name())
.setDescription(option.description())
.setRequired(option.required())
.setAutocompletable(option.autocomplete())
.setChoices(convertChoices(option));
return builder.build();
}
private List<org.javacord.api.interaction.SlashCommandOptionChoice> convertChoices(SlashCommandOption option) {
List<org.javacord.api.interaction.SlashCommandOptionChoice> convertedChoices = new ArrayList<>();
for (SlashCommandOptionChoice choice : SlashCommandDefinition.getActualChoices(option)) {
SlashCommandOptionChoiceBuilder builder = new SlashCommandOptionChoiceBuilder();
builder.setName(choice.name());
if (choice.longValue() != Long.MAX_VALUE)
builder.setValue(choice.longValue());
/*
not yet available
if (choice.doubleValue() != Double.MAX_VALUE)
builder.setValue(choice.doubleValue());
*/
if (!choice.stringValue().isEmpty())
builder.setValue(choice.stringValue());
convertedChoices.add(builder.build());
}
return convertedChoices;
}
@Override
public ContextObjectProvider getContextObjectProvider() {
return contextObjectProvider;