Search in sources :

Example 1 with StoredScript

use of net.robinfriedli.aiode.entities.StoredScript in project aiode by robinfriedli.

the class AlertScriptModificationInterceptor method alertScriptModification.

private void alertScriptModification(String verb, List<StoredScript> affectedEntities) {
    if (!affectedEntities.isEmpty()) {
        if (affectedEntities.size() == 1) {
            StoredScript script = affectedEntities.get(0);
            messageService.sendSuccess(String.format("%s script '%s'", verb, script.getIdentifier()), commandContext.getChannel());
        } else {
            messageService.sendSuccess(String.format("%s %d scripts", verb, affectedEntities.size()), commandContext.getChannel());
        }
    }
}
Also used : StoredScript(net.robinfriedli.aiode.entities.StoredScript)

Example 2 with StoredScript

use of net.robinfriedli.aiode.entities.StoredScript 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 3 with StoredScript

use of net.robinfriedli.aiode.entities.StoredScript in project aiode by robinfriedli.

the class AbstractScriptCommand method listAllScripts.

private void listAllScripts(QueryBuilderFactory queryBuilderFactory, Session session, CommandContext context) {
    List<StoredScript> storedScripts = queryBuilderFactory.find(StoredScript.class).where((cb, root, subQueryFactory) -> cb.equal(root.get("scriptUsage"), subQueryFactory.createUncorrelatedSubQuery(StoredScript.ScriptUsage.class, "pk").where((cb1, root1) -> cb1.equal(root1.get("uniqueId"), scriptUsageId)).build(session))).build(session).getResultList();
    EmbedBuilder embedBuilder = new EmbedBuilder();
    if (storedScripts.isEmpty()) {
        embedBuilder.setDescription(String.format("No %ss saved", scriptUsageId));
        getMessageService().sendTemporary(embedBuilder, context.getChannel());
    } else {
        embedBuilder.setDescription(String.format("Show a specific %s by entering its identifier", scriptUsageId));
        EmbedTable table = new EmbedTable(embedBuilder);
        table.addColumn("Identifier", storedScripts, StoredScript::getIdentifier);
        table.addColumn("Active", storedScripts, script -> String.valueOf(script.isActive()));
        table.build();
        sendMessage(embedBuilder);
    }
}
Also used : MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) CommandManager(net.robinfriedli.aiode.command.CommandManager) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Session(org.hibernate.Session) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) GroovyShell(groovy.lang.GroovyShell) GroovyVariableManager(net.robinfriedli.aiode.scripting.GroovyVariableManager) Aiode(net.robinfriedli.aiode.Aiode) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) List(java.util.List) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) CommandContext(net.robinfriedli.aiode.command.CommandContext) StoredScript(net.robinfriedli.aiode.entities.StoredScript) Optional(java.util.Optional) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) ExceptionUtils(net.robinfriedli.aiode.exceptions.ExceptionUtils) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) StoredScript(net.robinfriedli.aiode.entities.StoredScript) EmbedTable(net.robinfriedli.aiode.util.EmbedTable)

Example 4 with StoredScript

use of net.robinfriedli.aiode.entities.StoredScript in project aiode by robinfriedli.

the class AbstractScriptCommand method doRun.

@Override
public void doRun() {
    CommandContext context = getContext();
    Session session = context.getSession();
    QueryBuilderFactory queryBuilderFactory = Aiode.get().getQueryBuilderFactory();
    if (argumentSet("delete")) {
        StoredScript foundScript = findScript(session);
        invoke(() -> session.delete(foundScript));
    } else if (argumentSet("identifier") && !getCommandInput().isBlank()) {
        saveNewScript(queryBuilderFactory, session, context);
    } else if (argumentSet("activate")) {
        toggleActivation(false, session);
    } else if (argumentSet("deactivate")) {
        toggleActivation(true, session);
    } else if (getCommandInput().isBlank()) {
        if (argumentSet("identifier")) {
            sendScript(getArgumentValue("identifier"), session);
        } else {
            listAllScripts(queryBuilderFactory, session, context);
        }
    } else {
        sendScript(getCommandInput(), session);
    }
}
Also used : QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) CommandContext(net.robinfriedli.aiode.command.CommandContext) StoredScript(net.robinfriedli.aiode.entities.StoredScript) Session(org.hibernate.Session)

Example 5 with StoredScript

use of net.robinfriedli.aiode.entities.StoredScript in project aiode by robinfriedli.

the class AbstractScriptCommand method sendScript.

private void sendScript(String identifier, Session session) {
    Optional<StoredScript> storedScript = SearchEngine.searchScript(identifier, scriptUsageId, session);
    if (storedScript.isPresent()) {
        StoredScript script = storedScript.get();
        String firstLetter = scriptUsageId.length() > 0 ? scriptUsageId.substring(0, 1).toUpperCase() : "";
        String rest = scriptUsageId.length() > 1 ? scriptUsageId.substring(1) : "";
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setTitle(String.format("%s%s: %s", firstLetter, rest, script.getIdentifier()));
        embedBuilder.addField("Active", String.valueOf(script.isActive()), true);
        embedBuilder.addField("Groovy script", "```groovy" + System.lineSeparator() + script.getScript() + System.lineSeparator() + "```", false);
        sendMessage(embedBuilder);
    } else {
        throw new InvalidCommandException(String.format("No such %s '%s'", scriptUsageId, identifier));
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StoredScript(net.robinfriedli.aiode.entities.StoredScript)

Aggregations

StoredScript (net.robinfriedli.aiode.entities.StoredScript)11 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)6 Aiode (net.robinfriedli.aiode.Aiode)4 Session (org.hibernate.Session)4 GroovyShell (groovy.lang.GroovyShell)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 CommandContext (net.robinfriedli.aiode.command.CommandContext)3 QueryBuilderFactory (net.robinfriedli.aiode.persist.qb.QueryBuilderFactory)3 GroovyVariableManager (net.robinfriedli.aiode.scripting.GroovyVariableManager)3 List (java.util.List)2 Optional (java.util.Optional)2 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)2 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)2 MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)2 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1