Search in sources :

Example 1 with Version

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

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

the class VersionUpdateAlertTask method perform.

@Override
public void perform(@Nullable JDA shard) {
    Logger logger = LoggerFactory.getLogger(getClass());
    Version versionElem = versionManager.getCurrentVersion();
    if (versionElem != null) {
        Context context = versionManager.getContext();
        if (UPDATED || !versionElem.getAttribute("launched").getBool()) {
            UPDATED = true;
            if (!(versionElem.hasAttribute("silent") && versionElem.getAttribute("silent").getBool())) {
                sendUpdateAlert(context, versionElem, shard);
            }
            context.invoke(() -> versionElem.setAttribute("launched", true));
        }
    } else {
        logger.warn("Current version has no version element in versions.xml");
    }
}
Also used : Context(net.robinfriedli.jxp.persist.Context) Version(net.robinfriedli.aiode.entities.xml.Version) Logger(org.slf4j.Logger)

Aggregations

Version (net.robinfriedli.aiode.entities.xml.Version)2 Context (net.robinfriedli.jxp.persist.Context)2 Logger (org.slf4j.Logger)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 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 CheckedConsumer (net.robinfriedli.aiode.function.CheckedConsumer)1 StaticSessionProvider (net.robinfriedli.aiode.persist.StaticSessionProvider)1 XmlElement (net.robinfriedli.jxp.api.XmlElement)1