Search in sources :

Example 11 with Playlist

use of net.robinfriedli.aiode.entities.Playlist in project aiode by robinfriedli.

the class SetPlaylistItemIndexTask method perform.

@Override
public void perform(@Nullable JDA shard) {
    try (Session session = sessionFactory.openSession()) {
        Query<Playlist> relevantPlaylistQuery = session.createQuery("from " + Playlist.class.getName() + " as p where " + "exists(from " + Song.class.getName() + " where item_index = null and playlist_pk = p.pk) or " + "exists(from " + Video.class.getName() + " where item_index = null and playlist_pk = p.pk) or " + "exists(from " + UrlTrack.class.getName() + " where item_index = null and playlist_pk = p.pk)", Playlist.class);
        List<Playlist> relevantPlaylists = relevantPlaylistQuery.getResultList();
        session.beginTransaction();
        UpdatePlaylistItemIndicesTask task = new UpdatePlaylistItemIndicesTask(relevantPlaylists);
        task.perform();
        session.getTransaction().commit();
    }
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) Song(net.robinfriedli.aiode.entities.Song) UpdatePlaylistItemIndicesTask(net.robinfriedli.aiode.persist.tasks.UpdatePlaylistItemIndicesTask) UrlTrack(net.robinfriedli.aiode.entities.UrlTrack) Session(org.hibernate.Session)

Example 12 with Playlist

use of net.robinfriedli.aiode.entities.Playlist in project aiode by robinfriedli.

the class AddCommand method withUserResponse.

@Override
public void withUserResponse(Object option) {
    Session session = getContext().getSession();
    String playlistName = getArgumentValue(definingArgument);
    Playlist playlist = SearchEngine.searchLocalList(session, playlistName);
    if (playlist == null) {
        throw new NoResultsFoundException(String.format("No local list found for '%s'", getToAddString()));
    }
    PlayableFactory playableFactory = Aiode.get().getAudioManager().createPlayableFactory(getSpotifyService(), new BlockingTrackLoadingExecutor());
    List<Playable> playables = playableFactory.createPlayables(false, option);
    addPlayables(playlist, playables);
}
Also used : PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) Playlist(net.robinfriedli.aiode.entities.Playlist) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) Playable(net.robinfriedli.aiode.audio.Playable) BlockingTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor) Session(org.hibernate.Session)

Example 13 with Playlist

use of net.robinfriedli.aiode.entities.Playlist in project aiode by robinfriedli.

the class AddCommand method doRun.

@Override
public void doRun() throws Exception {
    Session session = getContext().getSession();
    if (argumentSet("queue")) {
        AudioQueue queue = Aiode.get().getAudioManager().getQueue(getContext().getGuild());
        if (queue.isEmpty()) {
            throw new InvalidCommandException("Queue is empty");
        }
        Playlist playlist = SearchEngine.searchLocalList(session, getToAddString());
        if (playlist == null) {
            throw new NoResultsFoundException(String.format("No local list found for '%s'", getToAddString()));
        }
        List<Playable> tracks = queue.getTracks();
        addPlayables(playlist, tracks);
    } else {
        if (!argumentSet(definingArgument)) {
            throw new InvalidCommandException(String.format("Expected argument '%s', defining the target playlist. " + "Hint: if you meant to add tracks to the queue, use the queue command instead.", definingArgument));
        }
        String playlistName = getArgumentValue(definingArgument);
        playlist = SearchEngine.searchLocalList(session, playlistName);
        if (playlist == null) {
            throw new NoResultsFoundException(String.format("No local list found for '%s'", playlistName));
        }
        super.doRun();
    }
}
Also used : Playlist(net.robinfriedli.aiode.entities.Playlist) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) Playable(net.robinfriedli.aiode.audio.Playable) AudioQueue(net.robinfriedli.aiode.audio.AudioQueue) Session(org.hibernate.Session)

Example 14 with Playlist

use of net.robinfriedli.aiode.entities.Playlist in project aiode by robinfriedli.

the class AddCommand method addToList.

