Skip to content

Commit

Permalink
Fix command execution & add command level middlewares with parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShindouMihou committed Nov 13, 2021
1 parent d266ffa commit 9e33d65
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 69 deletions.
19 changes: 18 additions & 1 deletion src/main/java/pw/mihou/velen/builders/VelenCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pw.mihou.velen.impl.VelenCommandImpl;
import pw.mihou.velen.interfaces.*;
import pw.mihou.velen.interfaces.messages.types.VelenConditionalMessage;
import pw.mihou.velen.interfaces.middleware.VelenMiddleware;

import java.time.Duration;
import java.util.ArrayList;
Expand Down Expand Up @@ -37,6 +38,7 @@ public class VelenCommandBuilder {
private Duration cooldown;
private boolean serverOnly = false;
private boolean privateOnly = false;
private final List<String> middlewares = new ArrayList<>();
private VelenEvent velenEvent;
private Velen velen;

Expand Down Expand Up @@ -376,6 +378,17 @@ public VelenCommandBuilder setServerOnly(boolean serverOnly) {
return this;
}

/**
* Adds a middleware to this command.
*
* @param middlewares The middlewares to add.
* @return VelenCommandBuilder for chain calling methods.
*/
public VelenCommandBuilder addMiddlewares(String... middlewares) {
this.middlewares.addAll(Arrays.asList(middlewares));
return this;
}

/**
* Should this command be private-channel only?
*
Expand Down Expand Up @@ -469,7 +482,11 @@ public VelenCommand build() {
VelenCommandImpl.Handlers handlers = new VelenCommandImpl
.Handlers(velenEvent, velenSlashEvent, velenHybridHandler);

return new VelenCommandImpl(general, requires, conditional, settings, handlers, velen);
VelenCommandImpl.Warehouse warehouse = new VelenCommandImpl.Warehouse(middlewares
.stream().map(s -> velen.getMiddleware(s).orElseThrow(() -> new IllegalStateException("The middleware " + s + " couldn't be found.")))
.collect(Collectors.toList()));

return new VelenCommandImpl(general, requires, conditional, settings, handlers, warehouse, velen);

}

Expand Down
35 changes: 34 additions & 1 deletion src/main/java/pw/mihou/velen/impl/VelenCommandImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pw.mihou.velen.impl.commands.children.BaseMessageCommand;
import pw.mihou.velen.interfaces.*;
import pw.mihou.velen.interfaces.messages.types.VelenConditionalMessage;
import pw.mihou.velen.interfaces.middleware.VelenMiddleware;

import java.time.Duration;
import java.util.*;
Expand All @@ -22,18 +23,20 @@ public class VelenCommandImpl implements VelenCommand {
private final ConditionalCollective conditional;
private final Settings settings;
private final Handlers handlers;
private final Warehouse warehouse;

private final BaseMessageCommand baseMessageCommand = new BaseMessageCommand(this);
private final BaseInteractionCommand baseInteractionCommand = new BaseInteractionCommand(this);

public VelenCommandImpl(GeneralCollective general, RequireCollective requires, ConditionalCollective conditional,
Settings settings, Handlers handlers, Velen velen) {
Settings settings, Handlers handlers, Warehouse warehouse, Velen velen) {
this.general = general;
this.requires = requires;
this.conditional = conditional;
this.settings = settings;
this.handlers = handlers;
this.velen = velen;
this.warehouse = warehouse;
}

/**
Expand Down Expand Up @@ -150,6 +153,11 @@ public String getCategory() {
return general.category;
}

@Override
public List<VelenMiddleware> getMiddlewares() {
return warehouse.getMiddlewares();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -251,6 +259,31 @@ public Velen getVelen() {
return velen;
}


public static class Warehouse {
private final List<VelenMiddleware> middlewares;

/**
* Creates a brand new warehouse that can store
* middlewares and afterwares.
*
* @param middlewares The middlewares to store.
*/
public Warehouse(List<VelenMiddleware> middlewares) {
this.middlewares = middlewares;
}

/**
* Retrieves all the middlewares of this command.
*
* @return All the middlewares being used in this command.
*/
public List<VelenMiddleware> getMiddlewares() {
return middlewares;
}

}

