Search in sources :

Example 6 with AudioPlaylist

use of com.sedmelluq.discord.lavaplayer.track.AudioPlaylist in project Rubicon by Rubicon-Bot.

the class MusicManager method loadSong.

public void loadSong(boolean force) {
    TextChannel textChannel = parsedCommandInvocation.getMessage().getTextChannel();
    boolean isURL = false;
    StringBuilder searchParam = new StringBuilder();
    for (int i = 0; i < args.length; i++) searchParam.append(args[i]);
    if (searchParam.toString().startsWith("http://") || searchParam.toString().startsWith("https://"))
        isURL = true;
    if (!isURL)
        searchParam.insert(0, "ytsearch: ");
    final EmbedBuilder embedBuilder = new EmbedBuilder();
    final boolean isURLFinal = isURL;
    playerManager.loadItemOrdered(getCurrentMusicManager(), searchParam.toString(), new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack audioTrack) {
            String trackName = audioTrack.getInfo().title;
            String trackAuthor = audioTrack.getInfo().author;
            String trackURL = audioTrack.getInfo().uri;
            boolean isStream = audioTrack.getInfo().isStream;
            long trackDuration = audioTrack.getDuration();
            if (!force) {
                getCurrentMusicManager().getScheduler().queue(audioTrack);
                embedBuilder.setAuthor("Added a new song to queue", trackURL, null);
                embedBuilder.addField("Title", trackName, true);
                embedBuilder.addField("Author", trackAuthor, true);
                embedBuilder.addField("Duration", (isStream) ? "Stream" : getTimestamp(trackDuration), false);
                embedBuilder.setColor(Colors.COLOR_PRIMARY);
                textChannel.sendMessage(embedBuilder.build()).queue();
            }
        }

        @Override
        public void playlistLoaded(AudioPlaylist audioPlaylist) {
            AudioTrack firstTrack = audioPlaylist.getSelectedTrack();
            List<AudioTrack> playlistTracks = audioPlaylist.getTracks();
            playlistTracks = playlistTracks.stream().limit(QUEUE_MAXIMUM - getCurrentMusicManager().getScheduler().getQueue().size()).collect(Collectors.toList());
            if (firstTrack == null)
                firstTrack = playlistTracks.get(0);
            if (force) {
                getCurrentMusicManager().getPlayer().playTrack(firstTrack);
                return;
            }
            if (isURLFinal) {
                playlistTracks.forEach(getCurrentMusicManager().getScheduler()::queue);
                embedBuilder.setTitle("Added playlist to queue");
                embedBuilder.setDescription("Added `" + playlistTracks.size() + "` songs from `" + audioPlaylist.getName() + "` to queue.\n" + "\n" + "**Now playing** `" + firstTrack.getInfo().title + "`");
                embedBuilder.addField("Author", firstTrack.getInfo().author, true);
                embedBuilder.addField("Duration", (firstTrack.getInfo().isStream) ? "Stream" : getTimestamp(firstTrack.getDuration()), false);
                textChannel.sendMessage(embedBuilder.build()).queue();
                embedBuilder.setColor(Colors.COLOR_PRIMARY);
                textChannel.sendMessage(embedBuilder.build()).queue();
            } else {
                MusicSearchResult musicSearchResult = new MusicSearchResult(parsedCommandInvocation.getAuthor(), guild, getCurrentMusicManager());
                audioPlaylist.getTracks().stream().limit(5).collect(Collectors.toList()).forEach(track -> {
                    try {
                        musicSearchResult.addTrack(track);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                musicSearchResult.setMessage(textChannel.sendMessage(musicSearchResult.generateEmbed().build()).complete());
                musicChoose.add(musicSearchResult);
                new Timer().schedule(new TimerTask() {

                    @Override
                    public void run() {
                        if (musicChoose.contains(musicSearchResult)) {
                            musicSearchResult.getMessage().delete().queue();
                            musicChoose.remove(musicSearchResult);
                        }
                    }
                }, 15000);
            }
        }

        @Override
        public void noMatches() {
            embedBuilder.setTitle("No matches!");
            embedBuilder.setDescription("There are no matches.");
            embedBuilder.setColor(Colors.COLOR_NOT_IMPLEMENTED);
            textChannel.sendMessage(embedBuilder.build()).queue();
        }

        @Override
        public void loadFailed(FriendlyException e) {
            embedBuilder.setTitle(":warning: Error!");
            embedBuilder.setDescription("Could not play this song: " + e.getMessage());
            embedBuilder.setColor(Colors.COLOR_ERROR);
            textChannel.sendMessage(embedBuilder.build()).queue();
        }
    });
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) EmbedUtil(fun.rubicon.util.EmbedUtil) java.util(java.util) CommandManager(fun.rubicon.command.CommandManager) Lyrics(org.jmusixmatch.entity.lyrics.Lyrics) GuildMusicSQL(fun.rubicon.sql.GuildMusicSQL) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) Permission(net.dv8tion.jda.core.Permission) HttpAudioSourceManager(com.sedmelluq.discord.lavaplayer.source.http.HttpAudioSourceManager) YoutubeAudioSourceManager(com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager) AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) MusixMatch(org.jmusixmatch.MusixMatch) RubiconBot(fun.rubicon.RubiconBot) DefaultAudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager) MusixMatchException(org.jmusixmatch.MusixMatchException) Track(org.jmusixmatch.entity.track.Track) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) fun.rubicon.util(fun.rubicon.util) Collectors(java.util.stream.Collectors) AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager) UserMusicSQL(fun.rubicon.sql.UserMusicSQL) MessageReceivedEvent(net.dv8tion.jda.core.events.message.MessageReceivedEvent) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) MusixMatchException(org.jmusixmatch.MusixMatchException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)

