Search in sources :

Example 1 with CommandContribution

use of net.robinfriedli.aiode.entities.xml.CommandContribution in project aiode by robinfriedli.

the class StoredScript method asCommand.

public ScriptCommand asCommand(CommandManager commandManager, CommandContext context, String input) {
    CommandContribution scriptCommandContribution = commandManager.getCommandContribution("script");
    String commandBody = input.substring(identifier.length()).trim();
    ScriptCommand scriptCommand = (ScriptCommand) scriptCommandContribution.instantiate(commandManager, context, commandBody);
    scriptCommand.setScript(this);
    return scriptCommand;
}
Also used : ScriptCommand(net.robinfriedli.aiode.command.commands.scripting.ScriptCommand) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution)

Example 2 with CommandContribution

use of net.robinfriedli.aiode.entities.xml.CommandContribution in project aiode by robinfriedli.

the class PermissionCommand method getSelectedTargets.

private Set<? extends PermissionTarget> getSelectedTargets() {
    Set<? extends PermissionTarget> selectedRootTargets = getSelectedRootTargets();
    if (!argumentSet("argument")) {
        return selectedRootTargets;
    }
    String argumentValue;
    if (argumentSet("all")) {
        argumentValue = getArgumentValueOrElse("argument", getCommandInput());
    } else {
        argumentValue = getArgumentValueWithTypeOrElse("argument", String.class, null);
    }
    if (argumentValue == null) {
        return getAllArguments(selectedRootTargets);
    }
    List<String> argumentIdentifiers = COMMA_SPLITTER.splitToList(argumentValue);
    if (argumentIdentifiers.isEmpty()) {
        return getAllArguments(selectedRootTargets);
    }
    Set<CommandArgument> selectedArguments = Sets.newHashSet();
    for (String argumentIdentifier : argumentIdentifiers) {
        for (PermissionTarget selectedRootTarget : selectedRootTargets) {
            if (selectedRootTarget instanceof CommandContribution) {
                CommandContribution commandContribution = (CommandContribution) selectedRootTarget;
                CommandArgument argument = commandContribution.getArgument(argumentIdentifier);
                if (argument != null) {
                    selectedArguments.add(argument);
                } else {
                    throw new InvalidCommandException(String.format("No such argument '%s' on command '%s'.", argumentIdentifier, commandContribution.getIdentifier()));
                }
            } else {
                throw new InvalidCommandException(String.format("Cannot find argument '%s' on permission target '%s' as it is not a command.", argumentIdentifier, selectedRootTarget.getFullPermissionTargetIdentifier()));
            }
        }
    }
    return selectedArguments;
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) CommandArgument(net.robinfriedli.aiode.command.argument.CommandArgument) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget)

Example 3 with CommandContribution

use of net.robinfriedli.aiode.entities.xml.CommandContribution in project aiode by robinfriedli.

the class CommandManager method instantiateCommandForContext.