public static class RequireCollective {
public final List<Long> requiredRoles;
public final List<Long> requiredUsers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pw.mihou.velen.ratelimiter.entities.RatelimitEntity;
import pw.mihou.velen.utils.Pair;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
Expand Down Expand Up @@ -81,8 +82,11 @@ public boolean applyRestraints(SlashCommandCreateEvent event) {
throw new IllegalStateException("The channel is somehow not present; this is possibly a change in Discord's side " +
"and may need to be addressed, please send an issue @ https:/ShindouMihou/Velen");

return !instance.getRequiredUsers().isEmpty() && !instance.getRequiredUsers()
.contains(event.getInteraction().getUser().getId());
if (!instance.getRequiredUsers().isEmpty() && !instance.getRequiredUsers()
.contains(event.getInteraction().getUser().getId()))
return false;

return true;
}


Expand All @@ -99,8 +103,11 @@ public boolean applyRestraints(MessageCreateEvent event) {
if (instance.isPrivateOnly() && event.getServer().isPresent())
return false;

return !instance.getRequiredUsers().isEmpty() && !instance.getRequiredUsers()
.contains(event.getMessageAuthor().getId());
if (!instance.getRequiredUsers().isEmpty() && !instance.getRequiredUsers()
.contains(event.getMessageAuthor().getId()))
return false;

return true;
}

/**
Expand Down Expand Up @@ -199,6 +206,55 @@ public boolean applySlashRestraints(SlashCommandCreateEvent event) {
.stream().allMatch(function -> function.apply(event));
}

/**
* Gets all the hybrid middlewares of this command.
*
* @return All the hybrid middlewares for this command.
*/
public List<VelenHybridMiddleware> getHybridMiddlewares() {
List<VelenHybridMiddleware> middlewares = new ArrayList<>();

if (instance.getVelen().findCategory(instance.getCategory()) != null) {
middlewares.addAll(instance.getVelen().findCategory(instance.getCategory()).getHybridMiddlewares());
}

middlewares.addAll(instance.getHybridMiddlewares());
return middlewares;
}

/**
* Gets all the message middlewares of this command.
*
* @return All the message middlewares for this command.
*/
public List<VelenMessageMiddleware> getMessageMiddlewares() {
List<VelenMessageMiddleware> middlewares = new ArrayList<>();

if (instance.getVelen().findCategory(instance.getCategory()) != null) {
middlewares.addAll(instance.getVelen().findCategory(instance.getCategory()).getMessageMiddlewares());
}

middlewares.addAll(instance.getMessageMiddlewares());
return middlewares;
}


/**
* Gets all the slash middlewares of this command.
*
* @return All the slash middlewares for this command.
*/
public List<VelenSlashMiddleware> getSlashMiddlewares() {
List<VelenSlashMiddleware> middlewares = new ArrayList<>();

if (instance.getVelen().findCategory(instance.getCategory()) != null) {
middlewares.addAll(instance.getVelen().findCategory(instance.getCategory()).getSlashMiddlewares());
}

middlewares.addAll(instance.getSlashMiddlewares());
return middlewares;
}

/**
* Dispatches an event for slash commands.
*
Expand All @@ -210,22 +266,20 @@ public void dispatch(SlashCommandCreateEvent event) {
instance.getName() + ".");

if (instance.getHybridHandler() == null) {
if (instance.getVelen().findCategory(instance.getCategory()) != null) {
Pair<Boolean, String> middlewareResponse = applySlashMiddlewares(
instance.getVelen().findCategory(instance.getCategory()).getSlashMiddlewares(),
event
);

if (!middlewareResponse.getLeft()) {
if (middlewareResponse.getRight() != null) {
event.getSlashCommandInteraction()
.createImmediateResponder()
.setContent(middlewareResponse.getRight())
.setFlags(InteractionCallbackDataFlag.EPHEMERAL)
.respond();
}
return;
Pair<Boolean, String> middlewareResponse = applySlashMiddlewares(
getSlashMiddlewares(),
event
);

if (!middlewareResponse.getLeft()) {
if (middlewareResponse.getRight() != null) {
event.getSlashCommandInteraction()
.createImmediateResponder()
.setContent(middlewareResponse.getRight())
.setFlags(InteractionCallbackDataFlag.EPHEMERAL)
.respond();
}
return;
}

instance.getInteractionHandler()
Expand All @@ -238,21 +292,19 @@ public void dispatch(SlashCommandCreateEvent event) {
} else {
VelenGeneralEvent e = new VelenGeneralEventImpl(instance.getName(), event, null, null, instance);

if (instance.getVelen().findCategory(instance.getCategory()) != null) {
Pair<Boolean, String> middlewareResponse = applyHybridMiddlewares(
instance.getVelen().findCategory(instance.getCategory()).getHybridMiddlewares(),
e
);

if (!middlewareResponse.getLeft()) {
if (middlewareResponse.getRight() != null) {
e.createResponder()
.setContent(middlewareResponse.getRight())
.setFlags(InteractionCallbackDataFlag.EPHEMERAL)
.respond();
}
return;
Pair<Boolean, String> middlewareResponse = applyHybridMiddlewares(
getHybridMiddlewares(),
e
);

if (!middlewareResponse.getLeft()) {
if (middlewareResponse.getRight() != null) {
e.createResponder()
.setContent(middlewareResponse.getRight())
.setFlags(InteractionCallbackDataFlag.EPHEMERAL)
.respond();
}
return;
}

instance.getHybridHandler().onEvent(e, e.createResponder(), e.getUser(), e.getArguments());
Expand All @@ -273,39 +325,35 @@ public void dispatch(MessageCreateEvent event, String[] args) {

if (instance.getHybridHandler() == null) {

if (instance.getVelen().findCategory(instance.getCategory()) != null) {
Pair<Boolean, String> middlewareResponse = applyMessageMiddlewares(
instance.getVelen().findCategory(instance.getCategory()).getMessageMiddlewares(),
event
);

if (!middlewareResponse.getLeft()) {
if (middlewareResponse.getRight() != null) {
event.getMessage().reply(middlewareResponse.getRight());
}
return;
Pair<Boolean, String> middlewareResponse = applyMessageMiddlewares(
getMessageMiddlewares(),
event
);

if (!middlewareResponse.getLeft()) {
if (middlewareResponse.getRight() != null) {
event.getMessage().reply(middlewareResponse.getRight());
}
return;
}

instance.getMessageHandler().onEvent(event, event.getMessage(), u, args, new VelenRoutedOptions(instance, event));
} else {
VelenGeneralEvent e = new VelenGeneralEventImpl(instance.getName(), null, event, args, instance);

if (instance.getVelen().findCategory(instance.getCategory()) != null) {
Pair<Boolean, String> middlewareResponse = applyHybridMiddlewares(
instance.getVelen().findCategory(instance.getCategory()).getHybridMiddlewares(),
e
);

if (!middlewareResponse.getLeft()) {
if (middlewareResponse.getRight() != null) {
e.createResponder()
.setContent(middlewareResponse.getRight())
.setFlags(InteractionCallbackDataFlag.EPHEMERAL)
.respond();
}
return;
Pair<Boolean, String> middlewareResponse = applyHybridMiddlewares(
getHybridMiddlewares(),
e
);

if (!middlewareResponse.getLeft()) {
if (middlewareResponse.getRight() != null) {
e.createResponder()
.setContent(middlewareResponse.getRight())
.setFlags(InteractionCallbackDataFlag.EPHEMERAL)
.respond();
}
return;
}

instance.getHybridHandler().onEvent(e, e.createResponder(), e.getUser(), e.getArguments());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/pw/mihou/velen/interfaces/Velen.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static VelenBuilder builder() {
* @param middleware The middleware itself.
* @return The Velen instance for chain-calling methods.
*/
default Velen addMiddleware(String name, VelenHybridMiddleware middleware) {
default Velen addHybridMiddleware(String name, VelenHybridMiddleware middleware) {
return storeMiddleware(name, middleware);
}

Expand All @@ -102,7 +102,7 @@ default Velen addMiddleware(String name, VelenHybridMiddleware middleware) {
* @param middleware The middleware itself.
* @return The Velen instance for chain-calling methods.
*/
default Velen addMiddleware(String name, VelenMessageMiddleware middleware) {
default Velen addMessageMiddleware(String name, VelenMessageMiddleware middleware) {
return storeMiddleware(name, middleware);
}

Expand All @@ -114,7 +114,7 @@ default Velen addMiddleware(String name, VelenMessageMiddleware middleware) {
* @param middleware The middleware itself.
* @return The Velen instance for chain-calling methods.
*/
default Velen addMiddleware(String name, VelenSlashMiddleware middleware) {
default Velen addSlashMiddleware(String name, VelenSlashMiddleware middleware) {
return storeMiddleware(name, middleware);
}

Expand Down
Loading

0 comments on commit 9e33d65

Please sign in to comment.