protected void addToList(Playlist playlist, List<PlaylistItem> items) {
    if (items.isEmpty()) {
        throw new NoResultsFoundException("Result is empty!");
    }
    Session session = getContext().getSession();
    invoke(() -> items.forEach(item -> {
        item.add();
        session.persist(item);
    }));
}
Also used : PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) CommandManager(net.robinfriedli.aiode.command.CommandManager) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) AudioQueue(net.robinfriedli.aiode.audio.AudioQueue) Playable(net.robinfriedli.aiode.audio.Playable) Session(org.hibernate.Session) AbstractPlayableLoadingCommand(net.robinfriedli.aiode.command.commands.AbstractPlayableLoadingCommand) Aiode(net.robinfriedli.aiode.Aiode) List(java.util.List) Lists(com.google.common.collect.Lists) Playlist(net.robinfriedli.aiode.entities.Playlist) HollowYouTubeVideo(net.robinfriedli.aiode.audio.youtube.HollowYouTubeVideo) CommandContext(net.robinfriedli.aiode.command.CommandContext) BlockingTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) NoResultsFoundException(net.robinfriedli.aiode.exceptions.NoResultsFoundException) Session(org.hibernate.Session)

Example 15 with Playlist

use of net.robinfriedli.aiode.entities.Playlist in project aiode by robinfriedli.

the class GuildManager method handleNewGuild.

private void handleNewGuild(Guild guild, GuildContext guildContext) {
    Aiode aiode = Aiode.get();
    MessageService messageService = aiode.getMessageService();
    try {
        EmbedDocumentContribution embedDocumentContribution = embedDocumentContext.query(attribute("name").is("getting-started"), EmbedDocumentContribution.class).getOnlyResult();
        if (embedDocumentContribution != null) {
            EmbedBuilder embedBuilder = embedDocumentContribution.buildEmbed();
            TextChannel defaultTextChannelForGuild = getDefaultTextChannelForGuild(guild, guildContext);
            if (defaultTextChannelForGuild != null) {
                messageService.sendWithLogo(embedBuilder, defaultTextChannelForGuild);
            }
        }
    } catch (Exception e) {
        logger.error("Error sending getting started message", e);
    }
    SessionFactory sessionFactory = hibernateComponent.getSessionFactory();
    SpotifyApi.Builder spotifyApiBuilder = aiode.getSpotifyApiBuilder();
    if (defaultPlaylistContext != null) {
        try (Session session = sessionFactory.withOptions().interceptor(InterceptorChain.of(PlaylistItemTimestampInterceptor.class, VerifyPlaylistInterceptor.class)).openSession()) {
            HibernatePlaylistMigrator hibernatePlaylistMigrator = new HibernatePlaylistMigrator(defaultPlaylistContext, guild, spotifyApiBuilder.build(), session);
            Map<Playlist, List<PlaylistItem>> playlistMap = hibernatePlaylistMigrator.perform();
            Mode mode = getMode();
            HibernateInvoker.create(session).invokeConsumer(currentSession -> {
                for (Playlist playlist : playlistMap.keySet()) {
                    Playlist existingList = SearchEngine.searchLocalList(currentSession, playlist.getName(), mode == GuildManager.Mode.PARTITIONED, guild.getId());
                    if (existingList == null) {
                        playlistMap.get(playlist).forEach(item -> {
                            item.add();
                            currentSession.persist(item);
                        });
                        currentSession.persist(playlist);
                    }
                }
            });
        } catch (Exception e) {
            logger.error("Exception while setting up default playlists", e);
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) HibernatePlaylistMigrator(net.robinfriedli.aiode.persist.tasks.HibernatePlaylistMigrator) Aiode(net.robinfriedli.aiode.Aiode) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Playlist(net.robinfriedli.aiode.entities.Playlist) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) EmbedDocumentContribution(net.robinfriedli.aiode.entities.xml.EmbedDocumentContribution) List(java.util.List) Session(org.hibernate.Session)

Aggregations

Playlist (net.robinfriedli.aiode.entities.Playlist)24 Session (org.hibernate.Session)15 PlaylistItem (net.robinfriedli.aiode.entities.PlaylistItem)12 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)10 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)10 List (java.util.List)8 Playable (net.robinfriedli.aiode.audio.Playable)5 User (net.dv8tion.jda.api.entities.User)4 Aiode (net.robinfriedli.aiode.Aiode)4 UrlTrack (net.robinfriedli.aiode.entities.UrlTrack)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)3 PlayableFactory (net.robinfriedli.aiode.audio.PlayableFactory)3 CommandContext (net.robinfriedli.aiode.command.CommandContext)3 CommandManager (net.robinfriedli.aiode.command.CommandManager)3 Song (net.robinfriedli.aiode.entities.Song)3 CommandContribution (net.robinfriedli.aiode.entities.xml.CommandContribution)3 SearchEngine (net.robinfriedli.aiode.util.SearchEngine)3