Search in sources :

Example 11 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project Ardent by adamint.

the class GetUrl method noArgs.

@Override
public void noArgs(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
    AudioManager audioManager = guild.getAudioManager();
    if (audioManager.isConnected()) {
        try {
            GuildMusicManager manager = getGuildAudioPlayer(guild, channel);
            BlockingQueue<ArdentTrack> queue = manager.scheduler.manager.getQueue();
            if (args.length > 1) {
                int numberToRemove = Integer.parseInt(args[1]) - 1;
                if (numberToRemove >= queue.size() || numberToRemove < 0)
                    sendTranslatedMessage("Invalid arguments", channel, user);
                else {
                    Iterator<ArdentTrack> iterator = queue.iterator();
                    int current = 0;
                    while (iterator.hasNext()) {
                        ArdentTrack ardentTrack = iterator.next();
                        AudioTrack track = ardentTrack.getTrack();
                        AudioTrackInfo info = track.getInfo();
                        String name = info.title;
                        if (current == numberToRemove) {
                            sendTranslatedMessage("The streaming link for {0} is {1}".replace("{0}", info.title).replace("{1}", info.uri), channel, user);
                            return;
                        }
                        current++;
                    }
                }
            } else {
                ArdentMusicManager musicManager = manager.scheduler.manager;
                ArdentTrack track = musicManager.getCurrentlyPlaying();
                if (track != null) {
                    AudioTrackInfo info = track.getTrack().getInfo();
                    sendTranslatedMessage("The streaming link for {0} is {1}".replace("{0}", info.title).replace("{1}", info.uri), channel, user);
                } else {
                    sendTranslatedMessage("I'm not playing anything right now!", channel, user);
                }
            }
        } catch (NumberFormatException ex) {
            sendTranslatedMessage("Invalid arguments", channel, user);
        }
    } else
        sendTranslatedMessage("I'm not in a voice channel!", channel, user);
}
Also used : AudioManager(net.dv8tion.jda.core.managers.AudioManager) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)

Example 12 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project Ardent by adamint.

the class Music method getDuration.

private static String getDuration(ArrayList<AudioTrack> tracks) {
    long length = 0;
    for (AudioTrack t : tracks) length += t.getDuration();
    int seconds = (int) (length / 1000);
    int minutes = seconds / 60;
    int hours = minutes / 60;
    return "[" + String.format("%02d", (hours % 60)) + ":" + String.format("%02d", (minutes % 60)) + ":" + String.format("%02d", (seconds % 60)) + "]";
}
Also used : AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack)

Example 13 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project Ardent by adamint.

the class Music method loadAndPlay.

