Created
May 18, 2018 20:07
-
-
Save Senither/a7d20918ca9df4a7fe681066a93c9b28 to your computer and use it in GitHub Desktop.
Middleware stack for Ava that adds the system command check for all commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.avairebot.middleware; | |
import com.avairebot.AvaIre; | |
import com.avairebot.commands.CommandContainer; | |
import com.avairebot.contracts.commands.Command; | |
import com.avairebot.contracts.middleware.Middleware; | |
import com.avairebot.handlers.DatabaseEventHolder; | |
import com.avairebot.middleware.global.IncrementMetricsForCommand; | |
import com.avairebot.middleware.global.IsCategoryEnabled; | |
import com.avairebot.middleware.global.ProcessCommand; | |
import net.dv8tion.jda.core.entities.Message; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.ListIterator; | |
public class MiddlewareStack { | |
private static ProcessCommand PROCESS_COMMAND; | |
private static IsCategoryEnabled IS_CATEGORY_ENABLED; | |
private static IncrementMetricsForCommand INCREMENT_METRICS_FOR_COMMAND; | |
private static IsBotAdminMiddleware IS_BOT_ADMIN_MIDDLEWARE; | |
private final Message message; | |
private final CommandContainer command; | |
private final List<MiddlewareContainer> middlewares = new ArrayList<>(); | |
private final DatabaseEventHolder databaseEventHolder; | |
private final boolean mentionableCommand; | |
private int index = -1; | |
public MiddlewareStack(Message message, CommandContainer command, DatabaseEventHolder databaseEventHolder, boolean mentionableCommand) { | |
this.message = message; | |
this.command = command; | |
this.mentionableCommand = mentionableCommand; | |
this.databaseEventHolder = databaseEventHolder; | |
middlewares.add(new MiddlewareContainer(PROCESS_COMMAND)); | |
buildMiddlewareStack(); | |
middlewares.add(new MiddlewareContainer(IS_CATEGORY_ENABLED)); | |
middlewares.add(new MiddlewareContainer(INCREMENT_METRICS_FOR_COMMAND)); | |
middlewares.add(new MiddlewareContainer(IS_BOT_ADMIN_MIDDLEWARE)); | |
} | |
public MiddlewareStack(Message message, CommandContainer command, DatabaseEventHolder databaseEventHolder) { | |
this(message, command, databaseEventHolder, false); | |
} | |
/** | |
* Builds the global messages so they can be used when building the middleware stack. | |
* | |
* @param avaire The AvaIre application instance. | |
*/ | |
static void buildGlobalMiddlewares(AvaIre avaire) { | |
PROCESS_COMMAND = new ProcessCommand(avaire); | |
IS_CATEGORY_ENABLED = new IsCategoryEnabled(avaire); | |
INCREMENT_METRICS_FOR_COMMAND = new IncrementMetricsForCommand(avaire); | |
IS_BOT_ADMIN_MIDDLEWARE = new IsBotAdminMiddleware(avaire); | |
} | |
/** | |
* Builds the middleware stack from the commands {@link Command#getMiddleware() getMiddleware()} method. | |
*/ | |
private void buildMiddlewareStack() { | |
List<String> middleware = command.getCommand().getMiddleware(); | |
if (middleware.isEmpty()) { | |
return; | |
} | |
ListIterator middlewareIterator = middleware.listIterator(middleware.size()); | |
while (middlewareIterator.hasPrevious()) { | |
String previous = (String) middlewareIterator.previous(); | |
String[] split = previous.split(":"); | |
Middleware middlewareReference = MiddlewareHandler.getMiddleware(split[0]); | |
if (middlewareReference == null) { | |
continue; | |
} | |
if (split.length == 1) { | |
middlewares.add(new MiddlewareContainer(middlewareReference)); | |
continue; | |
} | |
middlewares.add(new MiddlewareContainer(middlewareReference, split[1].split(","))); | |
} | |
} | |
/** | |
* Jumps to the next middleware in the stack, the end of the stack should | |
* always be the {@link ProcessCommand Process Command} middleware in | |
* order for the command to be invoked. | |
* | |
* @return <code>True</code> if the next middleware in the stack executed successfully, <code>False</code> otherwise. | |
*/ | |
public boolean next() { | |
if (index == -1) { | |
index = middlewares.size(); | |
} | |
MiddlewareContainer middlewareContainer = middlewares.get(--index); | |
return middlewareContainer | |
.getMiddleware() | |
.handle(message, this, middlewareContainer.getArguments()); | |
} | |
/** | |
* Gets the {@link Command command} the middleware stack is running for. | |
* | |
* @return The {@link Command command} the middleware stack is running for. | |
*/ | |
public Command getCommand() { | |
return command.getCommand(); | |
} | |
/** | |
* Gets the {@link CommandContainer command container} the middleware stack is running for. | |
* | |
* @return the {@link CommandContainer command container} the middleware stack is running for. | |
*/ | |
public CommandContainer getCommandContainer() { | |
return command; | |
} | |
/** | |
* Returns <code>True</code> if the command was invoked through mentioning the bot first. | |
* | |
* @return <code>True</code> if the command was invoked through mentioning the bot first. | |
*/ | |
public boolean isMentionableCommand() { | |
return mentionableCommand; | |
} | |
/** | |
* Returns the {@link DatabaseEventHolder database event holder} object for the given | |
* message, the object can be used to get the database record for the guild or user. | |
* | |
* @return The {@link DatabaseEventHolder database event holder} object. | |
*/ | |
public DatabaseEventHolder getDatabaseEventHolder() { | |
return databaseEventHolder; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment