refactor(command): add CommandRegisterer

This commit is contained in:
tueem 2025-03-16 17:06:37 +01:00
parent e3fc10a1ce
commit 24df1731da
Signed by: tueem
GPG Key ID: 65C8667EC17A88FB
4 changed files with 63 additions and 6 deletions

View File

@ -64,7 +64,7 @@ public class InteractionRegistry {
.aggregate(slashIdentifiers)
.toArray(SlashCommandDefinition[]::new);
marinara.getWrapper().registerSlashCommands(defs);
marinara.getWrapper().getRegisterer().register(defs);
logger.info("Registered all SlashCommands");
}

View File

@ -0,0 +1,45 @@
package net.tomatentum.marinara.wrapper;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
import net.tomatentum.marinara.util.ObjectAggregator;
public class CommandRegisterer<A extends Object> {
public static <A extends Object> CommandRegisterer<A> of(Strategy<A> strategy, CommandConverter<A, ?, ?> converter) {
return new CommandRegisterer<A>(strategy, converter);
}
private Strategy<A> strategy;
private CommandConverter<A, ?, ?> converter;
CommandRegisterer(Strategy<A> strategy, CommandConverter<A, ?, ?> converter) {
this.strategy = strategy;
this.converter = converter;
}
public void register(SlashCommandDefinition[] slashDefs) {
Set<ServerCommandList<A>> serverCommands = new ObjectAggregator<SlashCommandDefinition, Long, ServerCommandList<A>>(
def -> Arrays.stream(def.serverIds()).boxed().toList(),
(l, o) -> l.add(converter.convert(o)),
ServerCommandList::new)
.aggregate(Arrays.asList(slashDefs)).stream()
.collect(Collectors.toSet());
Set<A> globalCommands = Arrays.stream(slashDefs)
.filter(x -> x.serverIds().length <= 0)
.map(converter::convert)
.collect(Collectors.toSet());
serverCommands.forEach(strategy::registerServer);
strategy.registerGlobal(globalCommands);
}
public interface Strategy<A extends Object> {
void registerServer(ServerCommandList<A> commands);
void registerGlobal(Set<A> defs);
}
}

View File

@ -3,9 +3,6 @@ package net.tomatentum.marinara.wrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
public abstract class LibraryWrapper {
private List<Consumer<Object>> interactionSubscriber;
@ -25,8 +22,7 @@ public abstract class LibraryWrapper {
interactionSubscriber.remove(consumer);
}
public abstract void registerSlashCommands(SlashCommandDefinition[] defs);
public abstract CommandRegisterer<?> getRegisterer();
public abstract IdentifierProvider createIdentifierProvider();
public abstract ContextObjectProvider getContextObjectProvider();

View File

@ -0,0 +1,16 @@
package net.tomatentum.marinara.wrapper;
import java.util.HashSet;
public class ServerCommandList<A> extends HashSet<A>{
private long serverId;
public ServerCommandList(long serverId) {
this.serverId = serverId;
}
public long serverId() {
return serverId;
}
}