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();
}
}
Aggregations