Search in sources :

Example 1 with ClientException

use of discord4j.rest.http.client.ClientException 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)

Example 2 with ClientException

use of discord4j.rest.http.client.ClientException in project KaellyBot by Kaysoro.

the class RSSFinder method start.

public static void start() {
    if (!isStarted) {
        isStarted = true;
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(() -> {
            Map<Language, List<RSS>> allFeeds = new HashMap<>();
            for (Language lg : Language.values()) allFeeds.put(lg, RSS.getRSSFeeds(lg));
            for (RSSFinder finder : getRSSFinders().values()) try {
                RestChannel chan = ClientConfig.DISCORD().getChannelById(Snowflake.of(finder.chan));
                Language lg = Translator.getLanguageFrom(chan);
                List<RSS> rssFeeds = allFeeds.get(Translator.getLanguageFrom(chan));
                long lastRSS = -1;
                for (RSS rss : rssFeeds) if (rss.getDate() > finder.getLastRSS()) {
                    chan.createMessage(rss.decorateRestEmbedObject(lg)).doOnError(error -> {
                        if (error instanceof ClientException) {
                            LOG.warn("RSSFinder: no access on " + finder.getChan());
                            finder.removeToDatabase();
                        } else
                            LOG.error("RSSFinder", error);
                    }).subscribe();
                    lastRSS = rss.getDate();
                }
                if (lastRSS != -1)
                    finder.setLastRSS(lastRSS);
            } catch (ClientException e) {
                LOG.warn("RSSFinder: no access on " + finder.getChan());
                finder.removeToDatabase();
            } catch (Exception e) {
                Reporter.report(e);
                LOG.error("RSSFinder", e);
            }
        }, 0, DELTA, TimeUnit.MINUTES);
    }
}
Also used : Language(enums.Language) Logger(org.slf4j.Logger) Connection(java.sql.Connection) ClientException(discord4j.rest.http.client.ClientException) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RestChannel(discord4j.rest.entity.RestChannel) HashMap(java.util.HashMap) Snowflake(discord4j.common.util.Snowflake) PreparedStatement(java.sql.PreparedStatement) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) SQLException(java.sql.SQLException) List(java.util.List) util(util) ResultSet(java.sql.ResultSet) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Collections(java.util.Collections) RSS(data.RSS) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RSS(data.RSS) Language(enums.Language) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) List(java.util.List) RestChannel(discord4j.rest.entity.RestChannel) ClientException(discord4j.rest.http.client.ClientException) ClientException(discord4j.rest.http.client.ClientException) SQLException(java.sql.SQLException)

Aggregations

Snowflake (discord4j.common.util.Snowflake)2 RestChannel (discord4j.rest.entity.RestChannel)2 ClientException (discord4j.rest.http.client.ClientException)2 Language (enums.Language)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Executors (java.util.concurrent.Executors)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 TimeUnit (java.util.concurrent.TimeUnit)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 util (util)2 Almanax (data.Almanax)1 RSS (data.RSS)1 EmbedData (discord4j.discordjson.json.EmbedData)1 ExceptionManager (exceptions.ExceptionManager)1 IOException (java.io.IOException)1