From 0e3a9d3f9becb57a56e315c713686654da309db0 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 19 Nov 2022 23:27:45 +0800 Subject: [PATCH] add function to randomly reply "?" message, added a CommonRandom tool, replace all jb.NotNull to javax.Nonnull --- gradle.properties | 2 +- .../cono/morny/bot/command/Ip186Query.java | 5 ++- .../cono/morny/bot/command/MornyCommands.java | 3 +- .../cono/morny/bot/command/私わね.java | 5 +-- .../cono/morny/bot/event/EventListeners.java | 2 ++ .../bot/event/OnMedicationNotifyApply.java | 7 ++-- .../morny/bot/event/OnQuestionMarkReply.java | 33 +++++++++++++++++++ .../cono/morny/bot/event/OnUserRandoms.java | 11 ++++--- .../sukazyo/cono/morny/util/CommonRandom.java | 22 +++++++++++++ 9 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/OnQuestionMarkReply.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/CommonRandom.java diff --git a/gradle.properties b/gradle.properties index aee182c..ecb6ab9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ MORNY_ARCHIVE_NAME = morny-coeur MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s -VERSION = 1.0.0-RC3.1 +VERSION = 1.0.0-RC3.2 USE_DELTA = false VERSION_DELTA = diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java index 212305a..4d5b4f3 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java @@ -7,7 +7,6 @@ import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; -import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -29,7 +28,7 @@ public class Ip186Query { @Nullable @Override public String[] getAliases () { return new String[0]; } @Nonnull @Override public String getParamRule () { return "[ip]"; } @Nonnull @Override public String getDescription () { return "通过 https://ip.186526.xyz 查询 ip 资料"; } - @Override public void execute (@NotNull InputCommand command, @NotNull Update event) { exec(event, command); } + @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { exec(event, command); } } public static class Whois implements ITelegramCommand { @@ -37,7 +36,7 @@ public class Ip186Query { @Nullable @Override public String[] getAliases () { return new String[0]; } @Nonnull @Override public String getParamRule () { return "[domain]"; } @Nonnull @Override public String getDescription () { return "通过 https://ip.186526.xyz 查询域名资料"; } - @Override public void execute (@NotNull InputCommand command, @NotNull Update event) { exec(event, command); } + @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { exec(event, command); } } private static void exec (@Nonnull Update event, @Nonnull InputCommand command) { diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java index 5e14712..c3f78bb 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java @@ -14,7 +14,6 @@ import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.request.SendSticker; import com.pengrad.telegrambot.request.SetMyCommands; -import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -201,7 +200,7 @@ public class MornyCommands { private static class ExitAlias implements ISimpleCommand { @Nonnull @Override public String getName () { return "quit"; } @Nullable @Override public String[] getAliases () { return new String[]{"stop"}; } - @Override public void execute (@NotNull InputCommand command, @NotNull Update event) { onCommandExitExec(event); } + @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { onCommandExitExec(event); } } private static void onCommandExitExec (@Nonnull Update event) { if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java index b776046..6006c0d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java @@ -7,7 +7,8 @@ import com.pengrad.telegrambot.request.SendMessage; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.concurrent.ThreadLocalRandom; + +import static cc.sukazyo.cono.morny.util.CommonRandom.probabilityTrue; @SuppressWarnings("NonAsciiCharacters") public class 私わね implements ISimpleCommand { @@ -20,7 +21,7 @@ public class 私わね implements ISimpleCommand { @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { - if (ThreadLocalRandom.current().nextInt(521) == 0) { + if (probabilityTrue(521)) { // 可以接入未来的心情系统(如果有的话) // final String text = switch (ThreadLocalRandom.current().nextInt(11)) { // case 0,7,8,9,10 -> "才不是"; 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 6f6708b..76c9256 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 @@ -17,6 +17,7 @@ public class EventListeners { public static final OnMedicationNotifyApply MEDICATION_NOTIFY_APPLY = new OnMedicationNotifyApply(); public static final OnRandomlyTriggered RANDOMLY_TRIGGERED = new OnRandomlyTriggered(); public static final OnUniMeowTrigger UNI_MEOW_TRIGGER = new OnUniMeowTrigger(); + public static final OnQuestionMarkReply QUESTION_MARK_REPLY = new OnQuestionMarkReply(); public static void registerAllListeners () { EventListenerManager.addListener( @@ -28,6 +29,7 @@ public class EventListeners { UNI_MEOW_TRIGGER, RANDOMLY_TRIGGERED, USER_RANDOMS, + QUESTION_MARK_REPLY, USER_SLASH_ACTION, INLINE_QUERY, CALL_ME, 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 index 7217635..59ec01c 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnMedicationNotifyApply.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnMedicationNotifyApply.java @@ -5,17 +5,18 @@ import cc.sukazyo.cono.morny.bot.api.EventListener; import cc.sukazyo.cono.morny.daemon.MornyDaemons; import com.pengrad.telegrambot.model.Message; import com.pengrad.telegrambot.model.Update; -import org.jetbrains.annotations.NotNull; + +import javax.annotation.Nonnull; public class OnMedicationNotifyApply extends EventListener { @Override - public boolean onEditedChannelPost (@NotNull Update update) { + public boolean onEditedChannelPost (@Nonnull Update update) { return editedMessageProcess(update.editedChannelPost()); } @Override - public boolean onEditedMessage (@NotNull Update update) { + public boolean onEditedMessage (@Nonnull Update update) { return editedMessageProcess(update.editedMessage()); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnQuestionMarkReply.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnQuestionMarkReply.java new file mode 100644 index 0000000..00f1a90 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnQuestionMarkReply.java @@ -0,0 +1,33 @@ +package cc.sukazyo.cono.morny.bot.event; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.bot.api.EventListener; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.request.SendMessage; + +import javax.annotation.Nonnull; +import java.util.Set; + +import static cc.sukazyo.cono.morny.util.CommonRandom.probabilityTrue; + +public class OnQuestionMarkReply extends EventListener { + + public static final Set QUESTION_MARKS = Set.of("?", "?", "¿", "⁈", "⁇", "‽", "❔", "❓"); + + @Override + public boolean onMessage (@Nonnull Update update) { + + if (update.message().text() == null) return false; + + if (QUESTION_MARKS.contains(update.message().text()) && probabilityTrue(8)) { + MornyCoeur.extra().exec(new SendMessage( + update.message().chat().id(), update.message().text() + ).replyToMessageId(update.message().messageId())); + return true; + } + + return false; + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java index 79d8a28..b1deec4 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java @@ -4,19 +4,20 @@ import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.SendMessage; -import org.jetbrains.annotations.NotNull; -import java.util.concurrent.ThreadLocalRandom; +import javax.annotation.Nonnull; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static cc.sukazyo.cono.morny.util.CommonRandom.iif; + public class OnUserRandoms extends EventListener { private static final Pattern USER_OR_QUERY = Pattern.compile("(.+)(?:还是|or)(.+)"); private static final Pattern USER_IF_QUERY = Pattern.compile("(.+)[吗?|?]+$"); @Override - public boolean onMessage (@NotNull Update update) { + public boolean onMessage (@Nonnull Update update) { if (update.message().text() == null) return false; if (!update.message().text().startsWith("/")) return false; @@ -25,9 +26,9 @@ public class OnUserRandoms extends EventListener { String result = null; Matcher matcher; if ((matcher = USER_OR_QUERY.matcher(query)).find()) { - result = ThreadLocalRandom.current().nextBoolean() ? matcher.group(1) : matcher.group(2); + result = iif() ? matcher.group(1) : matcher.group(2); } else if ((matcher = USER_IF_QUERY.matcher(query)).matches()) { - result = (ThreadLocalRandom.current().nextBoolean()?"":"不") + matcher.group(1); + result = (iif()?"":"不") + matcher.group(1); } if (result == null) return false; diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonRandom.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonRandom.java new file mode 100644 index 0000000..4e90fff --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonRandom.java @@ -0,0 +1,22 @@ +package cc.sukazyo.cono.morny.util; + +import javax.annotation.Nonnegative; +import java.util.concurrent.ThreadLocalRandom; + +public class CommonRandom { + + public static boolean probabilityTrue (@Nonnegative int probability, @Nonnegative int base) { + if (probability < 1) throw new IllegalArgumentException("the probability must be a positive value!"); + if (base < 1) throw new IllegalArgumentException("the probability base must be a positive value!"); + return probability > ThreadLocalRandom.current().nextInt(base); + } + + public static boolean probabilityTrue (@Nonnegative int probabilityIn) { + return (probabilityTrue(1, probabilityIn)); + } + + public static boolean iif () { + return ThreadLocalRandom.current().nextBoolean(); + } + +}