public Optional<AbstractCommand> instantiateCommandForContext(CommandContext context, Session session, boolean includeScripts) {
    String commandBody = context.getCommandBody();
    if (commandBody.isBlank()) {
        return Optional.empty();
    }
    String formattedCommandInput = commandBody.toLowerCase();
    CommandContribution commandContribution = getCommandContributionForInput(commandBody);
    AbstractCommand commandInstance;
    // find a preset where the preset name matches the beginning of the command, find the longest matching preset name
    // corresponds to lower(name) = substring(lower('" + commandBody.replaceAll("'", "''") + "'), 0, length(name) + 1)
    Optional<Preset> optionalPreset = queryBuilderFactory.find(Preset.class).where((cb, root) -> cb.equal(cb.lower(root.get("name")), cb.substring(cb.literal(formattedCommandInput), cb.literal(1), cb.length(root.get("name"))))).orderBy((root, cb) -> cb.desc(cb.length(root.get("name")))).build(session).setMaxResults(1).setCacheable(true).uniqueResultOptional();
    Optional<StoredScript> optionalStoredScript;
    if (includeScripts) {
        optionalStoredScript = queryBuilderFactory.find(StoredScript.class).where(((cb, root, subQueryFactory) -> cb.and(cb.equal(cb.lower(root.get("identifier")), cb.substring(cb.literal(formattedCommandInput), cb.literal(1), cb.length(root.get("identifier")))), cb.equal(root.get("scriptUsage"), subQueryFactory.createUncorrelatedSubQuery(StoredScript.ScriptUsage.class, "pk").where((cb1, root1) -> cb1.equal(root1.get("uniqueId"), "script")).build(session))))).orderBy((root, cb) -> cb.desc(cb.length(root.get("identifier")))).build(session).setMaxResults(1).setCacheable(true).uniqueResultOptional();
    } else {
        optionalStoredScript = Optional.empty();
    }
    // A ∧ B ∧ C
    if (commandContribution != null && optionalPreset.isPresent() && optionalStoredScript.isPresent()) {
        Preset preset = optionalPreset.get();
        String identifier = commandContribution.getIdentifier();
        StoredScript storedScript = optionalStoredScript.get();
        if (preset.getName().length() > identifier.length() && preset.getName().length() >= storedScript.getIdentifier().length()) {
            commandInstance = preset.instantiateCommand(this, context, commandBody);
        } else if (storedScript.getIdentifier().length() > identifier.length() && storedScript.getIdentifier().length() > preset.getName().length()) {
            commandInstance = storedScript.asCommand(this, context, commandBody);
        } else {
            String commandInput = commandBody.substring(identifier.length()).trim();
            commandInstance = commandContribution.instantiate(this, context, commandInput);
        }
    } else if (commandContribution != null && optionalPreset.isPresent()) {
        // A ∧ B ∧ !C
        String identifier = commandContribution.getIdentifier();
        Preset preset = optionalPreset.get();
        if (preset.getName().length() > identifier.length()) {
            commandInstance = preset.instantiateCommand(this, context, commandBody);
        } else {
            String commandInput = commandBody.substring(identifier.length()).trim();
            commandInstance = commandContribution.instantiate(this, context, commandInput);
        }
    } else if (optionalPreset.isPresent() && optionalStoredScript.isPresent()) {
        // !A ∧ B ∧ C
        Preset preset = optionalPreset.get();
        StoredScript storedScript = optionalStoredScript.get();
        if (storedScript.getIdentifier().length() > preset.getName().length()) {
            commandInstance = storedScript.asCommand(this, context, commandBody);
        } else {
            commandInstance = preset.instantiateCommand(this, context, commandBody);
        }
    } else if (commandContribution != null && optionalStoredScript.isPresent()) {
        // A ∧ !B ∧ C
        StoredScript storedScript = optionalStoredScript.get();
        String identifier = commandContribution.getIdentifier();
        if (storedScript.getIdentifier().length() > identifier.length()) {
            commandInstance = storedScript.asCommand(this, context, commandBody);
        } else {
            String commandInput = commandBody.substring(identifier.length()).trim();
            commandInstance = commandContribution.instantiate(this, context, commandInput);
        }
    } else if (optionalStoredScript.isPresent()) {
        // !A ∧ !B ∧ C
        commandInstance = optionalStoredScript.get().asCommand(this, context, commandBody);
    } else if (optionalPreset.isPresent()) {
        // !A ∧ B ∧ !C
        commandInstance = optionalPreset.get().instantiateCommand(this, context, commandBody);
    } else if (commandContribution != null) {
        // A ∧ !B ∧ !C
        String commandInput = commandBody.substring(commandContribution.getIdentifier().length()).trim();
        commandInstance = commandContribution.instantiate(this, context, commandInput);
    } else {
        // !A ∧ !B ∧ !C
        return Optional.empty();
    }
    return Optional.of(commandInstance);
}
Also used : Arrays(java.util.Arrays) CommandInterceptorContribution(net.robinfriedli.aiode.entities.xml.CommandInterceptorContribution) ThreadExecutionQueue(net.robinfriedli.aiode.concurrent.ThreadExecutionQueue) JxpBackend(net.robinfriedli.jxp.api.JxpBackend) LoggerFactory(org.slf4j.LoggerFactory) Session(org.hibernate.Session) Preset(net.robinfriedli.aiode.entities.Preset) Value(org.springframework.beans.factory.annotation.Value) ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) Lists(com.google.common.collect.Lists) EventWaiter(net.robinfriedli.aiode.discord.listeners.EventWaiter) CommandInterceptorChain(net.robinfriedli.aiode.command.interceptor.CommandInterceptorChain) ThreadContext(net.robinfriedli.aiode.concurrent.ThreadContext) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) Resource(org.springframework.core.io.Resource) Context(net.robinfriedli.jxp.persist.Context) CommandListener(net.robinfriedli.aiode.discord.listeners.CommandListener) Logger(org.slf4j.Logger) MessageService(net.robinfriedli.aiode.discord.MessageService) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) RateLimitException(net.robinfriedli.aiode.exceptions.RateLimitException) Set(java.util.Set) IOException(java.io.IOException) Conditions(net.robinfriedli.jxp.queries.Conditions) Collectors(java.util.stream.Collectors) CommandExceptionHandlerExecutor(net.robinfriedli.aiode.exceptions.handler.CommandExceptionHandlerExecutor) Aiode(net.robinfriedli.aiode.Aiode) List(java.util.List) Component(org.springframework.stereotype.Component) StoredScript(net.robinfriedli.aiode.entities.StoredScript) Optional(java.util.Optional) CommandExecutionTask(net.robinfriedli.aiode.concurrent.CommandExecutionTask) CommandRuntimeException(net.robinfriedli.aiode.exceptions.CommandRuntimeException) ScriptCommandInterceptor(net.robinfriedli.aiode.command.interceptor.interceptors.ScriptCommandInterceptor) StoredScript(net.robinfriedli.aiode.entities.StoredScript) Preset(net.robinfriedli.aiode.entities.Preset) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution)

