Skip to content

Commit

Permalink
Add global reply config
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldmensch committed Jan 5, 2025
1 parent 76f866f commit 6e1c0bd
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.kaktushose.jda.commands.annotations.interactions.StringSelectMenu;
import com.github.kaktushose.jda.commands.definitions.description.ClassFinder;
import com.github.kaktushose.jda.commands.definitions.description.reflective.ReflectiveClassFinder;
import com.github.kaktushose.jda.commands.definitions.interactions.InteractionDefinition;
import com.github.kaktushose.jda.commands.definitions.interactions.InteractionRegistry;
import com.github.kaktushose.jda.commands.definitions.interactions.component.ButtonDefinition;
import com.github.kaktushose.jda.commands.definitions.interactions.component.menu.SelectMenuDefinition;
Expand Down Expand Up @@ -47,12 +48,13 @@ public final class JDACommands {
Middlewares middlewares,
ErrorMessageFactory errorMessageFactory,
GuildScopeProvider guildScopeProvider,
InteractionRegistry interactionRegistry) {
InteractionRegistry interactionRegistry,
InteractionDefinition.ReplyConfig globalReplyConfig) {
this.jdaContext = jdaContext;
this.dependencyInjector = dependencyInjector;
this.interactionRegistry = interactionRegistry;
this.updater = new SlashCommandUpdater(jdaContext, guildScopeProvider, interactionRegistry);
this.JDAEventListener = new JDAEventListener(new DispatchingContext(middlewares, errorMessageFactory, interactionRegistry, typeAdapters, expirationStrategy, dependencyInjector));
this.JDAEventListener = new JDAEventListener(new DispatchingContext(middlewares, errorMessageFactory, interactionRegistry, typeAdapters, expirationStrategy, dependencyInjector, globalReplyConfig));
}

