Search in sources :

Example 1 with NoResultsFoundException

use of net.robinfriedli.aiode.exceptions.NoResultsFoundException in project aiode by robinfriedli.

the class SearchCommand method listYouTubePlaylists.

private void listYouTubePlaylists() throws IOException {
    YouTubeService youTubeService = Aiode.get().getAudioManager().getYouTubeService();
    if (argumentSet("select")) {
        int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 10);
        List<YouTubePlaylist> playlists = youTubeService.searchSeveralPlaylists(limit, getCommandInput());
        if (playlists.size() == 1) {
            listYouTubePlaylist(playlists.get(0));
        } else if (playlists.isEmpty()) {
            throw new NoResultsFoundException(String.format("No YouTube playlist found for '%s'", getCommandInput()));
        } else {
            askQuestion(playlists, YouTubePlaylist::getTitle, YouTubePlaylist::getChannelTitle);
        }
    } else {
        listYouTubePlaylist(youTubeService.searchPlaylist(getCommandInput()));
    }
}
Also used : NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) YouTubeService(net.robinfriedli.aiode.audio.youtube.YouTubeService)

Example 2 with NoResultsFoundException

use of net.robinfriedli.aiode.exceptions.NoResultsFoundException in project aiode by robinfriedli.

the class SearchCommand method listLocalList.

private void listLocalList() {
    if (getCommandInput().isBlank()) {
        Session session = getContext().getSession();
        List<Playlist> playlists = getQueryBuilderFactory().find(Playlist.class).build(session).getResultList();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        if (playlists.isEmpty()) {
            embedBuilder.setDescription("No playlists");
        } else {
            EmbedTable table = new EmbedTable(embedBuilder);
            table.addColumn("Playlist", playlists, Playlist::getName);
            table.addColumn("Duration", playlists, playlist -> Util.normalizeMillis(playlist.getDuration()));
            table.addColumn("Items", playlists, playlist -> String.valueOf(playlist.getSize()));
            table.build();
        }
        sendMessage(embedBuilder);
    } else {
        Playlist playlist = SearchEngine.searchLocalList(getContext().getSession(), getCommandInput());
        if (playlist == null) {
            throw new NoResultsFoundException(String.format("No local list found for '%s'", getCommandInput()));
        }
        String createdUserId = playlist.getCreatedUserId();
        String createdUser;
        if (createdUserId.equals("system")) {
            createdUser = playlist.getCreatedUser();
        } else {
            ShardManager shardManager = Aiode.get().getShardManager();
            User userById;
            try {
                userById = shardManager.retrieveUserById(createdUserId).complete();
            } catch (ErrorResponseException e) {
                if (e.getErrorResponse() == ErrorResponse.UNKNOWN_USER) {
                    userById = null;
                } else {
                    throw e;
                }
            }
            createdUser = userById != null ? userById.getName() : playlist.getCreatedUser();
        }
        SpringPropertiesConfig springPropertiesConfig = Aiode.get().getSpringPropertiesConfig();
        String baseUri = springPropertiesConfig.requireApplicationProperty("aiode.server.base_uri");
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.addField("Name", playlist.getName(), true);
        embedBuilder.addField("Duration", Util.normalizeMillis(playlist.getDuration()), true);
        embedBuilder.addField("Created by", createdUser, true);
        embedBuilder.addField("Tracks", String.valueOf(playlist.getSize()), true);
        embedBuilder.addBlankField(false);
        String url = baseUri + String.format("/list?name=%s&guildId=%s", URLEncoder.encode(playlist.getName(), StandardCharsets.UTF_8), playlist.getGuildId());
        embedBuilder.addField("First tracks:", "[Full list](" + url + ")", false);
        List<PlaylistItem> items = playlist.getItemsSorted();
        Util.appendEmbedList(embedBuilder, items.size() > 5 ? items.subList(0, 5) : items, item -> item.display() + " - " + Util.normalizeMillis(item.getDuration()), "Track - Duration");
        sendWithLogo(embedBuilder);
    }
}
Also used : User(net.dv8tion.jda.api.entities.User) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) Playlist(net.robinfriedli.aiode.entities.Playlist) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) SpringPropertiesConfig(net.robinfriedli.aiode.boot.SpringPropertiesConfig) EmbedTable(net.robinfriedli.aiode.util.EmbedTable) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) Session(org.hibernate.Session)

Example 3 with NoResultsFoundException

use of net.robinfriedli.aiode.exceptions.NoResultsFoundException in project aiode by robinfriedli.

