Search in sources :

Example 1 with RadioCommand

use of ml.duncte123.skybot.commands.music.RadioCommand in project SkyBot by duncte123.

the class AudioUtils method loadAndPlay.

/**
 * Loads a track and plays it if the bot isn't playing
 *
 * @param mng         The {@link GuildMusicManager MusicManager} for the guild
 * @param channel     The {@link net.dv8tion.jda.core.entities.MessageChannel channel} that the bot needs to send the messages to
 * @param trackUrlRaw The url from the track to play
 * @param addPlayList If the url is a playlist
 */
public void loadAndPlay(GuildMusicManager mng, final TextChannel channel, final String trackUrlRaw, final boolean addPlayList) {
    final String trackUrl;
    // Strip <>'s that prevent discord from embedding link resources
    if (trackUrlRaw.startsWith("<") && trackUrlRaw.endsWith(">")) {
        trackUrl = trackUrlRaw.substring(1, trackUrlRaw.length() - 1);
    } else {
        trackUrl = trackUrlRaw;
    }
    playerManager.loadItemOrdered(mng, trackUrl, new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack track) {
            String title = track.getInfo().title;
            if (track.getInfo().isStream) {
                Optional<RadioStream> stream = ((RadioCommand) AirUtils.COMMAND_MANAGER.getCommand("radio")).getRadioStreams().stream().filter(s -> s.getUrl().equals(track.getInfo().uri)).findFirst();
                if (stream.isPresent())
                    title = stream.get().getName();
            }
            String msg = "Adding to queue: " + title;
            if (mng.player.getPlayingTrack() == null) {
                msg += "\nand the Player has started playing;";
            }
            mng.scheduler.queue(track);
            MessageUtils.sendEmbed(channel, EmbedUtils.embedField(embedTitle, msg));
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            AudioTrack firstTrack = playlist.getSelectedTrack();
            List<AudioTrack> tracks = playlist.getTracks();
            if (tracks.size() == 0) {
                MessageUtils.sendEmbed(channel, EmbedUtils.embedField(embedTitle, "Error: This playlist is empty."));
                return;
            } else if (firstTrack == null) {
                firstTrack = playlist.getTracks().get(0);
            }
            String msg;
            if (addPlayList) {
                msg = "Adding **" + playlist.getTracks().size() + "** tracks to queue from playlist: " + playlist.getName();
                if (mng.player.getPlayingTrack() == null) {
                    msg += "\nand the Player has started playing;";
                }
                tracks.forEach(mng.scheduler::queue);
            } else {
                msg = "Adding to queue " + firstTrack.getInfo().title + " (first track of playlist " + playlist.getName() + ")";
                if (mng.player.getPlayingTrack() == null) {
                    msg += "\nand the Player has started playing;";
                }
                mng.scheduler.queue(firstTrack);
            }
            MessageUtils.sendEmbed(channel, EmbedUtils.embedField(embedTitle, msg));
        }

        @Override
        public void noMatches() {
            MessageUtils.sendEmbed(channel, EmbedUtils.embedField(embedTitle, "Nothing found by _" + trackUrl + "_"));
        }

        @Override
        public void loadFailed(FriendlyException exception) {
            MessageUtils.sendEmbed(channel, EmbedUtils.embedField(embedTitle, "Could not play: " + exception.getMessage() + "\nIf this happens often try another link or join our [support guild](https://discord.gg/NKM9Xtk) for more!"));
        }
    });
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) RadioCommand(ml.duncte123.skybot.commands.music.RadioCommand) Optional(java.util.Optional) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) List(java.util.List) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

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 List (java.util.List)1 Optional (java.util.Optional)1 RadioCommand (ml.duncte123.skybot.commands.music.RadioCommand)1