Search in sources :

Example 1 with Almanax

use of data.Almanax in project KaellyBot by Kaysoro.

the class AlmanaxCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Language lg = Translator.getLanguageFrom(message.getChannel());
        try {
            Date date = new Date();
            Matcher m = getMatcher(message);
            m.find();
            if (m.group(1) != null && (m.group(1).matches("\\s+true") || m.group(1).matches("\\s+0") || m.group(1).matches("\\s+on") || m.group(1).matches("\\s+false") || m.group(1).matches("\\s+1") || m.group(1).matches("\\s+off"))) {
                if (!message.getChannel().isPrivate()) {
                    if (isUserHasEnoughRights(message)) {
                        if (m.group(1).matches("\\s+true") || m.group(1).matches("\\s+0") || m.group(1).matches("\\s+on"))
                            if (!AlmanaxCalendar.getAlmanaxCalendars().containsKey(message.getChannel().getStringID())) {
                                new AlmanaxCalendar(message.getGuild().getStringID(), message.getChannel().getStringID()).addToDatabase();
                                Message.sendText(message.getChannel(), Translator.getLabel(lg, "almanax.request.1"));
                            } else
                                Message.sendText(message.getChannel(), Translator.getLabel(lg, "almanax.request.2"));
                        else if (m.group(1).matches("\\s+false") || m.group(1).matches("\\s+1") || m.group(1).matches("\\s+off"))
                            if (AlmanaxCalendar.getAlmanaxCalendars().containsKey(message.getChannel().getStringID())) {
                                AlmanaxCalendar.getAlmanaxCalendars().get(message.getChannel().getStringID()).removeToDatabase();
                                Message.sendText(message.getChannel(), Translator.getLabel(lg, "almanax.request.3"));
                            } else
                                Message.sendText(message.getChannel(), Translator.getLabel(lg, "almanax.request.4"));
                    } else
                        noEnoughRights.throwException(message, this, lg);
                } else
                    notUsableInMp.throwException(message, this, lg);
            } else if (m.group(1) != null && m.group(1).matches("\\s+\\+\\d")) {
                int number = Integer.parseInt(m.group(1).replaceAll("\\s+\\+", ""));
                Message.sendEmbed(message.getChannel(), Almanax.getGroupedObject(lg, new Date(), number));
            } else {
                if (m.group(1) != null && m.group(1).matches("\\s+\\d{2}/\\d{2}/\\d{4}"))
                    date = Almanax.discordToBot.parse(m.group(1));
                Almanax almanax = Almanax.get(lg, date);
                Message.sendEmbed(message.getChannel(), almanax.getMoreEmbedObject(lg));
            }
        } catch (ParseException e) {
            incorrectDateFormat.throwException(message, this, lg);
            return false;
        } catch (IOException e) {
            ExceptionManager.manageIOException(e, message, this, lg, notFound);
        } catch (Exception e) {
            ExceptionManager.manageException(e, message, this, lg);
        }
    }
    return false;
}
Also used : AlmanaxCalendar(finders.AlmanaxCalendar) Language(enums.Language) Matcher(java.util.regex.Matcher) Almanax(data.Almanax) ParseException(java.text.ParseException) IOException(java.io.IOException) Date(java.util.Date) IOException(java.io.IOException) Exception(java.lang.Exception) ParseException(java.text.ParseException)

Example 2 with Almanax

use of data.Almanax 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, Almanax> almanax = new HashMap<>();
            while (!success) try {
                almanax.clear();
                for (Language lg : Language.values()) almanax.put(lg, Almanax.get(lg, new Date()));
                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 {
                IChannel chan = ClientConfig.DISCORD().getChannelByID(Long.parseLong(calendar.getChan()));
                Language lg = Translator.getLanguageFrom(chan);
                if (chan != null && !chan.isDeleted())
                    Message.sendEmbed(chan, almanax.get(lg).getMoreEmbedObject(lg));
            } catch (Exception e) {
                LOG.error("AlmanaxCalendar", e);
            }
        }, firstDelay, period, TimeUnit.MINUTES);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) IChannel(sx.blah.discord.handle.obj.IChannel) HashMap(java.util.HashMap) Almanax(data.Almanax) IOException(java.io.IOException) Date(java.util.Date) LocalDate(java.time.LocalDate) IOException(java.io.IOException) SQLException(java.sql.SQLException) Language(enums.Language)

Aggregations

Almanax (data.Almanax)2 Language (enums.Language)2 IOException (java.io.IOException)2 Date (java.util.Date)2 AlmanaxCalendar (finders.AlmanaxCalendar)1 Exception (java.lang.Exception)1 SQLException (java.sql.SQLException)1 ParseException (java.text.ParseException)1 LocalDate (java.time.LocalDate)1 HashMap (java.util.HashMap)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Matcher (java.util.regex.Matcher)1 IChannel (sx.blah.discord.handle.obj.IChannel)1