static void loadAndPlay(Message message, User user, Command command, final TextChannel channel, String trackUrl, final VoiceChannel voiceChannel, boolean search, boolean useEmbedSelect) {
    if (trackUrl.contains("spotify.com")) {
        String[] parsed = trackUrl.split("/track/");
        if (parsed.length == 2) {
            final TrackRequest request = spotifyApi.getTrack(parsed[1]).build();
            try {
                trackUrl = request.get().getName();
            } catch (Exception e) {
                new BotException(e);
            }
        }
    }
    Guild guild = channel.getGuild();
    GuildMusicManager musicManager = getGuildAudioPlayer(channel.getGuild(), channel);
    String finalTrackUrl = trackUrl;
    GuildUtils.getShard(guild).playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack track) {
            if (!UserUtils.hasTierTwoPermissions(user) && !EntityGuild.get(guild).isPremium()) {
                try {
                    if (!shouldContinue(user, guild, channel, track)) {
                        return;
                    }
                } catch (Exception e) {
                    new BotException(e);
                }
            }
            try {
                command.sendTranslatedMessage("Adding {0} to the queue".replace("{0}", track.getInfo().title) + " " + getDuration(track), channel, user);
            } catch (Exception e) {
                new BotException(e);
            }
            play(user, guild, voiceChannel, musicManager, track, channel);
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            List<AudioTrack> tracks = playlist.getTracks();
            if (playlist.isSearchResult()) {
                try {
                    if (!useEmbedSelect) {
                        AudioTrack[] possible;
                        if (playlist.getTracks().size() >= 5)
                            possible = playlist.getTracks().subList(0, 5).toArray(new AudioTrack[5]);
                        else
                            possible = playlist.getTracks().toArray(new AudioTrack[playlist.getTracks().size()]);
                        ArrayList<String> names = new ArrayList<>();
                        for (AudioTrack audioTrack : possible) {
                            names.add(audioTrack.getInfo().title);
                        }
                        Message embed = command.sendEmbed(command.chooseFromList("Choose Song", guild, user, command, names.toArray(new String[5])), channel, user);
                        interactiveOperation(channel, message, selectionMessage -> {
                            try {
                                AudioTrack selected = possible[Integer.parseInt(selectionMessage.getContent()) - 1];
                                if (!UserUtils.hasTierTwoPermissions(user) && !EntityGuild.get(guild).isPremium()) {
                                    try {
                                        if (!shouldContinue(user, guild, channel, selected)) {
                                            return;
                                        }
                                    } catch (Exception e) {
                                        new BotException(e);
                                    }
                                }
                                try {
                                    embed.delete().queue();
                                    selectionMessage.delete().queue();
                                } catch (Exception ignored) {
                                }
                                play(user, guild, voiceChannel, musicManager, selected, channel);
                                command.sendTranslatedMessage("Adding {0} to the queue".replace("{0}", selected.getInfo().title) + " " + getDuration(selected), channel, user);
                            } catch (Exception e) {
                                command.sendTranslatedMessage("Invalid response", channel, user);
                            }
                        });
                    } else {
                        AudioTrack track = playlist.getTracks().get(0);
                        play(user, guild, voiceChannel, musicManager, track, channel);
                        command.sendTranslatedMessage("Adding {0} to the queue".replace("{0}", track.getInfo().title) + " " + getDuration(track), channel, user);
                    }
                } catch (Exception e) {
                    new BotException(e);
                }
            } else {
                if (!UserUtils.hasTierTwoPermissions(user) && !EntityGuild.get(guild).isPremium()) {
                    try {
                        if (!shouldContinue(user, guild, channel, 1)) {
                            return;
                        }
                    } catch (Exception e) {
                        new BotException(e);
                    }
                }
                try {
                    command.sendTranslatedMessage("Adding {0} songs to the queue".replace("{0}", String.valueOf(tracks.size())), channel, user);
                } catch (Exception e) {
                    new BotException(e);
                }
                for (AudioTrack track : tracks) {
                    play(user, guild, voiceChannel, musicManager, track, channel);
                }
            }
        }

        @Override
        public void noMatches() {
            if (!search) {
                loadAndPlay(message, user, command, channel, "ytsearch: " + finalTrackUrl, voiceChannel, true, useEmbedSelect);
            } else {
                try {
                    command.sendTranslatedMessage("I couldn't find a song with that name", channel, user);
                } catch (Exception e) {
                    new BotException(e);
                }
            }
        }

        @Override
        public void loadFailed(FriendlyException exception) {
            try {
                sendTo(channel, guild).sendMessage("I wasn't able to play that track... **Reason: **" + exception.getLocalizedMessage()).queue();
            } catch (Exception e) {
                new BotException(e);
            }
        }
    });
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) TrackRequest(com.wrapper.spotify.methods.TrackRequest) MessageUtils(tk.ardentbot.utils.discord.MessageUtils) MusicSettingsModel(tk.ardentbot.rethink.models.MusicSettingsModel) Database.r(tk.ardentbot.rethink.Database.r) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) ArrayList(java.util.ArrayList) EntityGuild(tk.ardentbot.utils.rpg.EntityGuild) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) GuildUtils(tk.ardentbot.utils.discord.GuildUtils) SQLException(java.sql.SQLException) Permission(net.dv8tion.jda.core.Permission) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) Region(net.dv8tion.jda.core.Region) ShardManager(tk.ardentbot.main.ShardManager) Command(tk.ardentbot.core.executor.Command) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) UserUtils(tk.ardentbot.utils.discord.UserUtils) Subcommand(tk.ardentbot.core.executor.Subcommand) AudioManager(net.dv8tion.jda.core.managers.AudioManager) BaseCommand(tk.ardentbot.core.executor.BaseCommand) BotException(tk.ardentbot.core.misc.logging.BotException) Shard(tk.ardentbot.main.Shard) AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Ardent.spotifyApi(tk.ardentbot.main.Ardent.spotifyApi) Database.connection(tk.ardentbot.rethink.Database.connection) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) Pair(tk.ardentbot.utils.javaAdditions.Pair) WordUtils(org.apache.commons.lang.WordUtils) ArrayList(java.util.ArrayList) EntityGuild(tk.ardentbot.utils.rpg.EntityGuild) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) SQLException(java.sql.SQLException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) BotException(tk.ardentbot.core.misc.logging.BotException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) TrackRequest(com.wrapper.spotify.methods.TrackRequest) BotException(tk.ardentbot.core.misc.logging.BotException) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) ArrayList(java.util.ArrayList) List(java.util.List) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)

Aggregations

AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)13 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)6 AudioTrackInfo (com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)5 AudioLoadResultHandler (com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler)4 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)4 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)4 TimeUnit (java.util.concurrent.TimeUnit)4 Permission (net.dv8tion.jda.core.Permission)4 AudioManager (net.dv8tion.jda.core.managers.AudioManager)4 YoutubeAudioSourceManager (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager)3 Color (java.awt.Color)3 Optional (java.util.Optional)3 IntConsumer (java.util.function.IntConsumer)3 Slf4j (lombok.extern.slf4j.Slf4j)3 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)3 ReactionOperations (net.kodehawa.mantarobot.core.listeners.operations.ReactionOperations)3 MantaroData (net.kodehawa.mantarobot.data.MantaroData)3 DBGuild (net.kodehawa.mantarobot.data.entities.DBGuild)3 GuildData (net.kodehawa.mantarobot.data.entities.helpers.GuildData)3 DiscordUtils (net.kodehawa.mantarobot.utils.DiscordUtils)3