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;
}
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);
}
}
Aggregations