Skip to content

Commit

Permalink
replace filter api by middleware api and rework permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaktushose committed Nov 11, 2023
1 parent 3f1873e commit b26f308
Show file tree
Hide file tree
Showing 41 changed files with 466 additions and 440 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,16 @@
<artifactId>annotations</artifactId>
<version>24.0.1</version>
</dependency>
<!-- TODO remove dev dependencies -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.4.6</version>
</dependency>
</dependencies>
</project>
17 changes: 1 addition & 16 deletions src/examples/embeds.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"insufficientPermissions": {
"title": "Insufficient Permissions",
"description": "`{prefix}{label}` requires specific permissions to be executed",
"description": "`{name}` requires specific permissions to be executed",
"color": "#ff0000",
"fields": [
{
Expand All @@ -10,21 +10,6 @@
}
]
},
"guildMuted": {
"title": "Insufficient Permissions",
"description": "This guild is muted!",
"color": "#ff0000"
},
"channelMuted": {
"title": "Insufficient Permissions",
"description": "This channel is muted!",
"color": "#ff0000"
},
"userMuted": {
"title": "Insufficient Permissions",
"description": "This channel is muted!",
"color": "#ff0000"
},
"typeAdaptingFailed": {
"title": "Syntax Error",
"description": "`{usage}`",
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/github/kaktushose/jda/commands/JDACommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.github.kaktushose.jda.commands.dispatching.DispatcherSupervisor;
import com.github.kaktushose.jda.commands.dispatching.RuntimeSupervisor;
import com.github.kaktushose.jda.commands.dispatching.adapter.TypeAdapterRegistry;
import com.github.kaktushose.jda.commands.dispatching.filter.FilterRegistry;
import com.github.kaktushose.jda.commands.dispatching.middleware.MiddlewareRegistry;
import com.github.kaktushose.jda.commands.dispatching.validation.ValidatorRegistry;
import com.github.kaktushose.jda.commands.reflect.ImplementationRegistry;
import com.github.kaktushose.jda.commands.reflect.InteractionRegistry;
Expand All @@ -30,7 +30,7 @@ public class JDACommands {
private final JDAContext jdaContext;
private final ImplementationRegistry implementationRegistry;
private final DispatcherSupervisor dispatcherSupervisor;
private final FilterRegistry filterRegistry;
private final MiddlewareRegistry middlewareRegistry;
private final TypeAdapterRegistry adapterRegistry;
private final ValidatorRegistry validatorRegistry;
private final DependencyInjector dependencyInjector;
Expand All @@ -43,7 +43,7 @@ protected JDACommands() {
jdaContext = null;
implementationRegistry = null;
runtimeSupervisor = null;
filterRegistry = null;
middlewareRegistry = null;
adapterRegistry = null;
validatorRegistry = null;
dependencyInjector = null;
Expand All @@ -63,12 +63,12 @@ private JDACommands(Object jda, Class<?> clazz, LocalizationFunction function, S
dependencyInjector = new DependencyInjector();
dependencyInjector.index(clazz, packages);

filterRegistry = new FilterRegistry();
middlewareRegistry = new MiddlewareRegistry();
adapterRegistry = new TypeAdapterRegistry();
validatorRegistry = new ValidatorRegistry();
implementationRegistry = new ImplementationRegistry(
dependencyInjector,
filterRegistry,
middlewareRegistry,
adapterRegistry,
validatorRegistry
);
Expand All @@ -81,7 +81,7 @@ private JDACommands(Object jda, Class<?> clazz, LocalizationFunction function, S

interactionRegistry.index(clazz, packages);

updater = new SlashCommandUpdater(this);
updater = new SlashCommandUpdater(this, function);
updater.updateAllCommands();
jdaContext.performTask(it -> it.addEventListener(dispatcherSupervisor));

Expand Down Expand Up @@ -225,12 +225,12 @@ public JDAContext getJdaContext() {
}

/**
* Gets the {@link FilterRegistry}.
* Gets the {@link MiddlewareRegistry}.
*
* @return the {@link FilterRegistry}
* @return the {@link MiddlewareRegistry}
*/
public FilterRegistry getFilterRegistry() {
return filterRegistry;
public MiddlewareRegistry getMiddlewareRegistry() {
return middlewareRegistry;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.kaktushose.jda.commands.annotations;

import com.github.kaktushose.jda.commands.annotations.constraints.Constraint;
import com.github.kaktushose.jda.commands.dispatching.filter.FilterRegistry.FilterPosition;
import com.github.kaktushose.jda.commands.dispatching.middleware.Priority;
import com.github.kaktushose.jda.commands.reflect.ImplementationRegistry;

import java.lang.annotation.*;
Expand All @@ -16,16 +16,18 @@
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {
public @interface Implementation {

/**
* Gets the {@link FilterPosition FilterPosition} to register the
* {@link com.github.kaktushose.jda.commands.dispatching.filter.Filter Filter} at. If the component is not a subtype
* of {@link com.github.kaktushose.jda.commands.dispatching.filter.Filter Filter}, this field can be ignored.
* Gets the {@link Priority} to register the
* {@link com.github.kaktushose.jda.commands.dispatching.middleware.Middleware Middleware} with. If this
* implementation is not a subtype
* of {@link com.github.kaktushose.jda.commands.dispatching.middleware.Middleware Middleware}, this field can be
* ignored.
*
* @return the {@link FilterPosition FilterPosition}
* @return the {@link Priority}
*/
FilterPosition position() default FilterPosition.UNKNOWN;
Priority priority() default Priority.NORMAL;

/**
* Gets the annotation the {@link com.github.kaktushose.jda.commands.dispatching.validation.Validator Validator}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.kaktushose.jda.commands.annotations.interactions;

import com.github.kaktushose.jda.commands.dispatching.middleware.impl.CooldownMiddleware;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -11,7 +13,7 @@
*
* @author Kaktushose
* @version 2.0.0
* @see com.github.kaktushose.jda.commands.dispatching.filter.impl.CooldownFilter CooldownFilter
* @see CooldownMiddleware CooldownFilter
* @since 1.0.0
*/
@Target({ElementType.METHOD, ElementType.TYPE})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.kaktushose.jda.commands.reflect.interactions.commands.SlashCommandDefinition;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.interactions.commands.localization.LocalizationFunction;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -80,12 +81,12 @@ private String[] resolveLabel(String label) {
*
* @return a {@link List} of {@link SlashCommandData}
*/
public List<SlashCommandData> getCommands() {
return root.getCommandData();
public List<SlashCommandData> getCommands(LocalizationFunction localizationFunction) {
return root.getCommandData(localizationFunction);
}

/**
* Gets the sanitized labels of all {@link SlashCommandData} returned by {@link #getCommands()}.
* Gets the sanitized labels of all {@link SlashCommandData} returned by {@link #getCommands(LocalizationFunction)} ()}.
* The labels will match the regex {@code ^[\w-]+$}. Furthermore, if the label consists of more than three spaces
* any additional space will be replaced with {@code _} due to Discords limitations on SubcommandGroups.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void adapt(@NotNull SlashCommandContext context) {
ErrorMessageFactory messageFactory = context.getImplementationRegistry().getErrorMessageFactory();

log.debug("Type adapting arguments...");
arguments.add(new CommandEvent<SlashCommandDefinition>(context));
arguments.add(new CommandEvent(context));
for (int i = 0; i < command.getActualParameters().size(); i++) {
ParameterDefinition parameter = command.getActualParameters().get(i);

Expand All @@ -158,7 +158,7 @@ public void adapt(@NotNull SlashCommandContext context) {
IllegalStateException exception = new IllegalStateException(
"Command input doesn't match parameter length! Please report this error the the devs of jda-commands."
);
context.setCancelled(true).setErrorMessage(messageFactory.getCommandExecutionFailedMessage(context, exception));
context.setCancelled(messageFactory.getCommandExecutionFailedMessage(context, exception));
throw exception;
}

Expand All @@ -184,7 +184,7 @@ public void adapt(@NotNull SlashCommandContext context) {
Optional<?> parsed = adapter.get().parse(raw, context);
if (parsed.isEmpty()) {
log.debug("Type adapting failed!");
context.setCancelled(true).setErrorMessage(messageFactory.getTypeAdaptingFailedMessage(context));
context.setCancelled(messageFactory.getTypeAdaptingFailedMessage(context));
break;
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit b26f308

Please sign in to comment.