use of fredboat.audio.player.GuildPlayer 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.player.GuildPlayer in project FredBoat by Frederikam.
the class ShardReviveHandler method onShutdown.
@Override
public void onShutdown(ShutdownEvent event) {
try {
List<Long> channels = new ArrayList<>();
int shardId = event.getJDA().getShardInfo().getShardId();
playerRegistry.getPlayingPlayers().stream().filter(guildPlayer -> DiscordUtil.getShardId(guildPlayer.getGuildId(), credentials) == shardId).forEach(guildPlayer -> {
VoiceChannel channel = guildPlayer.getCurrentVoiceChannel();
if (channel != null)
channels.add(channel.getIdLong());
});
channelsToRejoin.put(shardId, channels);
} catch (Exception ex) {
log.error("Caught exception while saving channels to revive shard {}", event.getJDA().getShardInfo(), ex);
}
}
use of fredboat.audio.player.GuildPlayer in project FredBoat by Frederikam.
the class ShardReviveHandler method onReady.
@Override
public void onReady(ReadyEvent event) {
// Rejoin old channels if revived
List<Long> channels = channelsToRejoin.computeIfAbsent(event.getJDA().getShardInfo().getShardId(), ArrayList::new);
List<Long> toRejoin = new ArrayList<>(channels);
// avoid situations where this method is called twice with the same channels
channels.clear();
toRejoin.forEach(vcid -> {
VoiceChannel channel = event.getJDA().getVoiceChannelById(vcid);
if (channel == null)
return;
GuildPlayer player = playerRegistry.getOrCreate(channel.getGuild());
audioConnectionFacade.openConnection(channel, player);
});
}
use of fredboat.audio.player.GuildPlayer in project FredBoat by Frederikam.
the class VolumeCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
if (Launcher.getBotController().getAppConfig().getDistribution().volumeSupported()) {
GuildPlayer player = Launcher.getBotController().getPlayerRegistry().getOrCreate(context.guild);
try {
float volume = Float.parseFloat(context.args[0]) / 100;
volume = Math.max(0, Math.min(1.5f, volume));
context.reply(context.i18nFormat("volumeSuccess", Math.floor(player.getVolume() * 100), Math.floor(volume * 100)));
player.setVolume(volume);
} catch (NumberFormatException | ArrayIndexOutOfBoundsException ex) {
throw new MessagingException(context.i18nFormat("volumeSyntax", 100 * PlayerRegistry.DEFAULT_VOLUME, Math.floor(player.getVolume() * 100)));
}
} else {
String out = context.i18n("volumeApology") + "\n<" + BotConstants.DOCS_DONATE_URL + ">";
context.replyImage("https://fred.moe/1vD.png", out, msg -> CentralMessaging.restService.schedule(() -> CentralMessaging.deleteMessage(msg), 2, TimeUnit.MINUTES));
}
}
use of fredboat.audio.player.GuildPlayer 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);
}
}
Aggregations