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);
}
}
}
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);
}
}
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"));
}
}
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;
}
Aggregations