refactor(command): add CommandRegisterer
This commit is contained in:
parent
e3fc10a1ce
commit
24df1731da
@ -64,7 +64,7 @@ public class InteractionRegistry {
|
|||||||
.aggregate(slashIdentifiers)
|
.aggregate(slashIdentifiers)
|
||||||
.toArray(SlashCommandDefinition[]::new);
|
.toArray(SlashCommandDefinition[]::new);
|
||||||
|
|
||||||
marinara.getWrapper().registerSlashCommands(defs);
|
marinara.getWrapper().getRegisterer().register(defs);
|
||||||
logger.info("Registered all SlashCommands");
|
logger.info("Registered all SlashCommands");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -3,9 +3,6 @@ package net.tomatentum.marinara.wrapper;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
|
||||||
|
|
||||||
public abstract class LibraryWrapper {
|
public abstract class LibraryWrapper {
|
||||||
|
|
||||||
private List<Consumer<Object>> interactionSubscriber;
|
private List<Consumer<Object>> interactionSubscriber;
|
||||||
@ -25,8 +22,7 @@ public abstract class LibraryWrapper {
|
|||||||
interactionSubscriber.remove(consumer);
|
interactionSubscriber.remove(consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void registerSlashCommands(SlashCommandDefinition[] defs);
|
public abstract CommandRegisterer<?> getRegisterer();
|
||||||
|
|
||||||
public abstract IdentifierProvider createIdentifierProvider();
|
public abstract IdentifierProvider createIdentifierProvider();
|
||||||
public abstract ContextObjectProvider getContextObjectProvider();
|
public abstract ContextObjectProvider getContextObjectProvider();
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user