From 031a7990700e30b9ff43e460fe5fdd48b530ce2b Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 20 Nov 2022 12:45:20 +0800 Subject: [PATCH] support QuestionMarkReply to reply more than 1 mark char message. add javadoc --- gradle.properties | 2 +- .../morny/bot/event/OnQuestionMarkReply.java | 19 ++++++++++------ .../sukazyo/cono/morny/util/CommonRandom.java | 22 +++++++++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/gradle.properties b/gradle.properties index ecb6ab9..f3bdb84 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.2 +VERSION = 1.0.0-RC3.3 USE_DELTA = false VERSION_DELTA = 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 index 00f1a90..8e51eeb 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnQuestionMarkReply.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnQuestionMarkReply.java @@ -12,21 +12,26 @@ import static cc.sukazyo.cono.morny.util.CommonRandom.probabilityTrue; public class OnQuestionMarkReply extends EventListener { - public static final Set QUESTION_MARKS = Set.of("?", "?", "¿", "⁈", "⁇", "‽", "❔", "❓"); + /** + * 一个 unicode 的问号字符列表. 不仅有半角全角问号,也包含了变体问号,和叹号结合的问好以及 uni-emoji 问号。 + * @since 1.0.0-RC3.2 + */ + 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; + if (!probabilityTrue(8)) return false; + for (char c : update.message().text().toCharArray()) { + if (!QUESTION_MARKS.contains(c)) return false; } - return false; + MornyCoeur.extra().exec(new SendMessage( + update.message().chat().id(), update.message().text() + ).replyToMessageId(update.message().messageId())); + return true; } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonRandom.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonRandom.java index 4e90fff..22c9812 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/CommonRandom.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonRandom.java @@ -5,16 +5,38 @@ import java.util.concurrent.ThreadLocalRandom; public class CommonRandom { + /** + * 通过 {@link ThreadLocalRandom} 以指定的一定几率返回 true. + * @param probability 一个正整数,决定在样本空间中有多大的可能性为 true。 + * @param base 一个正整数,决定样本空间有多大。 + * @return 有 {@code base} 分之 {@code probability} 的几率,返回值为 {@link true}. + * 如果 {@code probability} 大于 {@code base},也就是为 true 的可能性大于 100%,则会永远为 true。 + * @throws IllegalArgumentException + * 当参数 base 或是 probability 不为正整数时 + * @since 1.0.0-RC3.2 + */ 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); } + /** + * 以一定几率返回 true. + * @return {@code probabilityIn} 分之 {@link 1} 的几率为 {@link true}. + * @see #probabilityTrue(int, int) + * @since 1.0.0-RC3.2 + */ public static boolean probabilityTrue (@Nonnegative int probabilityIn) { return (probabilityTrue(1, probabilityIn)); } + /** + * 通过 {@link ThreadLocalRandom} 实现的随机 boolean 取值. + * @return 随机的 {@link true} 或 {@link false},各占(近似)一半可能性. + * @see ThreadLocalRandom#nextBoolean() + * @since 1.0.0-RC3.2 + */ public static boolean iif () { return ThreadLocalRandom.current().nextBoolean(); }