Example 7 with AudioPlaylist

use of com.sedmelluq.discord.lavaplayer.track.AudioPlaylist in project FredBoat by Frederikam.

the class PlayCommand method searchForVideos.

private void searchForVideos(CommandContext context) {
    // Now remove all punctuation
    String query = context.rawArgs.replaceAll(TrackSearcher.PUNCTUATION_REGEX, "");
    context.reply(context.i18n("playSearching").replace("{q}", query), outMsg -> {
        AudioPlaylist list;
        try {
            list = trackSearcher.searchForTracks(query, searchProviders);
        } catch (TrackSearcher.SearchingException e) {
            context.reply(context.i18n("playYoutubeSearchError"));
            log.error("YouTube search exception", e);
            return;
        }
        if (list == null || list.getTracks().isEmpty()) {
            CentralMessaging.editMessage(outMsg, context.i18n("playSearchNoResults").replace("{q}", query));
        } else {
            // Get at most 5 tracks
            List<AudioTrack> selectable = list.getTracks().subList(0, Math.min(TrackSearcher.MAX_RESULTS, list.getTracks().size()));
            VideoSelection oldSelection = videoSelectionCache.remove(context.invoker);
            if (oldSelection != null) {
                oldSelection.deleteMessage();
            }
            MessageBuilder builder = CentralMessaging.getClearThreadLocalMessageBuilder();
            builder.append(context.i18nFormat("playSelectVideo", TextUtils.escapeMarkdown(context.getPrefix())));
            int i = 1;
            for (AudioTrack track : selectable) {
                builder.append("\n**").append(String.valueOf(i)).append(":** ").append(TextUtils.escapeAndDefuse(track.getInfo().title)).append(" (").append(TextUtils.formatTime(track.getInfo().length)).append(")");
                i++;
            }
            CentralMessaging.editMessage(outMsg, builder.build());
            videoSelectionCache.put(context.invoker, selectable, outMsg);
        }
    });
}
Also used : VideoSelection(fredboat.audio.player.VideoSelectionCache.VideoSelection) TrackSearcher(fredboat.util.rest.TrackSearcher) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)

Example 8 with AudioPlaylist

use of com.sedmelluq.discord.lavaplayer.track.AudioPlaylist in project PorkBot by DaMatrix.

the class AudioUtils method loadAndPlay.

public static void loadAndPlay(final TextChannel channel, final String trackUrl, final Member user, final boolean notify) {
    GuildAudioInfo musicManager = getGuildAudioPlayer(channel.getGuild(), true, user, channel);
    playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack track) {
            if (notify)
                channel.sendMessage("Adding to queue: " + track.getInfo().title).queue();
            play(channel.getGuild(), musicManager, track, user, channel);
            synchronized (trackUrl) {
                trackUrl.notifyAll();
            }
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            List<AudioTrack> tracks = playlist.getTracks();
            if (tracks == null || tracks.size() == 0) {
                channel.sendMessage("Empty or invalid playlist, not loading");
            }
            if (notify)
                channel.sendMessage("Adding " + tracks.size() + " tracks from playlist " + playlist.getName() + " to queue").queue();
            playList(channel.getGuild(), musicManager, tracks, user, channel);
            synchronized (trackUrl) {
                trackUrl.notifyAll();
            }
        }

        @Override
        public void noMatches() {
            channel.sendMessage("Nothing found by " + trackUrl).queue();
        }

        @Override
        public void loadFailed(FriendlyException exception) {
            channel.sendMessage("Could not play: " + exception.getMessage()).queue();
        }
    });
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) 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)

