Search in sources :

Example 1 with EmbedDocumentContribution

use of net.robinfriedli.aiode.entities.xml.EmbedDocumentContribution in project aiode by robinfriedli.

the class LoadDocumentCommand method runAdmin.

@Override
public void runAdmin() {
    JxpBackend jxpBackend = Aiode.get().getJxpBackend();
    EmbedBuilder embedBuilder;
    InputStream embedDocumentResource = getClass().getResourceAsStream("/xml-contributions/embedDocuments.xml");
    try (Context context = jxpBackend.createLazyContext(embedDocumentResource)) {
        if (getCommandInput().isBlank()) {
            List<EmbedDocumentContribution> documents = context.getInstancesOf(EmbedDocumentContribution.class);
            embedBuilder = new EmbedBuilder();
            Util.appendEmbedList(embedBuilder, documents, EmbedDocumentContribution::getName, "Documents");
        } else {
            EmbedDocumentContribution document = context.query(attribute("name").fuzzyIs(getCommandInput()), EmbedDocumentContribution.class).getOnlyResult();
            if (document == null) {
                throw new InvalidCommandException(String.format("No embed document found for '%s'", getCommandInput()));
            }
            embedBuilder = document.buildEmbed();
        }
    }
    sendMessage(embedBuilder);
}
Also used : Context(net.robinfriedli.jxp.persist.Context) CommandContext(net.robinfriedli.aiode.command.CommandContext) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) InputStream(java.io.InputStream) JxpBackend(net.robinfriedli.jxp.api.JxpBackend) EmbedDocumentContribution(net.robinfriedli.aiode.entities.xml.EmbedDocumentContribution)

Example 2 with EmbedDocumentContribution

use of net.robinfriedli.aiode.entities.xml.EmbedDocumentContribution 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

EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 EmbedDocumentContribution (net.robinfriedli.aiode.entities.xml.EmbedDocumentContribution)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 Aiode (net.robinfriedli.aiode.Aiode)1 CommandContext (net.robinfriedli.aiode.command.CommandContext)1 Playlist (net.robinfriedli.aiode.entities.Playlist)1 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)1 HibernatePlaylistMigrator (net.robinfriedli.aiode.persist.tasks.HibernatePlaylistMigrator)1 JxpBackend (net.robinfriedli.jxp.api.JxpBackend)1 Context (net.robinfriedli.jxp.persist.Context)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 SpotifyApi (se.michaelthelin.spotify.SpotifyApi)1