Search in sources :

Example 11 with AudioTrackContext

use of fredboat.audio.queue.AudioTrackContext in project FredBoat by Frederikam.

the class MusicPersistenceHandler method persist.

private void persist(int code) {
    File dir = new File("music_persistence");
    if (!dir.exists()) {
        boolean created = dir.mkdir();
        if (!created) {
            log.error("Failed to create music persistence directory");
            return;
        }
    }
    Map<Long, GuildPlayer> reg = playerRegistry.getRegistry();
    boolean isUpdate = code == ExitCodes.EXIT_CODE_UPDATE;
    boolean isRestart = code == ExitCodes.EXIT_CODE_RESTART;
    for (long gId : reg.keySet()) {
        try {
            GuildPlayer player = reg.get(gId);
            String msg;
            if (isUpdate) {
                msg = I18n.get(player.getGuild()).getString("shutdownUpdating");
            } else if (isRestart) {
                msg = I18n.get(player.getGuild()).getString("shutdownRestarting");
            } else {
                msg = I18n.get(player.getGuild()).getString("shutdownIndef");
            }
            TextChannel activeTextChannel = player.getActiveTextChannel();
            List<CompletableFuture> announcements = new ArrayList<>();
            if (activeTextChannel != null && player.isPlaying()) {
                announcements.add(CentralMessaging.message(activeTextChannel, msg).send(null));
            }
            for (Future announcement : announcements) {
                try {
                    // 30 seconds is enough on patron boat
                    announcement.get(30, TimeUnit.SECONDS);
                } catch (Exception ignored) {
                }
            }
            JSONObject data = new JSONObject();
            VoiceChannel vc = player.getCurrentVoiceChannel();
            data.put("vc", vc != null ? vc.getId() : "0");
            data.put("tc", activeTextChannel != null ? activeTextChannel.getId() : "");
            data.put("isPaused", player.isPaused());
            data.put("volume", Float.toString(player.getVolume()));
            data.put("repeatMode", player.getRepeatMode());
            data.put("shuffle", player.isShuffle());
            if (player.getPlayingTrack() != null) {
                data.put("position", player.getPosition());
            }
            ArrayList<JSONObject> identifiers = new ArrayList<>();
            for (AudioTrackContext atc : player.getRemainingTracks()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                audioPlayerManager.encodeTrack(new MessageOutput(baos), atc.getTrack());
                JSONObject ident = new JSONObject().put("message", Base64.encodeBase64String(baos.toByteArray())).put("user", atc.getUserId());
                if (atc instanceof SplitAudioTrackContext) {
                    JSONObject split = new JSONObject();
                    SplitAudioTrackContext c = (SplitAudioTrackContext) atc;
                    split.put("title", c.getEffectiveTitle()).put("startPos", c.getStartPosition()).put("endPos", c.getStartPosition() + c.getEffectiveDuration());
                    ident.put("split", split);
                }
                identifiers.add(ident);
            }
            data.put("sources", identifiers);
            try {
                FileUtils.writeStringToFile(new File(dir, Long.toString(gId)), data.toString(), Charset.forName("UTF-8"));
            } catch (IOException ex) {
                if (activeTextChannel != null) {
                    CentralMessaging.message(activeTextChannel, MessageFormat.format(I18n.get(player.getGuild()).getString("shutdownPersistenceFail"), ex.getMessage())).send(null);
                }
            }
        } catch (Exception ex) {
            log.error("Error when saving persistence file", ex);
        }
    }
}
Also used : MessageOutput(com.sedmelluq.discord.lavaplayer.tools.io.MessageOutput) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SplitAudioTrackContext(fredboat.audio.queue.SplitAudioTrackContext) IOException(java.io.IOException) TextChannel(net.dv8tion.jda.core.entities.TextChannel) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) GuildPlayer(fredboat.audio.player.GuildPlayer) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel) AudioTrackContext(fredboat.audio.queue.AudioTrackContext) SplitAudioTrackContext(fredboat.audio.queue.SplitAudioTrackContext) File(java.io.File)

Example 12 with AudioTrackContext

use of fredboat.audio.queue.AudioTrackContext in project FredBoat by Frederikam.

the class VoteSkipCommand method onInvoke.

@Override
public void onInvoke(@Nonnull CommandContext context) {
    // While you can join another vc and then voteskip i don't think this will be common
    if (!context.invoker.getVoiceState().inVoiceChannel()) {
        context.reply(context.i18n("playerUserNotInChannel"));
        return;
    }
    GuildPlayer player = Launcher.getBotController().getPlayerRegistry().getExisting(context.guild);
    if (player == null || player.isQueueEmpty()) {
        context.reply(context.i18n("skipEmpty"));
        return;
    }
    if (isOnCooldown(context.guild)) {
        return;
    } else {
        guildIdToLastSkip.put(context.guild.getId(), System.currentTimeMillis());
    }
    if (!context.hasArguments()) {
        String response = addVoteWithResponse(context);
        float actualMinSkip = player.getHumanUsersInCurrentVC().size() < 3 ? 1.0f : MIN_SKIP_PERCENTAGE;
        float skipPercentage = getSkipPercentage(context.guild, player);
        if (skipPercentage >= actualMinSkip) {
            AudioTrackContext atc = player.getPlayingTrack();
            if (atc == null) {
                context.reply(context.i18n("skipTrackNotFound"));
            } else {
                String skipPerc = "`" + TextUtils.formatPercent(skipPercentage) + "`";
                String trackTitle = "**" + TextUtils.escapeAndDefuse(atc.getEffectiveTitle()) + "**";
                context.reply(response + "\n" + context.i18nFormat("voteSkipSkipping", skipPerc, trackTitle));
                player.skip();
            }
        } else {
            String skipPerc = "`" + TextUtils.formatPercent(skipPercentage) + "`";
            String minSkipPerc = "`" + TextUtils.formatPercent(actualMinSkip) + "`";
            context.reply(response + "\n" + context.i18nFormat("voteSkipNotEnough", skipPerc, minSkipPerc));
        }
    } else if (context.args[0].toLowerCase().equals("list")) {
        displayVoteList(context, player);
    } else {
        HelpCommand.sendFormattedCommandHelp(context);
    }
}
Also used : GuildPlayer(fredboat.audio.player.GuildPlayer) AudioTrackContext(fredboat.audio.queue.AudioTrackContext)