Example 9 with AudioPlaylist

use of com.sedmelluq.discord.lavaplayer.track.AudioPlaylist in project BoltBot by DiscordBolt.

the class VoiceManager method getSongTitle.

public String getSongTitle(String songURL) {
    Semaphore s = new Semaphore(0);
    Info title = new Info();
    playerManager.loadItem(songURL, new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack track) {
            title.title = track.getInfo().title;
            s.release();
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            s.release();
        }

        @Override
        public void noMatches() {
            s.release();
        }

        @Override
        public void loadFailed(FriendlyException exception) {
            s.release();
        }
    });
    s.acquireUninterruptibly();
    return title.title;
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) Semaphore(java.util.concurrent.Semaphore) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 10 with AudioPlaylist

use of com.sedmelluq.discord.lavaplayer.track.AudioPlaylist in project c0debaseBot by Biospheere.

the class MusicManager method loadTrack.

public void loadTrack(TextChannel channel, User requester, String trackUrl) {
    Guild guild = channel.getGuild();
    loadTrack(guild, trackUrl, new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack track) {
            queue(guild, track);
            EmbedBuilder eb = getEmbed(guild, requester).setAuthor("Track geladen", trackUrl.startsWith("http") ? trackUrl : null, bot.getJda().getSelfUser().getEffectiveAvatarUrl());
            AudioTrackInfo trackInfo = track.getInfo();
            String length;
            if (TimeUnit.MILLISECONDS.toHours(trackInfo.length) >= 24) {
                length = String.format("%dd %02d:%02d:%02d", TimeUnit.MILLISECONDS.toDays(trackInfo.length), TimeUnit.MILLISECONDS.toHours(trackInfo.length) % 24, TimeUnit.MILLISECONDS.toMinutes(trackInfo.length) % 60, TimeUnit.MILLISECONDS.toSeconds(trackInfo.length) % 60);
            } else {
                length = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(trackInfo.length) % 24, TimeUnit.MILLISECONDS.toMinutes(trackInfo.length) % 60, TimeUnit.MILLISECONDS.toSeconds(trackInfo.length) % 60);
            }
            eb.addField(trackInfo.title, "`" + trackInfo.author + " - " + (trackInfo.isStream ? "STREAM" : length) + "`", false);
            channel.sendMessage(eb.build()).queue();
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            List<AudioTrack> tracks = playlist.getTracks();
            if (playlist.isSearchResult()) {
                trackLoaded(tracks.get(0));
                return;
            }
            EmbedBuilder eb = getEmbed(guild, requester).setAuthor("Playlist geladen", playlist.isSearchResult() ? null : trackUrl, bot.getJda().getSelfUser().getEffectiveAvatarUrl());
            eb.addField(playlist.getName(), "`" + tracks.size() + " Videos gefunden`", false);
            channel.sendMessage(eb.build()).queue();
            queue(guild, tracks);
            nextTrack(guild, false);
        }

        @Override
        public void noMatches() {
            channel.sendMessage(getEmbed(guild, requester).setAuthor("Nichts gefunden", null, bot.getJda().getSelfUser().getEffectiveAvatarUrl()).addField("Keine Übereinstimmung", "`" + trackUrl + "`", false).build()).queue();
        }

        @Override
        public void loadFailed(FriendlyException exception) {
            channel.sendMessage(getEmbed(guild, requester).setAuthor("Fehler aufgetreten", null, bot.getJda().getSelfUser().getEffectiveAvatarUrl()).addField("Fehlermeldung", "`" + exception.getMessage() + "`", false).build()).queue();
        }
    });
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) List(java.util.List) Guild(net.dv8tion.jda.core.entities.Guild) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Aggregations

AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)18 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)17 AudioLoadResultHandler (com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler)14 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)14 List (java.util.List)8 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)7 YoutubeAudioSourceManager (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager)5 AudioTrackInfo (com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)5 Optional (java.util.Optional)4 TimeUnit (java.util.concurrent.TimeUnit)4 Permission (net.dv8tion.jda.core.Permission)4 Color (java.awt.Color)3 ArrayList (java.util.ArrayList)3 Slf4j (lombok.extern.slf4j.Slf4j)3 MessageBuilder (net.dv8tion.jda.core.MessageBuilder)3 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)3 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)2 AudioPlayerManager (com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager)2 DefaultAudioPlayerManager (com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager)2 AudioSourceManager (com.sedmelluq.discord.lavaplayer.source.AudioSourceManager)2