Search in sources :

Example 21 with AudioPlayback

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());
}
Also used : AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) AudioQueue(net.robinfriedli.aiode.audio.AudioQueue)

Example 22 with AudioPlayback

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();
}
Also used : AudioManager(net.robinfriedli.aiode.audio.AudioManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) Guild(net.dv8tion.jda.api.entities.Guild)

Example 23 with AudioPlayback

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);
        }
    }
}
Also used : AudioManager(net.robinfriedli.aiode.audio.AudioManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback)

Example 24 with AudioPlayback

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));
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) GuildContext(net.robinfriedli.aiode.discord.GuildContext) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) GuildManager(net.robinfriedli.aiode.discord.GuildManager) Duration(java.time.Duration) Logger(org.slf4j.Logger) Guild(net.dv8tion.jda.api.entities.Guild) Aiode(net.robinfriedli.aiode.Aiode) DiscordEntityInitialisationException(net.robinfriedli.aiode.exceptions.DiscordEntityInitialisationException)

Example 25 with AudioPlayback

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);
    }
}
Also used : AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) OutputStream(java.io.OutputStream) Guild(net.dv8tion.jda.api.entities.Guild) InvalidRequestException(net.robinfriedli.aiode.exceptions.InvalidRequestException) IOException(java.io.IOException) Playable(net.robinfriedli.aiode.audio.Playable) InvalidRequestException(net.robinfriedli.aiode.exceptions.InvalidRequestException) AudioQueue(net.robinfriedli.aiode.audio.AudioQueue)

Aggregations

AudioPlayback (net.robinfriedli.aiode.audio.AudioPlayback)25 Guild (net.dv8tion.jda.api.entities.Guild)13 AudioManager (net.robinfriedli.aiode.audio.AudioManager)9 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)7 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)6 VoiceChannel (net.dv8tion.jda.api.entities.VoiceChannel)5 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)4 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)2 IOException (java.io.IOException)2 LocalDateTime (java.time.LocalDateTime)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 Aiode (net.robinfriedli.aiode.Aiode)2 Playable (net.robinfriedli.aiode.audio.Playable)2 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)2 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)2 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)2 Strings (com.google.common.base.Strings)1 Sets (com.google.common.collect.Sets)1 OutputStream (java.io.OutputStream)1 Duration (java.time.Duration)1