use of net.robinfriedli.aiode.audio.AudioPlayback in project aiode by robinfriedli.
the class ShuffleCommand method onSuccess.
@Override
public void onSuccess() {
AudioPlayback playback = Aiode.get().getAudioManager().getPlaybackForGuild(getContext().getGuild());
StringBuilder messageBuilder = new StringBuilder();
if (playback.isShuffle()) {
messageBuilder.append("Enabled ");
} else {
messageBuilder.append("Disabled ");
}
messageBuilder.append("shuffle");
if (!playback.getAudioQueue().isEmpty()) {
if (playback.isShuffle()) {
messageBuilder.append(" and shuffled queue order.");
} else {
messageBuilder.append(" and returned queue back to normal order.");
}
}
AudioQueue queue = playback.getAudioQueue();
if (queue.hasNext()) {
messageBuilder.append(" New next track: ").append(queue.getNext().display());
}
sendSuccess(messageBuilder.toString());
}
use of net.robinfriedli.aiode.audio.AudioPlayback in project aiode by robinfriedli.
the class StopCommand method doRun.
@Override
public void doRun() {
Guild guild = getContext().getGuild();
AudioManager audioManager = Aiode.get().getAudioManager();
AudioPlayback playback = audioManager.getPlaybackForGuild(guild);
getContext().getGuildContext().getReplaceableTrackLoadingExecutor().abort();
playback.stop();
playback.getAudioQueue().clear();
}
use of net.robinfriedli.aiode.audio.AudioPlayback in project aiode by robinfriedli.
the class AbstractPlayableLoadingCommand method doRun.
@Override
public void doRun() throws Exception {
AudioManager audioManager = Aiode.get().getAudioManager();
AudioPlayback playback = audioManager.getPlaybackForGuild(getContext().getGuild());
if (UrlValidator.getInstance().isValid(getCommandInput())) {
loadUrlItems(audioManager, playback);
} else if (SpotifyUri.isSpotifyUri(getCommandInput())) {
loadSpotifyUri(audioManager);
} else if (argumentSet("list")) {
Source source = getSource();
if (source.isSpotify()) {
loadSpotifyList(audioManager);
} else if (source.isYouTube()) {
loadYouTubeList(audioManager);
} else {
loadLocalList(audioManager);
}
} else if (argumentSet("episode")) {
loadSpotifyEpisode(audioManager);
} else if (argumentSet("podcast")) {
loadSpotifyShow(audioManager);
} else {
Source source = getSource();
if (source.isYouTube()) {
loadYouTubeVideo(audioManager);
} else if (source.isSoundCloud()) {
loadSoundCloudTrack(audioManager);
} else if (argumentSet("album")) {
loadSpotifyAlbum(audioManager);
} else {
loadTrack(audioManager);
}
}
}
use of net.robinfriedli.aiode.audio.AudioPlayback in project aiode by robinfriedli.
the class PlaybackCleanupTask method run.
@Override
public void run(JobExecutionContext jobExecutionContext) {
Logger logger = LoggerFactory.getLogger(getClass());
Aiode aiode = Aiode.get();
GuildManager guildManager = aiode.getGuildManager();
Set<GuildContext> guildContexts = guildManager.getGuildContexts();
Set<Guild> activeGuilds = StaticSessionProvider.invokeWithSession(session -> guildManager.getActiveGuilds(session, 3600000));
int clearedAlone = 0;
int playbacksCleared = 0;
for (GuildContext guildContext : guildContexts) {
try {
AudioPlayback playback = guildContext.getPlayback();
LocalDateTime aloneSince = playback.getAloneSince();
if (aloneSince != null) {
Duration aloneSinceDuration = Duration.between(aloneSince, LocalDateTime.now());
Duration oneHour = Duration.ofHours(1);
if (aloneSinceDuration.compareTo(oneHour) > 0) {
if (clearLonePlayback(guildContext, playback)) {
++clearedAlone;
continue;
}
}
}
if (!activeGuilds.contains(playback.getGuild()) && !playback.isPlaying()) {
if (playback.clear()) {
++playbacksCleared;
}
}
} catch (DiscordEntityInitialisationException e) {
// guild could not be loaded anymore, skip
continue;
}
}
if (clearedAlone > 0 || playbacksCleared > 0) {
logger.info(String.format("Cleared %d stale playbacks and stopped %d lone playbacks", playbacksCleared, clearedAlone));
}
}
use of net.robinfriedli.aiode.audio.AudioPlayback in project aiode by robinfriedli.
the class QueueViewHandler method handle.
@Override
public void handle(HttpExchange exchange) throws IOException {
try {
String html = Files.readString(Path.of("html/queue_view.html"));
Map<String, String> parameterMap = ServerUtil.getParameters(exchange);
String guildId = parameterMap.get("guildId");
if (guildId != null) {
Guild guild = shardManager.getGuildById(guildId);
if (guild != null) {
AudioPlayback playback = audioManager.getPlaybackForGuild(guild);
AudioQueue queue = playback.getAudioQueue();
String content;
if (!queue.isEmpty()) {
int position = queue.getPosition();
List<Playable> previous = queue.listPrevious(position);
List<Playable> next = queue.listNext(queue.getTracks().size() - position);
StringBuilder listBuilder = new StringBuilder();
if (!previous.isEmpty()) {
if (previous.size() > 20) {
listBuilder.append("<a href=\"#current\">Jump to current track</a>").append(System.lineSeparator());
}
appendList(listBuilder, previous, "Previous");
}
listBuilder.append("<div id=\"current\">").append(System.lineSeparator());
appendList(listBuilder, Collections.singletonList(queue.getCurrent()), "Current");
listBuilder.append("</div>").append(System.lineSeparator());
if (!next.isEmpty()) {
appendList(listBuilder, next, "Next");
}
content = listBuilder.toString();
} else {
content = "Queue is empty";
}
String response = String.format(html, boolToString(playback.isPaused()), boolToString(playback.isShuffle()), boolToString(playback.isRepeatAll()), boolToString(playback.isRepeatOne()), content);
byte[] bytes = response.getBytes();
exchange.sendResponseHeaders(200, bytes.length);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(bytes);
outputStream.close();
} else {
throw new InvalidRequestException("Guild " + guildId + " not found");
}
} else {
throw new InvalidRequestException("No guild provided");
}
} catch (InvalidRequestException e) {
ServerUtil.handleError(exchange, e);
} catch (Exception e) {
ServerUtil.handleError(exchange, e);
LoggerFactory.getLogger(getClass()).error("Error in HttpHandler", e);
}
}
Aggregations