From 072a37b49b370c18822cd5754f3b49f331e2fd4f Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 7 Jun 2022 15:48:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=83=E8=8D=AF=E6=8F=90=E9=86=92=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=90=91=E9=A2=91=E9=81=93=E5=8F=91=E9=80=81=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E6=B6=88=E6=81=AF=EF=BC=8C=E5=8F=96=E6=B6=88pin?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=8F=90=E9=86=92=E8=A2=AB?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E7=9A=84=E8=BD=AE=E5=AD=90=20#36?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/event/EventListeners.java | 2 + .../bot/event/OnMedicationNotifyApply.java | 28 ++++++++++++ .../cono/morny/daemon/MedicationTimer.java | 44 +++++++++++++++---- .../cono/morny/daemon/MornyDaemons.java | 3 +- 6 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/OnMedicationNotifyApply.java diff --git a/gradle.properties b/gradle.properties index a990ed9..972a331 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.1.1 +VERSION = 0.7.1.2 CODENAME = fuzhou diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 107cd31..26be073 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,7 +4,7 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.7.1.1"; + public static final String VERSION = "0.7.1.2"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1654430498871L; + public static final long COMPILE_TIMESTAMP = 1654487392325L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java index c50e6b2..3ff7ef6 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java @@ -14,6 +14,7 @@ public class EventListeners { @SuppressWarnings("unused") static final OnKuohuanhuanNeedSleep KUOHUANHUAN_NEED_SLEEP = new OnKuohuanhuanNeedSleep(); public static final OnUserRandoms USER_RANDOMS = new OnUserRandoms(); public static final OnCallMsgSend CALL_MSG_SEND = new OnCallMsgSend(); + public static final OnMedicationNotifyApply MEDICATION_NOTIFY_APPLY = new OnMedicationNotifyApply(); public static void registerAllListeners () { EventListenerManager.addListener( @@ -26,6 +27,7 @@ public class EventListeners { INLINE_QUERY, CALL_ME, CALL_MSG_SEND, + MEDICATION_NOTIFY_APPLY, EVENT_HACK_HANDLE ); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnMedicationNotifyApply.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnMedicationNotifyApply.java new file mode 100644 index 0000000..a5bd835 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnMedicationNotifyApply.java @@ -0,0 +1,28 @@ +package cc.sukazyo.cono.morny.bot.event; + +import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.daemon.MedicationTimer; +import cc.sukazyo.cono.morny.daemon.MornyDaemons; +import com.pengrad.telegrambot.model.Message; +import com.pengrad.telegrambot.model.Update; +import org.jetbrains.annotations.NotNull; + +public class OnMedicationNotifyApply extends EventListener { + + @Override + public boolean onEditedChannelPost (@NotNull Update update) { + return editedMessageProcess(update.editedChannelPost()); + } + + @Override + public boolean onEditedMessage (@NotNull Update update) { + return editedMessageProcess(update.editedMessage()); + } + + private boolean editedMessageProcess (Message edited) { + if (edited.chat().id() != MedicationTimer.NOTIFY_CHAT) return false; + MornyDaemons.medicationTimerInstance.refreshNotificationWrite(edited); + return true; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java index 36d464a..5932bb8 100644 --- a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java @@ -1,19 +1,31 @@ package cc.sukazyo.cono.morny.daemon; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.cono.morny.data.TelegramStickers; -import com.pengrad.telegrambot.request.PinChatMessage; -import com.pengrad.telegrambot.request.SendSticker; +import cc.sukazyo.cono.morny.util.CommonFormatUtils; +import com.pengrad.telegrambot.model.Message; +import com.pengrad.telegrambot.model.MessageEntity; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.EditMessageText; +import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.response.SendResponse; +import java.util.ArrayList; +import java.util.List; + import static cc.sukazyo.cono.morny.Log.logger; public class MedicationTimer extends Thread { - public static final long NOTIFY_RECEIVE_CHAT = 5028252995L; + public static final long NOTIFY_CHAT = -1001729016815L; + public static final String NOTIFY_MESSAGE = "\uD83C\uDF65⏲"; + private static final String DAEMON_THREAD_NAME = "TIMER_Medication"; + + private static final long LAST_NOTIFY_ID_NULL = -1L; + private long lastNotify = LAST_NOTIFY_ID_NULL; + MedicationTimer () { - super("TIMER_Medication"); + super(DAEMON_THREAD_NAME); } @Override @@ -34,9 +46,25 @@ public class MedicationTimer extends Thread { logger.info("MedicationTimer stopped"); } - private static void sendNotification () { - SendResponse m = MornyCoeur.extra().exec(new SendSticker(NOTIFY_RECEIVE_CHAT, TelegramStickers.ID_PROGYNOVA)); - if (m.isOk()) MornyCoeur.extra().exec(new PinChatMessage(NOTIFY_RECEIVE_CHAT, m.message().messageId())); + private void sendNotification () { + final SendResponse resp = MornyCoeur.extra().exec(new SendMessage(NOTIFY_CHAT, NOTIFY_MESSAGE)); + if (resp.isOk()) lastNotify = resp.message().messageId(); + else lastNotify = LAST_NOTIFY_ID_NULL; + } + + public void refreshNotificationWrite (Message edited) { + if (edited.messageId() != lastNotify) return; + final String editTime = CommonFormatUtils.formatDate(edited.editDate()*1000, 8); + ArrayList entities = new ArrayList<>(); + if (edited.entities() != null) entities.addAll(List.of(edited.entities())); + entities.add(new MessageEntity(MessageEntity.Type.italic, edited.text().length() + "\n-- ".length(), editTime.length())); + EditMessageText sending = new EditMessageText( + NOTIFY_CHAT, + edited.messageId(), + edited.text() + "\n-- " + editTime + " --" + ).parseMode(ParseMode.HTML).entities(entities.toArray(MessageEntity[]::new)); + MornyCoeur.extra().exec(sending); + lastNotify = LAST_NOTIFY_ID_NULL; } private static long calcNextRoutineTimestamp () { diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java index 7fe2678..b7585aa 100644 --- a/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java @@ -4,12 +4,11 @@ import static cc.sukazyo.cono.morny.Log.logger; public class MornyDaemons { - static MedicationTimer medicationTimerInstance; + public static final MedicationTimer medicationTimerInstance = new MedicationTimer(); public static void start () { logger.info("ALL Morny Daemons starting..."); TrackerDataManager.init(); - medicationTimerInstance = new MedicationTimer(); medicationTimerInstance.start(); logger.info("Morny Daemons started."); }