Search in sources :

Example 16 with InvalidCommandException

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);
        }
    });
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Session(org.hibernate.Session)

Example 17 with InvalidCommandException

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));
        }
    }
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) StringList(net.robinfriedli.stringlist.StringList) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) CommandManager(net.robinfriedli.aiode.command.CommandManager) Collection(java.util.Collection) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Session(org.hibernate.Session) List(java.util.List) Playlist(net.robinfriedli.aiode.entities.Playlist) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) CommandContext(net.robinfriedli.aiode.command.CommandContext) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) String(java.lang.String) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) Playlist(net.robinfriedli.aiode.entities.Playlist) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StringList(net.robinfriedli.stringlist.StringList) String(java.lang.String) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Session(org.hibernate.Session)

Example 18 with InvalidCommandException

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));
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StoredScript(net.robinfriedli.aiode.entities.StoredScript)

Example 19 with InvalidCommandException

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));
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StoredScript(net.robinfriedli.aiode.entities.StoredScript) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) Aiode(net.robinfriedli.aiode.Aiode) GroovyVariableManager(net.robinfriedli.aiode.scripting.GroovyVariableManager) GroovyShell(groovy.lang.GroovyShell)

Example 20 with InvalidCommandException

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));
    }
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) StoredScript(net.robinfriedli.aiode.entities.StoredScript)

Aggregations

InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)47 Guild (net.dv8tion.jda.api.entities.Guild)12 Session (org.hibernate.Session)11 CommandContext (net.robinfriedli.aiode.command.CommandContext)10 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)9 StringList (net.robinfriedli.stringlist.StringList)9 List (java.util.List)8 Playlist (net.robinfriedli.aiode.entities.Playlist)8 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)7 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)6 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)5 Collection (java.util.Collection)5 AudioPlayback (net.robinfriedli.aiode.audio.AudioPlayback)5 PlaylistItem (net.robinfriedli.aiode.entities.PlaylistItem)5 IOException (java.io.IOException)4 User (net.dv8tion.jda.api.entities.User)4 AudioManager (net.robinfriedli.aiode.audio.AudioManager)4 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)4 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)4 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)4