Compare commits
22 Commits
432cf78a2e
...
dev
Author | SHA1 | Date | |
---|---|---|---|
a3c5eb62ac | |||
996f854ff7
|
|||
d2eec8b07c
|
|||
caa2ee7089
|
|||
2e5979e6e4 | |||
ab1eb74e85
|
|||
a5737b9eaa
|
|||
faca21724c
|
|||
4c5e28b679
|
|||
33f355e6ea | |||
d32ac62b4a
|
|||
e7c35d9308
|
|||
d4a91f3251
|
|||
bce4ce7812
|
|||
bae077654e
|
|||
203498de68
|
|||
24df1731da
|
|||
e3fc10a1ce
|
|||
78cacb7eb6
|
|||
7287d44645
|
|||
630c8ddee5
|
|||
4e27e6ce56
|
@@ -3,7 +3,7 @@
|
||||
|
||||
[versions]
|
||||
junit-jupiter = "5.10.2"
|
||||
log4j = "2.24.1"
|
||||
slf4j = "2.0.17"
|
||||
javacord = "3.8.0"
|
||||
discord4j = "3.2.7"
|
||||
geantyref = "2.0.0"
|
||||
@@ -11,7 +11,7 @@ mockito = "5.15.2"
|
||||
|
||||
[libraries]
|
||||
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
|
||||
log4j = { module = "org.apache.logging.log4j:log4j-api", version.ref = "log4j"}
|
||||
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j"}
|
||||
javacord = { module = "org.javacord:javacord", version.ref = "javacord"}
|
||||
discord4j = { module = "com.discord4j:discord4j-core", version.ref = "discord4j"}
|
||||
geantyref = { module = "io.leangen.geantyref:geantyref", version.ref = "geantyref"}
|
||||
|
@@ -20,7 +20,7 @@ dependencies {
|
||||
testImplementation(libs.junit.jupiter)
|
||||
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
implementation(libs.log4j)
|
||||
implementation(libs.slf4j)
|
||||
implementation(libs.geantyref)
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package net.tomatentum.marinara;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||
|
@@ -5,7 +5,7 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
@@ -27,7 +27,7 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
||||
logger.debug("Pre Check {} {} with context {}", check.getClass().getName(), result ? "succeeded" : "failed", context.toString());
|
||||
return result;
|
||||
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
||||
logger.fatal(e);
|
||||
logger.error("Failed executing pre-check", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
||||
logger.debug("Executing post check {} with context {}", check.getClass().getName(), context.toString());
|
||||
method.invoke(check, context, annotation);
|
||||
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
||||
logger.fatal(e);
|
||||
logger.error("Failed executing post-check", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -4,8 +4,9 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption.PlaceHolderEnum;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice;
|
||||
@@ -18,10 +19,11 @@ import net.tomatentum.marinara.util.LoggerUtil;
|
||||
public class SlashCommandDefinition {
|
||||
|
||||
public static SlashCommandOptionChoice[] getActualChoices(SlashCommandOption option) {
|
||||
SlashCommandOptionChoice[] choices = option.choices();
|
||||
if (choices.length <= 0 && !option.choiceEnum().equals(PlaceHolderEnum.class))
|
||||
choices = EnumChoices.of(option.choiceEnum()).choices();
|
||||
return choices;
|
||||
CommandChoices choices = option.choices();
|
||||
SlashCommandOptionChoice[] actualChoices = choices.value();
|
||||
if (choices.value().length <= 0 && !choices.cenum().equals(PlaceHolderEnum.class))
|
||||
actualChoices = EnumChoices.of(choices.cenum()).choices();
|
||||
return actualChoices;
|
||||
}
|
||||
|
||||
private Set<InteractionIdentifier> entries;
|
||||
@@ -66,13 +68,13 @@ public class SlashCommandDefinition {
|
||||
.map(x -> x.parent())
|
||||
.toList();
|
||||
|
||||
return InteractionIdentifier.distinct(subCommandGroups).toArray(SlashCommandIdentifier[]::new);
|
||||
return subCommandGroups.toArray(SlashCommandIdentifier[]::new);
|
||||
}
|
||||
|
||||
public SlashCommandIdentifier[] getSubCommands() {
|
||||
if (isRootCommand)
|
||||
return null;
|
||||
return InteractionIdentifier.distinct(entries.stream().filter(x -> x.parent() instanceof RootCommandIdentifier).toList()).toArray(SlashCommandIdentifier[]::new);
|
||||
return entries.stream().filter(x -> x.parent() instanceof RootCommandIdentifier).toArray(SlashCommandIdentifier[]::new);
|
||||
}
|
||||
|
||||
public SlashCommandIdentifier[] getSubCommands(String groupName) {
|
||||
@@ -84,7 +86,7 @@ public class SlashCommandDefinition {
|
||||
.map(x -> x.parent().parent())
|
||||
.toList();
|
||||
|
||||
return InteractionIdentifier.distinct(subCommands).toArray(SlashCommandIdentifier[]::new);
|
||||
return subCommands.toArray(SlashCommandIdentifier[]::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -95,6 +97,10 @@ public class SlashCommandDefinition {
|
||||
return this.rootIdentifier().equals(other.rootIdentifier());
|
||||
}
|
||||
|
||||
public long[] serverIds() {
|
||||
return rootIdentifier().serverIds();
|
||||
}
|
||||
|
||||
public Set<InteractionIdentifier> entries() {
|
||||
return this.entries;
|
||||
}
|
||||
|
@@ -0,0 +1,8 @@
|
||||
package net.tomatentum.marinara.interaction.commands.annotation;
|
||||
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption.PlaceHolderEnum;
|
||||
|
||||
public @interface CommandChoices {
|
||||
public SlashCommandOptionChoice[] value() default {};
|
||||
public Class<? extends Enum<?>> cenum() default PlaceHolderEnum.class;
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
package net.tomatentum.marinara.interaction.commands.annotation;
|
||||
|
||||
public @interface Range {
|
||||
public double min() default Double.MIN_VALUE;
|
||||
public double max() default Double.MAX_VALUE;
|
||||
}
|
@@ -15,8 +15,8 @@ public @interface SlashCommandOption {
|
||||
public SlashCommandOptionType type() default SlashCommandOptionType.STRING;
|
||||
public boolean required() default false;
|
||||
public boolean autocomplete() default false;
|
||||
public SlashCommandOptionChoice[] choices() default {};
|
||||
public Class<? extends Enum<?>> choiceEnum() default PlaceHolderEnum.class;
|
||||
public Range range() default @Range;
|
||||
public CommandChoices choices() default @CommandChoices;
|
||||
|
||||
public static enum PlaceHolderEnum {
|
||||
|
||||
|
@@ -1,8 +1,5 @@
|
||||
package net.tomatentum.marinara.interaction.ident;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
@@ -38,20 +35,6 @@ public class InteractionIdentifier {
|
||||
tryAddDescriptions(receiver.parent(), provider.parent());
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO: Might not be the best solution. Propagating to future
|
||||
* returns only one Identifier per name and takes the first present description
|
||||
*/
|
||||
public static Collection<InteractionIdentifier> distinct(List<InteractionIdentifier> identifiers) {
|
||||
HashMap<String, InteractionIdentifier> distinctIdentifiers = new HashMap<>();
|
||||
identifiers.forEach((x) -> {
|
||||
InteractionIdentifier current = distinctIdentifiers.get(x.name());
|
||||
if (current == null || (current.description().isBlank() && !x.description().isBlank()))
|
||||
distinctIdentifiers.put(x.name(), x);
|
||||
});
|
||||
return distinctIdentifiers.values();
|
||||
}
|
||||
|
||||
private InteractionIdentifier parent;
|
||||
private String name;
|
||||
private String description;
|
||||
|
@@ -33,7 +33,7 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||
if (autocompleteOptionValue != null)
|
||||
return autocompleteOptionValue;
|
||||
|
||||
return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type);
|
||||
return marinara.getWrapper().getContextObjectProvider().getInteractionContextObject(context, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -7,7 +7,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||
@@ -69,7 +69,7 @@ public abstract class InteractionMethod {
|
||||
try {
|
||||
method.invoke(handler, getParameters(context));
|
||||
}catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
logger.fatal(ex);
|
||||
logger.error("InteractionMethod failed to run", ex);
|
||||
}
|
||||
|
||||
this.appliedChecks.forEach(x -> x.post(context));
|
||||
|
@@ -3,7 +3,7 @@ package net.tomatentum.marinara.parser;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.interaction.annotation.Button;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
|
@@ -6,7 +6,7 @@ import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||
import net.tomatentum.marinara.checks.InteractionCheck;
|
||||
|
@@ -3,7 +3,7 @@ package net.tomatentum.marinara.parser;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
@@ -59,7 +59,7 @@ public class SlashCommandParser implements AnnotationParser {
|
||||
.build(isAutoComplete);
|
||||
}
|
||||
|
||||
logger.trace("Parsed using SlashCommandParser for method {} with the result:\n{}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString());
|
||||
logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString());
|
||||
consumer.accept((SlashCommandIdentifier) lastIdentifier);
|
||||
}
|
||||
|
||||
|
@@ -6,7 +6,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import io.leangen.geantyref.GenericTypeReflector;
|
||||
import net.tomatentum.marinara.checks.InteractionCheck;
|
||||
|
@@ -3,7 +3,7 @@ package net.tomatentum.marinara.registry;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
|
@@ -1,21 +1,22 @@
|
||||
package net.tomatentum.marinara.registry;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||
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.util.ObjectAggregator;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||
|
||||
@@ -39,11 +40,12 @@ public class InteractionRegistry {
|
||||
for (Method method : interactionHandler.getClass().getMethods()) {
|
||||
InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara);
|
||||
if (iMethod != null) {
|
||||
Optional<InteractionEntry> entry = this.interactions.stream().filter(iMethod::equals).findFirst();
|
||||
if (entry.isEmpty()) {
|
||||
interactions.add(new InteractionEntry(iMethod.identifier()).addMethod(iMethod));
|
||||
}else
|
||||
entry.get().addMethod(iMethod);
|
||||
Optional<InteractionEntry> oentry = this.interactions.stream()
|
||||
.filter(i -> i.identifier().equals(iMethod.identifier()))
|
||||
.findFirst();
|
||||
|
||||
InteractionEntry entry = oentry.orElse(new InteractionEntry(iMethod.identifier())).addMethod(iMethod);
|
||||
if (oentry.isEmpty()) this.interactions.add(entry);
|
||||
logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
@@ -51,27 +53,19 @@ public class InteractionRegistry {
|
||||
}
|
||||
|
||||
public void registerCommands() {
|
||||
List<SlashCommandDefinition> defs = new ArrayList<>();
|
||||
List<SlashCommandIdentifier> slashIdentifiers = interactions.stream()
|
||||
List<InteractionIdentifier> slashIdentifiers = interactions.stream()
|
||||
.filter((x) -> x.type().equals(InteractionType.COMMAND))
|
||||
.map((x) -> (SlashCommandIdentifier)x.identifier())
|
||||
.map((x) -> x.identifier())
|
||||
.toList();
|
||||
|
||||
slashIdentifiers.forEach((ident) -> {
|
||||
Optional<SlashCommandDefinition> appDef = defs.stream()
|
||||
.filter((x) -> x.rootIdentifier().equals(ident.rootNode()))
|
||||
.findFirst();
|
||||
SlashCommandDefinition[] defs = new ObjectAggregator<InteractionIdentifier, RootCommandIdentifier, SlashCommandDefinition>(
|
||||
i -> Arrays.asList((RootCommandIdentifier)i.rootNode()),
|
||||
SlashCommandDefinition::addIdentifier,
|
||||
SlashCommandDefinition::new)
|
||||
.aggregate(slashIdentifiers)
|
||||
.toArray(SlashCommandDefinition[]::new);
|
||||
|
||||
if (appDef.isPresent())
|
||||
appDef.get().addIdentifier(ident);
|
||||
else
|
||||
defs.add(
|
||||
new SlashCommandDefinition((RootCommandIdentifier) ident.rootNode())
|
||||
.addIdentifier(ident));
|
||||
});
|
||||
|
||||
marinara.getWrapper().registerSlashCommands(defs.toArray(SlashCommandDefinition[]::new));
|
||||
logger.info("Registered all SlashCommands");
|
||||
marinara.getWrapper().getRegisterer().register(defs);
|
||||
}
|
||||
|
||||
public void handle(Object context) {
|
||||
|
@@ -1,21 +1,14 @@
|
||||
package net.tomatentum.marinara.util;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.simple.SimpleLogger;
|
||||
import org.apache.logging.log4j.util.PropertiesUtil;
|
||||
import org.apache.logging.log4j.util.ProviderUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.helpers.NOPLoggerFactory;
|
||||
|
||||
public class LoggerUtil {
|
||||
public static Logger getLogger(String name) {
|
||||
if (ProviderUtil.hasProviders()) {
|
||||
return LogManager.getLogger(name);
|
||||
}else
|
||||
return new SimpleLogger(name, Level.DEBUG, true, false, true, true, "yyyy-MM-dd HH:mm:ss.SSSZ", null,
|
||||
new PropertiesUtil(new Properties()), System.out);
|
||||
if (LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory)
|
||||
return new SimpleLogger(name);
|
||||
return LoggerFactory.getLogger(name);
|
||||
}
|
||||
|
||||
public static Logger getLogger(Class<?> clazz) {
|
||||
|
@@ -0,0 +1,46 @@
|
||||
package net.tomatentum.marinara.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ObjectAggregator<O, K, V> {
|
||||
private Function<O, Iterable<K>> keySupplier;
|
||||
private BiConsumer<V, O> valueConsumer;
|
||||
private Function<K, V> defaultGenerator;
|
||||
|
||||
public ObjectAggregator(
|
||||
Function<O, Iterable<K>> keySupplier,
|
||||
BiConsumer<V, O> valueConsumer,
|
||||
Function<K, V> defaultGenerator) {
|
||||
this.keySupplier = keySupplier;
|
||||
this.valueConsumer = valueConsumer;
|
||||
this.defaultGenerator = defaultGenerator;
|
||||
}
|
||||
|
||||
public ObjectAggregator(
|
||||
Function<O, Iterable<K>> keySupplier,
|
||||
BiConsumer<V, O> valueConsumer,
|
||||
Supplier<V> defaultGenerator) {
|
||||
this.keySupplier = keySupplier;
|
||||
this.valueConsumer = valueConsumer;
|
||||
this.defaultGenerator = _ -> defaultGenerator.get();
|
||||
}
|
||||
|
||||
public Collection<V> aggregate(Iterable<O> iterator) {
|
||||
Map<K, V> map = new HashMap<>();
|
||||
for (O element : iterator) {
|
||||
Iterable<K> keys = this.keySupplier.apply(element);
|
||||
for (K key : keys) {
|
||||
V value = map.getOrDefault(key, this.defaultGenerator.apply(key));
|
||||
this.valueConsumer.accept(value, element);
|
||||
map.putIfAbsent(key, value);
|
||||
}
|
||||
}
|
||||
return map.values();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package net.tomatentum.marinara.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ObjectListAggregator<O, K, V> extends ObjectAggregator<O, K, ArrayList<V>> {
|
||||
|
||||
public ObjectListAggregator(Function<O, Iterable<K>> keySupplier, Function<O, V> valueConsumer) {
|
||||
super(keySupplier,
|
||||
(l, o) -> l.add(valueConsumer.apply(o)),
|
||||
() -> new ArrayList<>());
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -102,6 +102,6 @@ public final class ReflectionUtil {
|
||||
}
|
||||
|
||||
public static String getFullMethodName(Method method) {
|
||||
return method.getClass().getName() + "." + method.getName();
|
||||
return method.getDeclaringClass().getName() + "." + method.getName();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,55 @@
|
||||
package net.tomatentum.marinara.util;
|
||||
|
||||
import org.slf4j.Marker;
|
||||
import org.slf4j.event.Level;
|
||||
import org.slf4j.helpers.LegacyAbstractLogger;
|
||||
import org.slf4j.helpers.MessageFormatter;
|
||||
|
||||
public class SimpleLogger extends LegacyAbstractLogger {
|
||||
|
||||
private String name;
|
||||
|
||||
public SimpleLogger(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTraceEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDebugEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isErrorEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFullyQualifiedCallerName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleNormalizedLoggingCall(Level level, Marker marker, String messagePattern, Object[] arguments,
|
||||
Throwable throwable) {
|
||||
String formatted = MessageFormatter.basicArrayFormat(messagePattern, arguments);
|
||||
System.out.println("[%s] %s => %s".formatted(level, this.name, formatted));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@@ -4,12 +4,15 @@ 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<A extends Object, O extends Object, C extends Object> {
|
||||
|
||||
@@ -17,6 +20,8 @@ public class CommandConverter<A extends Object, O extends Object, C extends Obje
|
||||
return new CommandConverter<>(spec);
|
||||
}
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
private Spec<A, O, C> spec;
|
||||
|
||||
CommandConverter(Spec<A, O, C> spec) {
|
||||
@@ -24,6 +29,7 @@ public class CommandConverter<A extends Object, O extends Object, C extends Obje
|
||||
}
|
||||
|
||||
public A convert(SlashCommandDefinition def) {
|
||||
logger.debug("Converting command {}", def);
|
||||
List<O> options = new ArrayList<>();
|
||||
if (!def.isRootCommand()) {
|
||||
Arrays.stream(def.getSubCommands()).map(this::convertSubCommand).forEach(options::add);
|
||||
@@ -35,17 +41,20 @@ public class CommandConverter<A extends Object, O extends Object, C extends Obje
|
||||
}
|
||||
|
||||
private O convertSubCommandGroup(SlashCommandDefinition def, InteractionIdentifier identifier) {
|
||||
logger.debug("Converting subCommandGroup {} of {}", identifier, def);
|
||||
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) {
|
||||
logger.debug("Converting subCommand {}", identifier);
|
||||
List<O> 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<C> choices = Arrays.stream(SlashCommandDefinition.getActualChoices(option)).map(spec::convertChoice).toList();
|
||||
return spec.convertOption(option, choices);
|
||||
}
|
||||
|
@@ -0,0 +1,51 @@
|
||||
package net.tomatentum.marinara.wrapper;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
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 Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
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);
|
||||
logger.info("Registered all SlashCommands");
|
||||
}
|
||||
|
||||
public interface Strategy<A extends Object> {
|
||||
void registerServer(ServerCommandList<A> commands);
|
||||
void registerGlobal(Set<A> defs);
|
||||
}
|
||||
}
|
@@ -7,7 +7,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import io.leangen.geantyref.GenericTypeReflector;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
|
@@ -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();
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -19,11 +19,12 @@ dependencies {
|
||||
// Use JUnit Jupiter for testing.
|
||||
testImplementation(libs.junit.jupiter)
|
||||
testImplementation(libs.mockito)
|
||||
testImplementation(libs.discord4j)
|
||||
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
implementation(libs.log4j)
|
||||
implementation(libs.slf4j)
|
||||
implementation(libs.discord4j) {
|
||||
// exclude(module="discord4j-voice")
|
||||
exclude(module="discord4j-voice")
|
||||
}
|
||||
implementation(libs.geantyref)
|
||||
implementation(project(":lib"))
|
||||
|
@@ -56,6 +56,10 @@ public class Discord4JConverterSpec implements CommandConverter.Spec<Application
|
||||
.description(option.description())
|
||||
.required(option.required())
|
||||
.autocomplete(option.autocomplete())
|
||||
.minLength(Double.valueOf(option.range().min()).intValue())
|
||||
.minValue(option.range().min())
|
||||
.maxLength(Double.valueOf(option.range().max()).intValue())
|
||||
.maxValue(option.range().max())
|
||||
.choices(choices)
|
||||
.build();
|
||||
}
|
||||
|
@@ -0,0 +1,32 @@
|
||||
package net.tomatentum.marinara.wrapper.discord4j;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
|
||||
import discord4j.core.GatewayDiscordClient;
|
||||
import discord4j.discordjson.json.ApplicationCommandRequest;
|
||||
import discord4j.rest.service.ApplicationService;
|
||||
import net.tomatentum.marinara.wrapper.CommandRegisterer;
|
||||
import net.tomatentum.marinara.wrapper.ServerCommandList;
|
||||
|
||||
public class Discord4JRegistererStrategy implements CommandRegisterer.Strategy<ApplicationCommandRequest> {
|
||||
|
||||
private ApplicationService appService;
|
||||
private long applicationId;
|
||||
|
||||
public Discord4JRegistererStrategy(GatewayDiscordClient api) {
|
||||
this.appService = api.getRestClient().getApplicationService();
|
||||
this.applicationId = api.getRestClient().getApplicationId().block();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerServer(ServerCommandList<ApplicationCommandRequest> commands) {
|
||||
appService.bulkOverwriteGuildApplicationCommand(applicationId, commands.serverId(), new ArrayList<>(commands));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerGlobal(Set<ApplicationCommandRequest> defs) {
|
||||
appService.bulkOverwriteGlobalApplicationCommand(applicationId, new ArrayList<>(defs));
|
||||
}
|
||||
}
|
@@ -1,23 +1,19 @@
|
||||
package net.tomatentum.marinara.wrapper.discord4j;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import discord4j.core.GatewayDiscordClient;
|
||||
import discord4j.core.event.domain.interaction.InteractionCreateEvent;
|
||||
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||
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.SlashCommandDefinition;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.wrapper.CommandConverter;
|
||||
import net.tomatentum.marinara.wrapper.CommandRegisterer;
|
||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||
@@ -37,47 +33,28 @@ public class Discord4JWrapper extends LibraryWrapper {
|
||||
.filter(o -> !o.getType().equals(Type.SUB_COMMAND) && !o.getType().equals(Type.SUB_COMMAND_GROUP))
|
||||
.toList();
|
||||
|
||||
private GatewayDiscordClient api;
|
||||
private Discord4JContextObjectProvider contextObjectProvider;
|
||||
private CommandConverter<ApplicationCommandRequest, ApplicationCommandOptionData, ApplicationCommandOptionChoiceData> commandConverter;
|
||||
private CommandRegisterer<ApplicationCommandRequest> commandRegisterer;
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
public Discord4JWrapper(GatewayDiscordClient api) {
|
||||
this.api = api;
|
||||
this.contextObjectProvider = new Discord4JContextObjectProvider();
|
||||
this.commandConverter = CommandConverter.of(new Discord4JConverterSpec());
|
||||
var converter = CommandConverter.of(new Discord4JConverterSpec());
|
||||
|
||||
if (api != null)
|
||||
if (api != null) {
|
||||
this.commandRegisterer = CommandRegisterer.of(new Discord4JRegistererStrategy(api), converter);
|
||||
api.on(InteractionCreateEvent.class)
|
||||
.subscribe(event -> handleInteraction(event));
|
||||
else
|
||||
}else
|
||||
logger.warn("GatewayDiscordClient was null so no Events were subscribed to.");
|
||||
|
||||
logger.info("Discord4J wrapper loaded!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerSlashCommands(SlashCommandDefinition[] defs) {
|
||||
HashMap<Long, List<ApplicationCommandRequest>> serverCommands = new HashMap<>();
|
||||
List<ApplicationCommandRequest> globalCommands = new ArrayList<>();
|
||||
long applicationId = api.getRestClient().getApplicationId().block();
|
||||
|
||||
for (SlashCommandDefinition slashCommandDefinition : defs) {
|
||||
ApplicationCommandRequest request = this.commandConverter.convert(slashCommandDefinition);
|
||||
if (slashCommandDefinition.rootIdentifier().serverIds().length > 0) {
|
||||
for (long serverId : slashCommandDefinition.rootIdentifier().serverIds()) {
|
||||
serverCommands.putIfAbsent(serverId, new ArrayList<>());
|
||||
serverCommands.get(serverId).add(request);
|
||||
}
|
||||
}else
|
||||
globalCommands.add(request);
|
||||
}
|
||||
|
||||
for (long serverId : serverCommands.keySet()) {
|
||||
api.getRestClient().getApplicationService().bulkOverwriteGuildApplicationCommand(applicationId, serverId, serverCommands.get(serverId));
|
||||
}
|
||||
api.getRestClient().getApplicationService().bulkOverwriteGlobalApplicationCommand(applicationId, globalCommands);
|
||||
public CommandRegisterer<?> getRegisterer() {
|
||||
return this.commandRegisterer;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||
@@ -20,7 +21,7 @@ public class TestCommand implements InteractionHandler {
|
||||
name = "foo",
|
||||
description = "foo bar is very fooby",
|
||||
type = SlashCommandOptionType.STRING,
|
||||
choiceEnum = TestChoiceEnum.class
|
||||
choices = @CommandChoices(cenum = TestChoiceEnum.class)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
@@ -21,7 +21,7 @@ dependencies {
|
||||
testImplementation(libs.mockito)
|
||||
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
implementation(libs.log4j)
|
||||
implementation(libs.slf4j)
|
||||
implementation(libs.javacord)
|
||||
implementation(libs.geantyref)
|
||||
implementation(project(":lib"))
|
||||
|
@@ -51,6 +51,10 @@ public class JavacordConverterSpec implements CommandConverter.Spec<SlashCommand
|
||||
.setDescription(option.description())
|
||||
.setRequired(option.required())
|
||||
.setAutocompletable(option.autocomplete())
|
||||
.setMinLength(Double.valueOf(option.range().min()).longValue())
|
||||
.setDecimalMinValue(option.range().min())
|
||||
.setMaxLength(Double.valueOf(option.range().max()).longValue())
|
||||
.setDecimalMaxValue(option.range().max())
|
||||
.setChoices(choices)
|
||||
.build();
|
||||
}
|
||||
|
@@ -0,0 +1,29 @@
|
||||
package net.tomatentum.marinara.wrapper.javacord;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.javacord.api.DiscordApi;
|
||||
import org.javacord.api.interaction.SlashCommandBuilder;
|
||||
|
||||
import net.tomatentum.marinara.wrapper.CommandRegisterer;
|
||||
import net.tomatentum.marinara.wrapper.ServerCommandList;
|
||||
|
||||
public class JavacordRegistererStrategy implements CommandRegisterer.Strategy<SlashCommandBuilder> {
|
||||
|
||||
private DiscordApi api;
|
||||
|
||||
public JavacordRegistererStrategy(DiscordApi api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerServer(ServerCommandList<SlashCommandBuilder> commands) {
|
||||
api.bulkOverwriteServerApplicationCommands(commands.serverId(), commands);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerGlobal(Set<SlashCommandBuilder> defs) {
|
||||
api.bulkOverwriteGlobalApplicationCommands(defs);
|
||||
}
|
||||
|
||||
}
|
@@ -1,17 +1,11 @@
|
||||
package net.tomatentum.marinara.wrapper.javacord;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.javacord.api.DiscordApi;
|
||||
import org.javacord.api.interaction.SlashCommandBuilder;
|
||||
import org.javacord.api.interaction.SlashCommandOption;
|
||||
import org.javacord.api.interaction.SlashCommandOptionChoice;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||
import net.tomatentum.marinara.wrapper.CommandConverter;
|
||||
import net.tomatentum.marinara.wrapper.CommandRegisterer;
|
||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
@@ -22,43 +16,26 @@ import net.tomatentum.marinara.wrapper.javacord.identifierconverter.SlashCommand
|
||||
|
||||
public class JavacordWrapper extends LibraryWrapper {
|
||||
|
||||
private DiscordApi api;
|
||||
private JavacordContextObjectProvider contextObjectProvider;
|
||||
private CommandConverter<SlashCommandBuilder, SlashCommandOption, SlashCommandOptionChoice> commandConverter;
|
||||
private CommandRegisterer<SlashCommandBuilder> commandRegisterer;
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
public JavacordWrapper(DiscordApi api) {
|
||||
this.api = api;
|
||||
this.contextObjectProvider = new JavacordContextObjectProvider();
|
||||
this.commandConverter = CommandConverter.of(new JavacordConverterSpec());
|
||||
var converter = CommandConverter.of(new JavacordConverterSpec());
|
||||
|
||||
if (api != null)
|
||||
if (api != null) {
|
||||
this.commandRegisterer = CommandRegisterer.of(new JavacordRegistererStrategy(api), converter);
|
||||
api.addInteractionCreateListener((e) -> handleInteraction(e.getInteraction()));
|
||||
else
|
||||
}else
|
||||
logger.warn("DiscordApi was null so no Events were subscribed to.");
|
||||
logger.info("Javacord wrapper loaded!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerSlashCommands(SlashCommandDefinition[] defs) {
|
||||
HashMap<Long, Set<SlashCommandBuilder>> serverCommands = new HashMap<>();
|
||||
Set<SlashCommandBuilder> globalCommands = new HashSet<>();
|
||||
for (SlashCommandDefinition slashCommandDefinition : defs) {
|
||||
SlashCommandBuilder builder = commandConverter.convert(slashCommandDefinition);
|
||||
if (slashCommandDefinition.rootIdentifier().serverIds().length > 0) {
|
||||
for (long serverId : slashCommandDefinition.rootIdentifier().serverIds()) {
|
||||
serverCommands.putIfAbsent(serverId, new HashSet<>());
|
||||
serverCommands.get(serverId).add(builder);
|
||||
}
|
||||
}else
|
||||
globalCommands.add(builder);
|
||||
}
|
||||
|
||||
for (long serverId : serverCommands.keySet()) {
|
||||
api.bulkOverwriteServerApplicationCommands(serverId, serverCommands.get(serverId));
|
||||
}
|
||||
api.bulkOverwriteGlobalApplicationCommands(globalCommands);
|
||||
public CommandRegisterer<?> getRegisterer() {
|
||||
return this.commandRegisterer;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.javacord.api.interaction.SlashCommandInteraction;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||
@@ -21,7 +22,7 @@ public class TestCommand implements InteractionHandler {
|
||||
name = "foo",
|
||||
description = "foo bar is very fooby",
|
||||
type = SlashCommandOptionType.STRING,
|
||||
choiceEnum = TestChoiceEnum.class
|
||||
choices = @CommandChoices(cenum = TestChoiceEnum.class)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
Reference in New Issue
Block a user