use of fredboat.audio.player.GuildPlayer in project FredBoat by Frederikam.
the class SelectCommand method select.
static void select(CommandContext context, VideoSelectionCache videoSelectionCache) {
Member invoker = context.invoker;
VideoSelection selection = videoSelectionCache.get(invoker);
if (selection == null) {
context.reply(context.i18n("selectSelectionNotGiven"));
return;
}
try {
// Step 1: Parse the issued command for numbers
// LinkedHashSet to handle order of choices + duplicates
LinkedHashSet<Integer> requestChoices = new LinkedHashSet<>();
// Combine all args and the command trigger if it is numeric
String commandOptions = context.rawArgs;
if (StringUtils.isNumeric(context.trigger)) {
commandOptions = (context.trigger + " " + commandOptions).trim();
}
if (StringUtils.isNumeric(commandOptions)) {
requestChoices.add(Integer.valueOf(commandOptions));
} else if (TextUtils.isSplitSelect(commandOptions)) {
requestChoices.addAll(TextUtils.getSplitSelect(commandOptions));
}
// Step 2: Use only valid numbers (usually 1-5)
ArrayList<Integer> validChoices = new ArrayList<>();
// Only include valid values which are 1 to <size> of the offered selection
for (Integer value : requestChoices) {
if (1 <= value && value <= selection.choices.size()) {
validChoices.add(value);
Metrics.selectionChoiceChosen.labels(value.toString()).inc();
}
}
// any valid choices at all?
if (validChoices.isEmpty()) {
throw new NumberFormatException();
} else {
if (validChoices.size() > 1) {
Metrics.multiSelections.labels(Integer.toString(validChoices.size())).inc();
}
AudioTrack[] selectedTracks = new AudioTrack[validChoices.size()];
StringBuilder outputMsgBuilder = new StringBuilder();
GuildPlayer player = Launcher.getBotController().getPlayerRegistry().getOrCreate(context.guild);
for (int i = 0; i < validChoices.size(); i++) {
selectedTracks[i] = selection.choices.get(validChoices.get(i) - 1);
String msg = context.i18nFormat("selectSuccess", validChoices.get(i), TextUtils.escapeAndDefuse(selectedTracks[i].getInfo().title), TextUtils.formatTime(selectedTracks[i].getInfo().length));
if (i < validChoices.size()) {
outputMsgBuilder.append("\n");
}
outputMsgBuilder.append(msg);
player.queue(new AudioTrackContext(Launcher.getBotController().getJdaEntityProvider(), selectedTracks[i], invoker));
}
videoSelectionCache.remove(invoker);
TextChannel tc = Launcher.getBotController().getJdaEntityProvider().getTextChannelById(selection.channelId);
if (tc != null) {
CentralMessaging.editMessage(tc, selection.outMsgId, CentralMessaging.from(outputMsgBuilder.toString()));
}
player.setPause(false);
context.deleteMessage();
}
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
context.reply(context.i18nFormat("selectInterval", selection.choices.size()));
}
}
use of fredboat.audio.player.GuildPlayer in project FredBoat by Frederikam.
the class ShuffleCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
GuildPlayer player = Launcher.getBotController().getPlayerRegistry().getOrCreate(context.guild);
player.setShuffle(!player.isShuffle());
if (player.isShuffle()) {
context.reply(context.i18n("shuffleOn"));
} else {
context.reply(context.i18n("shuffleOff"));
}
}
use of fredboat.audio.player.GuildPlayer in project FredBoat by Frederikam.
the class StopCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
GuildPlayer player = Launcher.getBotController().getPlayerRegistry().getExisting(context.guild);
int tracksCount = 0;
if (player != null) {
tracksCount = player.getTrackCount();
player.pause();
player.stop();
player.leaveVoiceChannelRequest(context, true);
}
switch(tracksCount) {
case 0:
context.reply(context.i18n("stopAlreadyEmpty"));
break;
case 1:
context.reply(context.i18n("stopEmptyOne"));
break;
default:
context.reply(context.i18nFormat("stopEmptySeveral", tracksCount));
break;
}
}
use of fredboat.audio.player.GuildPlayer in project FredBoat by Frederikam.
the class UnpauseCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
Guild guild = context.guild;
GuildPlayer player = Launcher.getBotController().getPlayerRegistry().getExisting(guild);
if (player == null || player.isQueueEmpty()) {
context.reply(context.i18n("unpauseQueueEmpty"));
} else if (!player.isPaused()) {
context.reply(context.i18n("unpausePlayerNotPaused"));
} else if (player.getHumanUsersInCurrentVC().isEmpty() && player.isPaused() && guild.getSelfMember().getVoiceState().getChannel() != null) {
context.reply(context.i18n("unpauseNoUsers"));
} else if (guild.getSelfMember().getVoiceState().getChannel() == null) {
// When we just want to continue playing, but the user is not in a VC
JOIN_COMMAND.onInvoke(context);
if (guild.getSelfMember().getVoiceState().getChannel() != null) {
player.play();
context.reply(context.i18n("unpauseSuccess"));
}
} else {
player.play();
context.reply(context.i18n("unpauseSuccess"));
}
}
use of fredboat.audio.player.GuildPlayer in project FredBoat by Frederikam.
the class SeekCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
GuildPlayer player = Launcher.getBotController().getPlayerRegistry().getExisting(context.guild);
if (player == null || player.isQueueEmpty()) {
context.replyWithName(context.i18n("queueEmpty"));
return;
}
if (!context.hasArguments()) {
HelpCommand.sendFormattedCommandHelp(context);
return;
}
long t;
try {
t = TextUtils.parseTimeString(context.args[0]);
} catch (IllegalStateException e) {
HelpCommand.sendFormattedCommandHelp(context);
return;
}
AudioTrackContext atc = player.getPlayingTrack();
// Ensure bounds
t = Math.max(0, t);
t = Math.min(atc.getEffectiveDuration(), t);
player.seekTo(atc.getStartPosition() + t);
context.reply(context.i18nFormat("seekSuccess", TextUtils.escapeAndDefuse(atc.getEffectiveTitle()), TextUtils.formatTime(t)));
}
Aggregations