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());
}
}
}
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);
}
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);
}
}
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);
}
}
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));
}
}
Aggregations