Search in sources :

Example 26 with GuildMusicManager

use of me.fero.ascent.lavaplayer.GuildMusicManager 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

GuildMusicManager (me.fero.ascent.lavaplayer.GuildMusicManager)26 TextChannel (net.dv8tion.jda.api.entities.TextChannel)21 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)15 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)13 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)8 Member (net.dv8tion.jda.api.entities.Member)8 AudioLoadResultHandler (com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler)5 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)5 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 ParseException (org.apache.hc.core5.http.ParseException)4 SpotifyWebApiException (se.michaelthelin.spotify.exceptions.SpotifyWebApiException)4 List (java.util.List)3 Matcher (java.util.regex.Matcher)3 AudioManager (net.dv8tion.jda.api.managers.AudioManager)3 AudioTrackInfo (com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)2 HashMap (java.util.HashMap)2 SelectionMenu (net.dv8tion.jda.api.interactions.components.selections.SelectionMenu)2 EventWaiter (com.jagrosh.jdautilities.commons.waiter.EventWaiter)1