the class AbstractPlayableLoadingCommand method loadUrlItems.

private void loadUrlItems(AudioManager audioManager, AudioPlayback playback) throws IOException {
    PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
    List<Playable> playables = playableFactory.createPlayables(getCommandInput(), getContext().getSpotifyApi(), shouldRedirectSpotify());
    if (playables.isEmpty()) {
        throw new NoResultsFoundException("Result is empty!");
    }
    handleResults(playables);
    loadedAmount = playables.size();
}
Also used : PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable)

Example 4 with NoResultsFoundException

use of net.robinfriedli.aiode.exceptions.NoResultsFoundException in project aiode by robinfriedli.

the class AbstractPlayableLoadingCommand method loadSoundCloudTrack.

private void loadSoundCloudTrack(AudioManager audioManager) {
    AudioTrackLoader audioTrackLoader = new AudioTrackLoader(audioManager.getPlayerManager());
    String commandInput = getCommandInput();
    AudioItem audioItem = audioTrackLoader.loadByIdentifier("scsearch:" + commandInput);
    if (audioItem instanceof AudioTrack) {
        AudioTrack audioTrack = (AudioTrack) audioItem;
        handleResults(Lists.newArrayList(new UrlPlayable(audioTrack)));
        this.loadedAudioTrack = audioTrack;
    } else if (audioItem == null) {
        throw new NoResultsFoundException(String.format("No soundcloud track found for '%s'", commandInput));
    } else if (audioItem instanceof AudioPlaylist) {
        int limit = getArgumentValueWithTypeOrElse("select", Integer.class, 20);
        List<AudioTrack> tracks = ((AudioPlaylist) audioItem).getTracks();
        if (tracks.isEmpty()) {
            throw new NoResultsFoundException(String.format("No soundcloud track found for '%s'", commandInput));
        }
        if (tracks.size() > limit) {
            tracks = tracks.subList(0, limit);
        }
        askQuestion(tracks, audioTrack -> audioTrack.getInfo().title, audioTrack -> audioTrack.getInfo().author);
    }
}
Also used : UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) AudioTrackLoader(net.robinfriedli.aiode.audio.AudioTrackLoader) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)

Example 5 with NoResultsFoundException

use of net.robinfriedli.aiode.exceptions.NoResultsFoundException in project aiode by robinfriedli.

the class AbstractPlayableLoadingCommand method loadLocalList.

private void loadLocalList(AudioManager audioManager) throws Exception {
    Playlist playlist = SearchEngine.searchLocalList(getContext().getSession(), getCommandInput());
    if (playlist == null) {
        throw new NoResultsFoundException(String.format("No local playlist found for '%s'", getCommandInput()));
    }
    List<Object> items = runWithCredentials(() -> playlist.getTracks(getContext().getSpotifyApi()));
    if (items.isEmpty()) {
        throw new NoResultsFoundException("Playlist is empty");
    }
    PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), trackLoadingExecutor);
    List<Playable> playables = playableFactory.createPlayables(shouldRedirectSpotify(), items);
    handleResults(playables);
    loadedLocalList = playlist;
}
Also used : PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) YouTubePlaylist(net.robinfriedli.aiode.audio.youtube.YouTubePlaylist) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) Playlist(net.robinfriedli.aiode.entities.Playlist) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) UrlPlayable(net.robinfriedli.aiode.audio.UrlPlayable) Playable(net.robinfriedli.aiode.audio.Playable)

Aggregations

NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)23 Playlist (net.robinfriedli.aiode.entities.Playlist)11 Session (org.hibernate.Session)11 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)8 PlayableFactory (net.robinfriedli.aiode.audio.PlayableFactory)8 Playable (net.robinfriedli.aiode.audio.Playable)7 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)7 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)6 List (java.util.List)6 YouTubePlaylist (net.robinfriedli.aiode.audio.youtube.YouTubePlaylist)6 AudioItem (com.sedmelluq.discord.lavaplayer.track.AudioItem)5 IOException (java.io.IOException)5 Aiode (net.robinfriedli.aiode.Aiode)5 AudioTrackLoader (net.robinfriedli.aiode.audio.AudioTrackLoader)5 StringList (net.robinfriedli.stringlist.StringList)5 Lists (com.google.common.collect.Lists)4 Callable (java.util.concurrent.Callable)4 SpotifyTrack (net.robinfriedli.aiode.audio.spotify.SpotifyTrack)4 YouTubeService (net.robinfriedli.aiode.audio.youtube.YouTubeService)4 CommandContext (net.robinfriedli.aiode.command.CommandContext)4