use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.
the class EmptyCommand method doRun.
@Override
public void doRun() {
Session session = getContext().getSession();
Playlist playlist = SearchEngine.searchLocalList(session, getCommandInput());
if (playlist == null) {
throw new NoResultsFoundException(String.format("No aiode playlist found for '%s'", getCommandInput()));
}
if (playlist.isEmpty()) {
throw new InvalidCommandException("Playlist is already empty");
}
invoke(() -> {
for (PlaylistItem item : playlist.getItems()) {
session.delete(item);
}
});
}
use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.
the class RemoveCommand method doRun.
@Override
public void doRun() {
Session session = getContext().getSession();
String playlistName = getArgumentValue("from");
Playlist playlist = SearchEngine.searchLocalList(session, playlistName);
if (playlist == null) {
throw new NoResultsFoundException(String.format("No local list found for '%s'", playlistName));
} else if (playlist.isEmpty()) {
throw new InvalidCommandException("Playlist is empty");
}
if (argumentSet("index")) {
StringList indices = StringList.createWithRegex(getCommandInput(), "-");
if (indices.size() == 1 || indices.size() == 2) {
indices.applyForEach(String::trim);
if (indices.size() == 1) {
int index = parse(indices.get(0));
checkIndex(index, playlist);
invoke(() -> session.delete(playlist.getItemsSorted().get(index - 1)));
} else {
int start = parse(indices.get(0));
int end = parse(indices.get(1));
checkIndex(start, playlist);
checkIndex(end, playlist);
if (end <= start) {
throw new InvalidCommandException("End index needs to be greater than start.");
}
invoke(() -> playlist.getItemsSorted().subList(start - 1, end).forEach(session::delete));
}
} else {
throw new InvalidCommandException("Expected one or two indices but found " + indices.size());
}
} else {
List<PlaylistItem> playlistItems = SearchEngine.searchPlaylistItems(playlist, getCommandInput());
if (playlistItems.size() == 1) {
invoke(() -> session.delete(playlistItems.get(0)));
} else if (playlistItems.isEmpty()) {
throw new NoResultsFoundException(String.format("No tracks found for '%s' on list '%s'", getCommandInput(), playlistName));
} else {
askQuestion(playlistItems, PlaylistItem::display, item -> valueOf(item.getIndex() + 1));
}
}
}
use of net.robinfriedli.aiode.exceptions.InvalidCommandException 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));
}
}
use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.
the class AbstractScriptCommand method saveNewScript.
private void saveNewScript(QueryBuilderFactory queryBuilderFactory, Session session, CommandContext context) {
String identifier = getArgumentValue("identifier");
String script = getCommandInput();
Optional<StoredScript> existingScript = SearchEngine.searchScript(identifier, scriptUsageId, session);
if (existingScript.isPresent()) {
throw new InvalidCommandException(String.format("%s with identifier '%s' already exists", scriptUsageId, identifier));
}
Aiode aiode = Aiode.get();
GroovyVariableManager groovyVariableManager = aiode.getGroovyVariableManager();
GroovyShell groovyShell;
if (argumentSet("privileged")) {
groovyShell = new GroovyShell();
} else {
groovyShell = new GroovyShell(aiode.getGroovySandboxComponent().getCompilerConfiguration());
}
groovyVariableManager.prepareShell(groovyShell);
try {
groovyShell.parse(script);
} catch (MultipleCompilationErrorsException e) {
throw new InvalidCommandException(String.format("Could not compile provided script:%s%s", System.lineSeparator(), ExceptionUtils.formatScriptCompilationError(e)));
}
StoredScript.ScriptUsage scriptUsage = queryBuilderFactory.find(StoredScript.ScriptUsage.class).where((cb, root) -> cb.equal(root.get("uniqueId"), scriptUsageId)).build(session).uniqueResult();
StoredScript storedScript = new StoredScript();
storedScript.setGuildId(context.getGuild().getIdLong());
storedScript.setIdentifier(identifier);
storedScript.setScript(script);
storedScript.setScriptUsage(scriptUsage);
storedScript.setActive(!argumentSet("deactivate"));
invoke(() -> session.persist(storedScript));
}
use of net.robinfriedli.aiode.exceptions.InvalidCommandException in project aiode by robinfriedli.
the class AbstractScriptCommand method findScript.
private StoredScript findScript(Session session) {
String identifier;
if (argumentSet("identifier")) {
identifier = getArgumentValue("identifier");
} else if (!getCommandInput().isBlank()) {
identifier = getCommandInput();
} else {
throw new InvalidCommandException("Expected either the command input or identifier argument to specify the target script.");
}
Optional<StoredScript> foundScript = SearchEngine.searchScript(identifier, scriptUsageId, session);
if (foundScript.isPresent()) {
return foundScript.get();
} else {
throw new InvalidCommandException(String.format("No saved %s found for '%s'", scriptUsageId, identifier));
}
}
Aggregations