Search in sources :

Example 1 with CheckedConsumer

use of net.robinfriedli.aiode.function.CheckedConsumer in project aiode by robinfriedli.

the class VersionUpdateAlertTask method sendUpdateAlert.

private void sendUpdateAlert(Context context, Version versionElem, JDA shard) {
    List<XmlElement> lowerLaunchedVersions = context.query(and(tagName("version"), attribute("launched").is(true), lowerVersionThan(versionElem.getVersion()))).collect();
    if (!lowerLaunchedVersions.isEmpty()) {
        String message = "Aiode has been updated to " + versionElem.getVersion() + ". [Check the releases here](" + "https://github.com/robinfriedli/botify/releases)";
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setTitle("Update");
        embedBuilder.setDescription(message);
        List<XmlElement> features = versionElem.query(tagName("feature")).collect();
        if (!features.isEmpty()) {
            embedBuilder.addField("**Features**", "Changes in this update", false);
            for (XmlElement feature : features) {
                embedBuilder.addField(feature.getAttribute("title").getValue(), feature.getTextContent(), false);
            }
        }
        List<Guild> guilds = shard.getGuilds();
        long delaySecs = OFFSET++ * (guilds.size() / MESSAGES_PER_SECOND);
        if (delaySecs > 0) {
            delaySecs += 10;
        }
        MESSAGE_DISPATCH.schedule(() -> {
            // with the other shards
            synchronized (DISPATCH_LOCK) {
                // setup current thread session and handle all guilds within one session instead of opening a new session for each
                StaticSessionProvider.consumeSession((CheckedConsumer<Session>) session -> {
                    int counter = 0;
                    long currentTimeMillis = System.currentTimeMillis();
                    for (Guild guild : guilds) {
                        messageService.sendWithLogo(embedBuilder, guild);
                        if (++counter % MESSAGES_PER_SECOND == 0) {
                            long delta = System.currentTimeMillis() - currentTimeMillis;
                            if (delta < 1000) {
                                Thread.sleep(1000 - delta);
                            }
                            currentTimeMillis = System.currentTimeMillis();
                        }
                    }
                });
            }
        }, delaySecs, TimeUnit.SECONDS);
    }
}
Also used : CheckedConsumer(net.robinfriedli.aiode.function.CheckedConsumer) Context(net.robinfriedli.jxp.persist.Context) StartupTask(net.robinfriedli.aiode.boot.StartupTask) Logger(org.slf4j.Logger) JDA(net.dv8tion.jda.api.JDA) MessageService(net.robinfriedli.aiode.discord.MessageService) LoggerFactory(org.slf4j.LoggerFactory) VersionManager(net.robinfriedli.aiode.boot.VersionManager) Session(org.hibernate.Session) LoggingThreadFactory(net.robinfriedli.aiode.concurrent.LoggingThreadFactory) StartupTaskContribution(net.robinfriedli.aiode.entities.xml.StartupTaskContribution) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Version(net.robinfriedli.aiode.entities.xml.Version) Conditions(net.robinfriedli.jxp.queries.Conditions) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Guild(net.dv8tion.jda.api.entities.Guild) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StaticSessionProvider(net.robinfriedli.aiode.persist.StaticSessionProvider) XmlElement(net.robinfriedli.jxp.api.XmlElement) Conditions(net.robinfriedli.aiode.boot.VersionManager.Conditions) Nullable(javax.annotation.Nullable) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) XmlElement(net.robinfriedli.jxp.api.XmlElement) Guild(net.dv8tion.jda.api.entities.Guild) Session(org.hibernate.Session)

Example 2 with CheckedConsumer

use of net.robinfriedli.aiode.function.CheckedConsumer in project aiode by robinfriedli.

the class HollowYouTubeVideo method fetch.

@Override
public Playable fetch() {
    // only supported for Spotify redirect as YouTube does not allow loading specific playlist items
    if (isHollow() && redirectedSpotifyTrack != null) {
        markLoading();
        EagerFetchQueue.submitFetch(() -> StaticSessionProvider.consumeSession((CheckedConsumer<Session>) session -> {
            SpotifyRedirectService spotifyRedirectService = new SpotifyRedirectService(session, youTubeService);
            spotifyRedirectService.redirectTrack(this);
        }));
    }
    return this;
}
Also used : SpotifyRedirectService(net.robinfriedli.aiode.audio.spotify.SpotifyRedirectService) CheckedConsumer(net.robinfriedli.aiode.function.CheckedConsumer)

Aggregations

CheckedConsumer (net.robinfriedli.aiode.function.CheckedConsumer)2 List (java.util.List)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 Nullable (javax.annotation.Nullable)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 JDA (net.dv8tion.jda.api.JDA)1 Guild (net.dv8tion.jda.api.entities.Guild)1 SpotifyRedirectService (net.robinfriedli.aiode.audio.spotify.SpotifyRedirectService)1 StartupTask (net.robinfriedli.aiode.boot.StartupTask)1 VersionManager (net.robinfriedli.aiode.boot.VersionManager)1 Conditions (net.robinfriedli.aiode.boot.VersionManager.Conditions)1 LoggingThreadFactory (net.robinfriedli.aiode.concurrent.LoggingThreadFactory)1 MessageService (net.robinfriedli.aiode.discord.MessageService)1 StartupTaskContribution (net.robinfriedli.aiode.entities.xml.StartupTaskContribution)1 Version (net.robinfriedli.aiode.entities.xml.Version)1 StaticSessionProvider (net.robinfriedli.aiode.persist.StaticSessionProvider)1 XmlElement (net.robinfriedli.jxp.api.XmlElement)1 Context (net.robinfriedli.jxp.persist.Context)1