Search in sources :

Example 1 with GetTrackRequest

use of se.michaelthelin.spotify.requests.data.tracks.GetTrackRequest in project ascent_bot by FeroniK.

the class SpotifyAudioSource method getTrack.

public void getTrack(CommandContext ctx, String url) {
    final Matcher res = SPOTIFY_TRACK_REGEX.matcher(url);
    if (!res.matches()) {
        return;
    }
    try {
        GetTrackRequest request = this.spi.getTrack(res.group(res.groupCount())).build();
        Track track = request.execute();
        LOGGER.info("Name: " + track.getName());
        LOGGER.info("Artist: " + track.getArtists()[0].getName());
        String query = "ytsearch:" + track.getName() + " " + track.getArtists()[0].getName();
        GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(ctx.getGuild());
        PlayerManager.getInstance().audioPlayerManager.loadItemOrdered(musicManager, query, new AudioLoadResultHandler() {

            @Override
            public void trackLoaded(AudioTrack track) {
                System.out.println("track loaded");
            }

            @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());
                ctx.getChannel().sendMessageEmbeds(Embeds.songEmbed(ctx.getMember(), audioTrack).setDescription("[" + audioTrack.getInfo().title + " - " + audioTrack.getInfo().author + "]" + "(" + track.getExternalUrls().get("spotify") + ")").build()).queue();
                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) IOException(java.io.IOException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) GetTrackRequest(se.michaelthelin.spotify.requests.data.tracks.GetTrackRequest) 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 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 GetTrackRequest (se.michaelthelin.spotify.requests.data.tracks.GetTrackRequest)1