From 925bc0a805baeb6ada093937429c2682739c37cc Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sat, 17 Apr 2021 20:03:10 +0200 Subject: [PATCH] Added !anniv feature (#5343). --- src/org/april/hebdobot/bot/Hebdobot.java | 6 +- .../april/hebdobot/bot/hooks/AnnivHook.java | 103 ++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/org/april/hebdobot/bot/hooks/AnnivHook.java diff --git a/src/org/april/hebdobot/bot/Hebdobot.java b/src/org/april/hebdobot/bot/Hebdobot.java index d267095..c8ec3d9 100644 --- a/src/org/april/hebdobot/bot/Hebdobot.java +++ b/src/org/april/hebdobot/bot/Hebdobot.java @@ -27,6 +27,7 @@ import java.time.LocalTime; import org.apache.commons.lang3.StringUtils; import org.april.hebdobot.HebdobotException; +import org.april.hebdobot.bot.hooks.AnnivHook; import org.april.hebdobot.bot.hooks.BadCommandHook; import org.april.hebdobot.bot.hooks.CancelPreviousInputHook; import org.april.hebdobot.bot.hooks.ChronoHook; @@ -142,6 +143,8 @@ public class Hebdobot extends PircBot // this.hooker = new Hooker(); + this.hooker.add(new CancelPreviousInputHook()); + this.hooker.add(new ChronoHook()); this.hooker.add(new CollectiveSubjectHook()); this.hooker.add(new CommentHook()); this.hooker.add(new CurrentHook()); @@ -154,9 +157,8 @@ public class Hebdobot extends PircBot this.hooker.add(new StopReviewHook()); this.hooker.add(new StatsHook()); this.hooker.add(new StatusHook()); - this.hooker.add(new CancelPreviousInputHook()); - this.hooker.add(new ChronoHook()); + this.hooker.add(new AnnivHook()); this.hooker.add(new DateHook()); this.hooker.add(new HelloHook()); this.hooker.add(new LicenseHook()); diff --git a/src/org/april/hebdobot/bot/hooks/AnnivHook.java b/src/org/april/hebdobot/bot/hooks/AnnivHook.java new file mode 100644 index 0000000..6c758b2 --- /dev/null +++ b/src/org/april/hebdobot/bot/hooks/AnnivHook.java @@ -0,0 +1,103 @@ +/** + * Copyright (C) 2021 Christian Pierre MOMON + * + * This file is part of (April) Hebdobot. + * + * Hebdobot is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Hebdobot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Hebdobot. If not, see + */ +package org.april.hebdobot.bot.hooks; + +import java.time.LocalDate; +import java.time.Period; +import java.time.format.DateTimeFormatter; +import java.util.Locale; + +import org.apache.commons.lang3.StringUtils; +import org.april.hebdobot.HebdobotException; +import org.april.hebdobot.bot.Hebdobot; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.strings.StringList; +import fr.devinsy.strings.StringsUtils; + +/** + * The Class AnnivHook. + */ +public class AnnivHook extends Hook +{ + private static final Logger logger = LoggerFactory.getLogger(AnnivHook.class); + + /* (non-Javadoc) + * @see org.april.hebdobot.bot.hooks.Hook#attemptHelp(java.lang.String) + */ + @Override + public StringList attemptHelp(final String token) throws HebdobotException + { + StringList result; + + if (StringsUtils.equalsAnyIgnoreCase(token, "anniv")) + { + result = new StringList("!anniv : afficher des anniversaires"); + } + else + { + result = null; + } + + // + return result; + } + + /* (non-Javadoc) + * @see org.april.hebdobot.bot.hooks.Hook#attemptProcess(org.april.hebdobot.bot.Hebdobot, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public boolean attemptProcess(final Hebdobot bot, final String channel, final String sender, final String login, final String hostname, + final String message) + { + boolean result; + + if (StringUtils.equalsAnyIgnoreCase(message, "!anniv")) + { + logger.info("!anniv caught."); + + LocalDate now = LocalDate.now(); + LocalDate revueBirthdate = LocalDate.of(2010, 04, 30); + String birthdate = revueBirthdate.format(DateTimeFormatter.ofPattern("EEEE dd MMMM yyyy", Locale.FRENCH)); + int years = Period.between(revueBirthdate, now).getYears(); + + bot.sendMessage(String.format("La revue hebdomadaire est née le %s et a %d ans", birthdate, years)); + + LocalDate hebdobotBirthdate = LocalDate.of(2011, 9, 9); + years = Period.between(hebdobotBirthdate, now).getYears(); + birthdate = hebdobotBirthdate.format(DateTimeFormatter.ofPattern("EEEE dd MMMM yyyy", Locale.FRENCH)); + bot.sendMessage(String.format("Hebdobot a géré sa première revue le %s et a %d ans", birthdate, years)); + + LocalDate aprilBirthdate = LocalDate.of(1996, 11, 20); + birthdate = aprilBirthdate.format(DateTimeFormatter.ofPattern("EEEE dd MMMM yyyy", Locale.FRENCH)); + years = Period.between(aprilBirthdate, now).getYears(); + bot.sendMessage(String.format("L'April a été déclarée en préférecture le %s et a %d ans", birthdate, years)); + + result = true; + } + else + { + result = false; + } + + // + return result; + } +}