Search in sources :

Example 1 with EmbedData

use of discord4j.discordjson.json.EmbedData in project KaellyBot by Kaysoro.

the class AlmanaxCalendar method start.

public static void start() {
    if (!isStarted) {
        isStarted = true;
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        long firstDelay = LocalDateTime.now().until(LocalDate.now().plusDays(1).atStartOfDay().plusMinutes(1), ChronoUnit.MINUTES);
        long period = TimeUnit.DAYS.toMinutes(1);
        scheduler.scheduleAtFixedRate(() -> {
            boolean success = false;
            Map<Language, EmbedData> almanax = new HashMap<>();
            while (!success) try {
                almanax.clear();
                for (Language lg : Language.values()) almanax.put(lg, Almanax.get(lg, new Date()).decorateRestEmbedObject(lg));
                success = true;
            } catch (IOException e) {
                ExceptionManager.manageSilentlyIOException(e);
                try {
                    // 5min
                    Thread.sleep(300000);
                } catch (InterruptedException e1) {
                    LOG.error("start", e1);
                }
            }
            for (AlmanaxCalendar calendar : getAlmanaxCalendars().values()) {
                try {
                    RestChannel chan = ClientConfig.DISCORD().getChannelById(Snowflake.of(calendar.chan));
                    Language lg = Translator.getLanguageFrom(chan);
                    chan.createMessage(almanax.get(lg)).doOnError(error -> {
                        if (error instanceof ClientException) {
                            LOG.warn("AlmanaxCalendar: no access on " + calendar.getChan());
                            calendar.removeToDatabase();
                        } else
                            LOG.error("AlmanaxCalendar", error);
                    }).subscribe();
                } catch (ClientException e) {
                    LOG.warn("AlmanaxCalendar: no access on " + calendar.getChan());
                    calendar.removeToDatabase();
                } catch (Exception e) {
                    LOG.error("AlmanaxCalendar", e);
                }
            }
        }, firstDelay, period, TimeUnit.MINUTES);
    }
}
Also used : Language(enums.Language) java.util(java.util) Logger(org.slf4j.Logger) Connection(java.sql.Connection) ClientException(discord4j.rest.http.client.ClientException) Almanax(data.Almanax) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RestChannel(discord4j.rest.entity.RestChannel) IOException(java.io.IOException) EmbedData(discord4j.discordjson.json.EmbedData) Snowflake(discord4j.common.util.Snowflake) PreparedStatement(java.sql.PreparedStatement) Executors(java.util.concurrent.Executors) ExceptionManager(exceptions.ExceptionManager) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) SQLException(java.sql.SQLException) ChronoUnit(java.time.temporal.ChronoUnit) util(util) ResultSet(java.sql.ResultSet) LocalDate(java.time.LocalDate) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RestChannel(discord4j.rest.entity.RestChannel) IOException(java.io.IOException) EmbedData(discord4j.discordjson.json.EmbedData) LocalDate(java.time.LocalDate) ClientException(discord4j.rest.http.client.ClientException) IOException(java.io.IOException) SQLException(java.sql.SQLException) Language(enums.Language) ClientException(discord4j.rest.http.client.ClientException)

Aggregations

Almanax (data.Almanax)1 Snowflake (discord4j.common.util.Snowflake)1 EmbedData (discord4j.discordjson.json.EmbedData)1 RestChannel (discord4j.rest.entity.RestChannel)1 ClientException (discord4j.rest.http.client.ClientException)1 Language (enums.Language)1 ExceptionManager (exceptions.ExceptionManager)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 ChronoUnit (java.time.temporal.ChronoUnit)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1