Example 13 with AudioTrackContext

use of fredboat.audio.queue.AudioTrackContext in project FredBoat by Frederikam.

the class SkipCommand method skipUser.

private void skipUser(GuildPlayer player, CommandContext context, List<User> users) {
    if (!PermsUtil.checkPerms(PermissionLevel.DJ, context.invoker)) {
        if (users.size() == 1) {
            User user = users.get(0);
            if (context.invoker.getUser().getIdLong() != user.getIdLong()) {
                context.reply(context.i18n("skipDeniedTooManyTracks"));
                return;
            }
        } else {
            context.reply(context.i18n("skipDeniedTooManyTracks"));
            return;
        }
    }
    List<AudioTrackContext> listAtc = player.getTracksInRange(0, player.getTrackCount());
    List<Long> userAtcIds = new ArrayList<>();
    List<User> affectedUsers = new ArrayList<>();
    for (User user : users) {
        for (AudioTrackContext atc : listAtc) {
            if (atc.getUserId() == user.getIdLong()) {
                userAtcIds.add(atc.getTrackId());
                if (!affectedUsers.contains(user)) {
                    affectedUsers.add(user);
                }
            }
        }
    }
    if (userAtcIds.size() > 0) {
        String title = TextUtils.escapeAndDefuse(player.getPlayingTrack().getEffectiveTitle());
        player.skipTracks(userAtcIds);
        if (affectedUsers.size() > 1) {
            context.reply(context.i18nFormat("skipUsersMultiple", ("`" + userAtcIds.size() + "`"), ("**" + affectedUsers.size() + "**")));
        } else {
            User user = affectedUsers.get(0);
            String userName = "**" + TextUtils.escapeAndDefuse(user.getName()) + "#" + user.getDiscriminator() + "**";
            if (userAtcIds.size() == 1) {
                context.reply(context.i18nFormat("skipUserSingle", "**" + title + "**", userName));
            } else {
                context.reply(context.i18nFormat("skipUserMultiple", "`" + userAtcIds.size() + "`", userName));
            }
        }
    } else {
        context.reply(context.i18n("skipUserNoTracks"));
    }
}
Also used : User(net.dv8tion.jda.core.entities.User) AudioTrackContext(fredboat.audio.queue.AudioTrackContext)

Example 14 with AudioTrackContext

use of fredboat.audio.queue.AudioTrackContext in project FredBoat by Frederikam.

the class AbstractPlayer method getRemainingTracks.

// the unshuffled playlist
public List<AudioTrackContext> getRemainingTracks() {
    log.trace("getRemainingTracks()");
    // Includes currently playing track, which comes first
    List<AudioTrackContext> list = new ArrayList<>();
    AudioTrackContext atc = getPlayingTrack();
    if (atc != null) {
        list.add(atc);
    }
    list.addAll(audioTrackProvider.getAsList());
    return list;
}
Also used : ArrayList(java.util.ArrayList) SplitAudioTrackContext(fredboat.audio.queue.SplitAudioTrackContext) AudioTrackContext(fredboat.audio.queue.AudioTrackContext)

Aggregations

AudioTrackContext (fredboat.audio.queue.AudioTrackContext)14 GuildPlayer (fredboat.audio.player.GuildPlayer)9 Member (net.dv8tion.jda.core.entities.Member)5 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)4 SplitAudioTrackContext (fredboat.audio.queue.SplitAudioTrackContext)4 ArrayList (java.util.ArrayList)4 TextChannel (net.dv8tion.jda.core.entities.TextChannel)3 File (java.io.File)2 IOException (java.io.IOException)2 MessageBuilder (net.dv8tion.jda.core.MessageBuilder)2 VoiceChannel (net.dv8tion.jda.core.entities.VoiceChannel)2 JSONObject (org.json.JSONObject)2 BandcampAudioTrack (com.sedmelluq.discord.lavaplayer.source.bandcamp.BandcampAudioTrack)1 BeamAudioTrack (com.sedmelluq.discord.lavaplayer.source.beam.BeamAudioTrack)1 HttpAudioTrack (com.sedmelluq.discord.lavaplayer.source.http.HttpAudioTrack)1 SoundCloudAudioTrack (com.sedmelluq.discord.lavaplayer.source.soundcloud.SoundCloudAudioTrack)1 TwitchStreamAudioTrack (com.sedmelluq.discord.lavaplayer.source.twitch.TwitchStreamAudioTrack)1 YoutubeAudioTrack (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioTrack)1 MessageInput (com.sedmelluq.discord.lavaplayer.tools.io.MessageInput)1 MessageOutput (com.sedmelluq.discord.lavaplayer.tools.io.MessageOutput)1