Search in sources :

Example 6 with PlaylistItem

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

the class SearchCommand method listLocalList.

private void listLocalList() {
    if (getCommandInput().isBlank()) {
        Session session = getContext().getSession();
        List<Playlist> playlists = getQueryBuilderFactory().find(Playlist.class).build(session).getResultList();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        if (playlists.isEmpty()) {
            embedBuilder.setDescription("No playlists");
        } else {
            EmbedTable table = new EmbedTable(embedBuilder);
            table.addColumn("Playlist", playlists, Playlist::getName);
            table.addColumn("Duration", playlists, playlist -> Util.normalizeMillis(playlist.getDuration()));
            table.addColumn("Items", playlists, playlist -> String.valueOf(playlist.getSize()));
            table.build();
        }
        sendMessage(embedBuilder);
    } else {
        Playlist playlist = SearchEngine.searchLocalList(getContext().getSession(), getCommandInput());
        if (playlist == null) {
            throw new NoResultsFoundException(String.format("No local list found for '%s'", getCommandInput()));
        }
        String createdUserId = playlist.getCreatedUserId();
        String createdUser;
        if (createdUserId.equals("system")) {
            createdUser = playlist.getCreatedUser();
        } else {
            ShardManager shardManager = Aiode.get().getShardManager();
            User userById;
            try {
                userById = shardManager.retrieveUserById(createdUserId).complete();
            } catch (ErrorResponseException e) {
                if (e.getErrorResponse() == ErrorResponse.UNKNOWN_USER) {
                    userById = null;
                } else {
                    throw e;
                }
            }
            createdUser = userById != null ? userById.getName() : playlist.getCreatedUser();
        }
        SpringPropertiesConfig springPropertiesConfig = Aiode.get().getSpringPropertiesConfig();
        String baseUri = springPropertiesConfig.requireApplicationProperty("aiode.server.base_uri");
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.addField("Name", playlist.getName(), true);
        embedBuilder.addField("Duration", Util.normalizeMillis(playlist.getDuration()), true);
        embedBuilder.addField("Created by", createdUser, true);
        embedBuilder.addField("Tracks", String.valueOf(playlist.getSize()), true);
        embedBuilder.addBlankField(false);
        String url = baseUri + String.format("/list?name=%s&guildId=%s", URLEncoder.encode(playlist.getName(), StandardCharsets.UTF_8), playlist.getGuildId());
        embedBuilder.addField("First tracks:", "[Full list](" + url + ")", false);
        List<PlaylistItem> items = playlist.getItemsSorted();
        Util.appendEmbedList(embedBuilder, items.size() > 5 ? items.subList(0, 5) : items, item -> item.display() + " - " + Util.normalizeMillis(item.getDuration()), "Track - Duration");
        sendWithLogo(embedBuilder);
    }
}
Also used : User(net.dv8tion.jda.api.entities.User) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) Playlist(net.robinfriedli.aiode.entities.Playlist) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Session(org.hibernate.Session)

Example 7 with PlaylistItem

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

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

the class MoveCommand method moveIndexRange.

private void moveIndexRange(int start, int end, int targetIndex, Playlist playlist) {
    List<PlaylistItem> itemsSorted = playlist.getItemsSorted();
    invoke(() -> {
        boolean movedDown = start < targetIndex;
        int range = end - start + 1;
        if (movedDown) {
            for (int i = start; i <= targetIndex; i++) {
                PlaylistItem item = itemsSorted.get(i);
                if (i <= end) {
                    // set items are within the defined start - end range to the desired index
                    // note that the end item ends up at the targetIndex because of how the items are moved up
                    // e.g moving 4 - 6 to 10
                    // before                   after
                    // 3. track 3               3. track 3
                    // 4. to move 1             4. track 7
                    // 5. to move 2             5. track 8
                    // 6. to move 3             6. track 9
                    // 7. track 7               7. track 10
                    // 8. track 8               8. to move 1
                    // 9. track 9               9. to move 2
                    // 10. track 10             10. to move 3
                    // 11. track 11             11. track 11
                    item.setIndex(targetIndex - (end - i));
                } else {
                    item.setIndex(item.getIndex() - range);
                }
            }
            String message = String.format("Moved items %d through %d behind item '%s'", start + 1, end + 1, itemsSorted.get(targetIndex).display());
            successMessageBuilder.append(message);
        } else {
            // 17. track 17                     17. track 17
            for (int i = end; i >= targetIndex; i--) {
                PlaylistItem item = itemsSorted.get(i);
                if (i >= start) {
                    item.setIndex(targetIndex + i - start);
                } else {
                    item.setIndex(item.getIndex() + range);
                }
            }
            String message = String.format("Moved items %d through %d ahead of item '%s'", start + 1, end + 1, itemsSorted.get(targetIndex).display());
            successMessageBuilder.append(message);
        }
    });
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem)

Example 9 with PlaylistItem

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

the class MoveCommand method moveSingleIndex.

private void moveSingleIndex(int index, int targetIndex, Playlist playlist) {
    List<PlaylistItem> itemsSorted = playlist.getItemsSorted();
    PlaylistItem itemToMove = itemsSorted.get(index);
    invoke(() -> {
        if (index < targetIndex) {
            // move up items that were after the moving item but are now before
            for (int i = index + 1; i <= targetIndex; i++) {
                PlaylistItem item = itemsSorted.get(i);
                item.setIndex(item.getIndex() - 1);
            }
        } else {
            // move down items that were before the moving item but are now after
            for (int i = targetIndex; i < index; i++) {
                PlaylistItem item = itemsSorted.get(i);
                item.setIndex(item.getIndex() + 1);
            }
        }
        itemToMove.setIndex(targetIndex);
        successMessageBuilder.append("Moved item '").append(itemToMove.display()).append("' to index ").append(targetIndex + 1);
    });
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem)

Example 10 with PlaylistItem

use of net.robinfriedli.aiode.entities.PlaylistItem 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)

Aggregations

PlaylistItem (net.robinfriedli.aiode.entities.PlaylistItem)18 Playlist (net.robinfriedli.aiode.entities.Playlist)10 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)6 Session (org.hibernate.Session)6 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)4 List (java.util.List)3 HollowYouTubeVideo (net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo)3 Song (net.robinfriedli.aiode.entities.Song)3 UrlTrack (net.robinfriedli.aiode.entities.UrlTrack)3 Video (net.robinfriedli.aiode.entities.Video)3 User (net.dv8tion.jda.api.entities.User)2 ShardManager (net.dv8tion.jda.api.sharding.ShardManager)2 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)2 Playable (net.robinfriedli.aiode.audio.Playable)2 SpringPropertiesConfig (net.robinfriedli.aiode.boot.SpringPropertiesConfig)2 CommandContext (net.robinfriedli.aiode.command.CommandContext)2 CommandManager (net.robinfriedli.aiode.command.CommandManager)2 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)2 SearchEngine (net.robinfriedli.aiode.util.SearchEngine)2 Lists (com.google.common.collect.Lists)1