Example 4 with CommandContribution

use of net.robinfriedli.aiode.entities.xml.CommandContribution in project aiode by robinfriedli.

the class Preset method instantiateCommand.

public AbstractCommand instantiateCommand(CommandManager commandManager, CommandContext context, String input) {
    CommandContribution presetContribution = commandManager.getCommandContributionForInput(preset);
    if (presetContribution == null) {
        throw new InvalidCommandException("Invalid preset, no command found");
    }
    String commandBody = preset.substring(presetContribution.getIdentifier().length()).trim();
    String var = input.substring(name.length()).trim();
    if (commandBody.contains("%s")) {
        commandBody = String.format(commandBody, var);
    } else if (!var.isBlank()) {
        throw new InvalidCommandException("This preset does not have a variable");
    }
    return presetContribution.instantiate(commandManager, context, commandBody);
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution)

Aggregations

CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)4 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)3 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Aiode (net.robinfriedli.aiode.Aiode)1 PermissionTarget (net.robinfriedli.aiode.command.PermissionTarget)1 CommandArgument (net.robinfriedli.aiode.command.argument.CommandArgument)1 ScriptCommand (net.robinfriedli.aiode.command.commands.scripting.ScriptCommand)1 CommandInterceptorChain (net.robinfriedli.aiode.command.interceptor.CommandInterceptorChain)1 ScriptCommandInterceptor (net.robinfriedli.aiode.command.interceptor.interceptors.ScriptCommandInterceptor)1 CommandExecutionTask (net.robinfriedli.aiode.concurrent.CommandExecutionTask)1 ExecutionContext (net.robinfriedli.aiode.concurrent.ExecutionContext)1 ThreadContext (net.robinfriedli.aiode.concurrent.ThreadContext)1 ThreadExecutionQueue (net.robinfriedli.aiode.concurrent.ThreadExecutionQueue)1 MessageService (net.robinfriedli.aiode.discord.MessageService)1