Search in sources :

Example 1 with IPlaylistItem

use of se.michaelthelin.spotify.model_objects.IPlaylistItem in project ascent_bot by FeroniK.

the class SpotifyAudioSource method getPlaylist.

public void getPlaylist(CommandContext ctx, String url) {
    final Matcher res = SPOTIFY_PLAYLIST_REGEX.matcher(url);
    if (!res.matches()) {
        return;
    }
    try {
        GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(ctx.getGuild());
        final Playlist spotifyPlaylist = this.spi.getPlaylist(res.group(res.groupCount())).build().execute();
        List<PlaylistTrack> playlistTracks = List.of(spotifyPlaylist.getTracks().getItems());
        if (playlistTracks.isEmpty()) {
            return;
        }
        final int originalSize = playlistTracks.size();
        if (originalSize > musicManager.scheduler.MAX_QUEUE_SIZE) {
            playlistTracks = playlistTracks.subList(0, musicManager.scheduler.MAX_QUEUE_SIZE);
        }
        final List<Track> finalPlaylist = new ArrayList<>();
        for (final PlaylistTrack playlistTrack : playlistTracks) {
            if (playlistTrack.getIsLocal()) {
                continue;
            }
            final IPlaylistItem item = playlistTrack.getTrack();
            if (item instanceof Track) {
                Track track = (Track) item;
                finalPlaylist.add(track);
            }
        }
        ctx.getChannel().sendMessageEmbeds(Embeds.createBuilder(null, "Spotify Playlist Loaded : Adding " + finalPlaylist.size() + " Tracks to the queue", null, null, null).build()).queue();
        for (Track track : finalPlaylist) {
            final String query = "ytsearch:" + track.getName() + " " + track.getArtists()[0].getName();
            PlayerManager.getInstance().audioPlayerManager.loadItemOrdered(musicManager, query, new AudioLoadResultHandler() {

                @Override
                public void trackLoaded(AudioTrack track) {
                }

                @Override
                public void playlistLoaded(AudioPlaylist playlist) {
                    boolean empty = playlist.getTracks().isEmpty();
                    if (empty) {
                        return;
                    }
                    AudioTrack audioTrack = playlist.getTracks().get(0);
                    audioTrack.setUserData(ctx.getAuthor().getIdLong());
                    musicManager.scheduler.queue(audioTrack);
                }

                @Override
                public void noMatches() {
                }

                @Override
                public void loadFailed(FriendlyException exception) {
                }
            });
        }
    } catch (IOException | SpotifyWebApiException | ParseException e) {
        e.printStackTrace();
    }
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) Matcher(java.util.regex.Matcher) GuildMusicManager(me.fero.ascent.lavaplayer.GuildMusicManager) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) IPlaylistItem(se.michaelthelin.spotify.model_objects.IPlaylistItem) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) ParseException(org.apache.hc.core5.http.ParseException) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)

Aggregations

AudioLoadResultHandler (com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler)1 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)1 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)1 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 GuildMusicManager (me.fero.ascent.lavaplayer.GuildMusicManager)1 ParseException (org.apache.hc.core5.http.ParseException)1 SpotifyWebApiException (se.michaelthelin.spotify.exceptions.SpotifyWebApiException)1 IPlaylistItem (se.michaelthelin.spotify.model_objects.IPlaylistItem)1