JDACommands start(Collection<ClassFinder> classFinders, Class<?> clazz, String[] packages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.kaktushose.jda.commands.definitions.description.ClassFinder;
import com.github.kaktushose.jda.commands.definitions.description.Descriptor;
import com.github.kaktushose.jda.commands.definitions.description.reflective.ReflectiveDescriptor;
import com.github.kaktushose.jda.commands.definitions.interactions.InteractionDefinition.ReplyConfig;
import com.github.kaktushose.jda.commands.definitions.interactions.InteractionRegistry;
import com.github.kaktushose.jda.commands.dependency.DefaultDependencyInjector;
import com.github.kaktushose.jda.commands.dependency.DependencyInjector;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class JDACommandsBuilder {
private final Collection<ClassFinder> classFinders;
private Descriptor descriptor = new ReflectiveDescriptor();

private ReplyConfig globalReplyConfig = new ReplyConfig();

// registries
private final Map<Class<?>, TypeAdapter<?>> typeAdapters = new HashMap<>();
private final Set<Map.Entry<Priority, Middleware>> middlewares = new HashSet<>();
Expand Down Expand Up @@ -140,6 +143,12 @@ public JDACommandsBuilder guildScopeProvider(GuildScopeProvider guildScopeProvid
return this;
}

/// @param globalReplyConfig the [ReplyConfig] to be used as a global fallback option
public JDACommandsBuilder globalReplyConfig(ReplyConfig globalReplyConfig) {
this.globalReplyConfig = globalReplyConfig;
return this;
}

/// This method instantiated an instance of [JDACommands] and starts the framework.
public JDACommands start(Class<?> clazz, String... packages) {
Validators validators = new Validators(this.validators);
Expand All @@ -151,7 +160,8 @@ public JDACommands start(Class<?> clazz, String... packages) {
new Middlewares(middlewares, errorMessageFactory, permissionsProvider),
errorMessageFactory,
guildScopeProvider,
new InteractionRegistry(dependencyInjector, validators, localizationFunction, descriptor)
new InteractionRegistry(dependencyInjector, validators, localizationFunction, descriptor),
globalReplyConfig
);

return jdaCommands.start(classFinders, clazz, packages);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.kaktushose.jda.commands.annotations.interactions;

import com.github.kaktushose.jda.commands.JDACommandsBuilder;
import com.github.kaktushose.jda.commands.dispatching.events.ReplyableEvent;
import com.github.kaktushose.jda.commands.dispatching.reply.ConfigurableReply;
import com.github.kaktushose.jda.commands.dispatching.reply.GlobalReplyConfig;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -14,7 +14,8 @@
/// Interaction methods annotated with [ReplyConfig] will use the configured values of this annotation when sending a reply.
/// Interaction classes annotated with [ReplyConfig] will apply the configured values of this annotation to
/// every method, if and only if no annotation is present at method level. If the [ReplyConfig] annotation is neither
/// present at the class level nor the method level, the [GlobalReplyConfig] will be used instead.
/// present at the class level nor the method level, the global [`ReplyConfig`][com.github.kaktushose.jda.commands.definitions.interactions.InteractionDefinition.ReplyConfig]
/// will be used instead.
///
/// **These values are always overridden by [ConfigurableReply#ephemeral(boolean)],
/// [ConfigurableReply#keepComponents(boolean)] or respectively [ConfigurableReply#editReply(boolean)].**
Expand All @@ -23,7 +24,7 @@
/// 1. [ConfigurableReply]
/// 2. [ReplyConfig] method annotation
/// 3. [ReplyConfig] class annotation
/// 4. [GlobalReplyConfig]
/// 4. global [`ReplyConfig`][com.github.kaktushose.jda.commands.definitions.interactions.InteractionDefinition.ReplyConfig] provided in [JDACommandsBuilder]
///
/// @see ReplyableEvent
@Target({ElementType.TYPE, ElementType.METHOD})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.github.kaktushose.jda.commands.definitions.interactions.component.menu.EntitySelectMenuDefinition;
import com.github.kaktushose.jda.commands.definitions.interactions.component.menu.StringSelectMenuDefinition;
import com.github.kaktushose.jda.commands.dispatching.middleware.impl.PermissionsMiddleware;
import com.github.kaktushose.jda.commands.dispatching.reply.GlobalReplyConfig;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -58,17 +57,17 @@ default Object newInstance() throws InvocationTargetException, InstantiationExce
/// The [ReplyConfig] that should be used when sending replies.
///
/// @implNote This will first attempt to use the [`ReplyConfig`][com.github.kaktushose.jda.commands.annotations.interactions.ReplyConfig]
/// annotation of the method and then of the class. If neither is present will fall back to the [GlobalReplyConfig].
/// annotation of the method and then of the class. If neither is present will fall back to the global [ReplyConfig] provided by [com.github.kaktushose.jda.commands.JDACommandsBuilder]
@NotNull
default ReplyConfig replyConfig() {
var global = clazzDescription().annotation(com.github.kaktushose.jda.commands.annotations.interactions.ReplyConfig.class);
var local = methodDescription().annotation(com.github.kaktushose.jda.commands.annotations.interactions.ReplyConfig.class);
default ReplyConfig replyConfig(@NotNull ReplyConfig globalFallback) {
var clazz = clazzDescription().annotation(com.github.kaktushose.jda.commands.annotations.interactions.ReplyConfig.class);
var method = methodDescription().annotation(com.github.kaktushose.jda.commands.annotations.interactions.ReplyConfig.class);

if (global.isEmpty() && local.isEmpty()) {
return new ReplyConfig();
if (clazz.isEmpty() && method.isEmpty()) {
return globalFallback;
}

return local.map(ReplyConfig::new).orElseGet(() -> new ReplyConfig(global.get()));
return method.map(ReplyConfig::new).orElseGet(() -> new ReplyConfig(clazz.get()));

}

Expand All @@ -78,9 +77,9 @@ default ReplyConfig replyConfig() {
/// @see com.github.kaktushose.jda.commands.annotations.interactions.ReplyConfig ReplyConfig
record ReplyConfig(boolean ephemeral, boolean keepComponents, boolean editReply) {

/// Constructs a new ReplyConfig using the default values specified by [GlobalReplyConfig].
/// Constructs a new ReplyConfig using the same default values as [`ReplyConfig`][com.github.kaktushose.jda.commands.annotations.interactions.ReplyConfig].
public ReplyConfig() {
this(GlobalReplyConfig.ephemeral, GlobalReplyConfig.keepComponents, GlobalReplyConfig.editReply);
this(false, true, true);
}

/// Constructs a new ReplyConfig.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public abstract sealed class ModalReplyableEvent<T extends GenericInteractionCre
protected ModalReplyableEvent(@NotNull T event,
@NotNull InteractionRegistry registry,
@NotNull Runtime runtime,
@NotNull InteractionDefinition definition) {
super(event, registry, runtime, definition);
@NotNull InteractionDefinition definition,
@NotNull InteractionDefinition.ReplyConfig global
) {
super(event, registry, runtime, definition, global);
}

/// Acknowledgement of this event with a [Modal]. This will open a popup on the target user's Discord client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ public sealed abstract class ReplyableEvent<T extends GenericInteractionCreateEv
protected ReplyableEvent(T event,
InteractionRegistry interactionRegistry,
Runtime runtime,
InteractionDefinition definition) {
InteractionDefinition definition,
InteractionDefinition.ReplyConfig globalReplyConfig) {
super(event, interactionRegistry, runtime);
this.replyConfig = definition.replyConfig();
this.replyConfig = definition.replyConfig(globalReplyConfig);
this.definition = definition;
}

Expand Down Expand Up @@ -132,6 +133,6 @@ public Message reply(@NotNull EmbedBuilder builder) {

private MessageReply newReply() {
log.debug("Reply Debug: [Runtime={}]", runtimeId());
return new MessageReply(event, definition);
return new MessageReply(event, definition, replyConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public final class CommandEvent extends ModalReplyableEvent<GenericCommandIntera
public CommandEvent(@NotNull GenericCommandInteractionEvent event,
@NotNull InteractionRegistry registry,
@NotNull Runtime runtime,
@NotNull InteractionDefinition definition) {
super(event, registry, runtime, definition);
@NotNull InteractionDefinition definition,
@NotNull InteractionDefinition.ReplyConfig global) {
super(event, registry, runtime, definition, global);
}

/// Returns the underlying [GenericCommandInteractionEvent] and casts it to the given type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public final class ComponentEvent extends ModalReplyableEvent<GenericComponentIn
public ComponentEvent(@NotNull GenericComponentInteractionCreateEvent event,
@NotNull InteractionRegistry registry,
@NotNull Runtime runtime,
@NotNull InteractionDefinition definition) {
super(event, registry, runtime, definition);
@NotNull InteractionDefinition definition,
@NotNull InteractionDefinition.ReplyConfig global) {
super(event, registry, runtime, definition, global);
}

/// Returns the underlying [GenericComponentInteractionCreateEvent] and casts it to the given type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public final class ModalEvent extends ReplyableEvent<ModalInteractionEvent> {
public ModalEvent(@NotNull ModalInteractionEvent event,
@NotNull InteractionRegistry registry,
@NotNull Runtime runtime,
@NotNull InteractionDefinition definition) {
super(event, registry, runtime, definition);
@NotNull InteractionDefinition definition,
@NotNull InteractionDefinition.ReplyConfig global) {
super(event, registry, runtime, definition, global);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected InvocationContext<GenericComponentInteractionCreateEvent> prepare(@Not
default ->
throw new IllegalStateException("Should not occur. Please report this error the the devs of jda-commands.");
};
arguments.addFirst(new ComponentEvent(genericEvent, registry, runtime, component));
arguments.addFirst(new ComponentEvent(genericEvent, registry, runtime, component, dispatchingContext.globalReplyConfig()));

return new InvocationContext<>(
genericEvent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.kaktushose.jda.commands.dispatching.handling;

import com.github.kaktushose.jda.commands.definitions.interactions.InteractionDefinition;
import com.github.kaktushose.jda.commands.definitions.interactions.InteractionRegistry;
import com.github.kaktushose.jda.commands.dependency.DependencyInjector;
import com.github.kaktushose.jda.commands.dispatching.adapter.internal.TypeAdapters;
Expand All @@ -15,5 +16,6 @@ public record DispatchingContext(Middlewares middlewares,
InteractionRegistry registry,
TypeAdapters adapterRegistry,
ExpirationStrategy expirationStrategy,
DependencyInjector dependencyInjector) {
DependencyInjector dependencyInjector,
InteractionDefinition.ReplyConfig globalReplyConfig) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected InvocationContext<ModalInteractionEvent> prepare(@NotNull ModalInterac
);

List<Object> arguments = event.getValues().stream().map(ModalMapping::getAsString).collect(Collectors.toList());
arguments.addFirst(new ModalEvent(event, registry, runtime, modal));
arguments.addFirst(new ModalEvent(event, registry, runtime, modal, dispatchingContext.globalReplyConfig()));

return new InvocationContext<>(
event,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected InvocationContext<GenericContextInteractionEvent<?>> prepare(@NotNull
);

return new InvocationContext<>(event, runtime.keyValueStore(), command,
List.of(new CommandEvent(event, registry, runtime, command), event.getTarget())
List.of(new CommandEvent(event, registry, runtime, command, dispatchingContext.globalReplyConfig()), event.getTarget())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.github.kaktushose.jda.commands.dispatching.handling.DispatchingContext;
import com.github.kaktushose.jda.commands.dispatching.handling.EventHandler;
import com.github.kaktushose.jda.commands.dispatching.reply.MessageReply;
import com.github.kaktushose.jda.commands.embeds.error.ErrorMessageFactory;
import com.github.kaktushose.jda.commands.internal.Helpers;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -45,7 +44,7 @@ private Optional<List<Object>> parseArguments(SlashCommandDefinition command, Sl
.toArray(String[]::new);

List<Object> arguments = new ArrayList<>();
arguments.addFirst(new CommandEvent(event, registry, runtime, command));
arguments.addFirst(new CommandEvent(event, registry, runtime, command, dispatchingContext.globalReplyConfig()));

log.debug("Type adapting arguments...");
var parameters = List.copyOf(command.commandParameters());
Expand Down Expand Up @@ -92,7 +91,7 @@ private Optional<List<Object>> parseArguments(SlashCommandDefinition command, Sl
Optional<?> parsed = adapter.apply(raw, event);
if (parsed.isEmpty()) {
log.debug("Type adapting failed!");
new MessageReply(event, command).reply(
new MessageReply(event, command, dispatchingContext.globalReplyConfig()).reply(
errorMessageFactory.getTypeAdaptingFailedMessage(Helpers.errorContext(event, command), Arrays.asList(input))
);
return Optional.empty();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ public MessageReply(@NotNull GenericInteractionCreateEvent event,
this.builder = new MessageCreateBuilder();
}

/// Constructs a new MessageReply.
///
/// @param event the corresponding [GenericInteractionCreateEvent]
/// @param definition the corresponding [InteractionDefinition]. This is mostly needed by the [ConfigurableReply]
public MessageReply(@NotNull GenericInteractionCreateEvent event, @NotNull InteractionDefinition definition) {
this(event, definition, definition.replyConfig());
}

/// Constructs a new MessageReply.
///
/// @param reply the [MessageReply] to copy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.github.kaktushose.jda.commands.definitions.description.ParameterDescription;
import com.github.kaktushose.jda.commands.definitions.interactions.InteractionDefinition;
import com.github.kaktushose.jda.commands.definitions.interactions.MethodBuildContext;
import com.github.kaktushose.jda.commands.dispatching.reply.GlobalReplyConfig;
import com.github.kaktushose.jda.commands.embeds.error.ErrorMessageFactory.ErrorContext;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
Expand Down Expand Up @@ -84,11 +83,12 @@ public static Set<String> permissions(@NotNull MethodBuildContext context) {
/// Constructs the [InteractionDefinition.ReplyConfig ReplyConfig] based on the passed [Method].
///
/// @param method the [Method] to use
/// @param fallbackReplyConfig the [InteractionDefinition.ReplyConfig] to be used as a fallback
/// @return the [InteractionDefinition.ReplyConfig ReplyConfig]
/// @implNote This will first attempt to use the [ReplyConfig] annotation of the method and then of the class.
/// If neither is present will fall back to the [GlobalReplyConfig].
/// If neither is present will fall back to the provided fallback.
@NotNull
public static InteractionDefinition.ReplyConfig replyConfig(@NotNull Method method) {
public static InteractionDefinition.ReplyConfig replyConfig(@NotNull Method method, InteractionDefinition.ReplyConfig fallbackReplyConfig) {
var global = method.getDeclaringClass().getAnnotation(ReplyConfig.class);
var local = method.getAnnotation(ReplyConfig.class);

Expand Down

0 comments on commit 6e1c0bd

Please sign in to comment.