From 692abb9f56cf0e2eac76e394cdd3082e24b1f09f Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 9 Oct 2023 21:51:22 +0800 Subject: [PATCH] =?UTF-8?q?fix=20OnQuestionMarkReply=20false-trigger,=20ad?= =?UTF-8?q?d=20=E5=B0=8A=E5=98=9F=E5=81=87=E5=98=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../morny/bot/event/MornyEventListeners.scala | 5 +- .../morny/bot/event/OnQuestionMarkReply.scala | 4 +- .../cono/morny/bot/event/OnUserRandom.scala | 74 +++++++++++++------ .../morny/event/OnQuestionMarkReplyTest.scala | 35 +++++++++ 5 files changed, 94 insertions(+), 26 deletions(-) create mode 100644 src/test/scala/cc/sukazyo/cono/morny/test/cc/sukazyo/cono/morny/event/OnQuestionMarkReplyTest.scala diff --git a/gradle.properties b/gradle.properties index 603ea0b..f7d4990 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.1.0-alpha1 +VERSION = 1.1.0-alpha2 USE_DELTA = false VERSION_DELTA = diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyEventListeners.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyEventListeners.scala index ea3149b..e3a6d1f 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyEventListeners.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyEventListeners.scala @@ -5,11 +5,14 @@ import cc.sukazyo.cono.morny.MornyCoeur class MornyEventListeners (using manager: EventListenerManager) (using coeur: MornyCoeur) { + private val $OnUserRandom = OnUserRandom() manager.register( // ACTIVITY_RECORDER // KUOHUANHUAN_NEED_SLEEP OnUniMeowTrigger(using coeur.commands), - OnUserRandom(), + $OnUserRandom.RandomSelect, + //noinspection NonAsciiCharacters + $OnUserRandom.尊嘟假嘟, OnQuestionMarkReply(), OnUserSlashAction(), OnCallMe(), diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnQuestionMarkReply.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnQuestionMarkReply.scala index 1aff5f2..a56e42d 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnQuestionMarkReply.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnQuestionMarkReply.scala @@ -36,7 +36,9 @@ object OnQuestionMarkReply { def isAllMessageMark (using text: String): Boolean = { boundary[Boolean] { - for (c <- text) if QUESTION_MARKS contains c then boundary.break(false) + for (c <- text) + if !(QUESTION_MARKS contains c) then + boundary break false true } } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserRandom.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserRandom.scala index 0611122..6232323 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserRandom.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserRandom.scala @@ -4,37 +4,65 @@ import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.bot.api.EventListener import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec import com.pengrad.telegrambot.model.Update -import com.pengrad.telegrambot.request.SendMessage +import com.pengrad.telegrambot.request.{AbstractSendRequest, SendMessage} +import com.pengrad.telegrambot.response.SendResponse import scala.language.postfixOps -class OnUserRandom (using coeur: MornyCoeur) extends EventListener { - - private val USER_OR_QUERY = "^(.+)(?:还是|or)(.+)$"r - private val USER_IF_QUERY = "^(.+)(?:吗\\?|?|\\?|吗?)$"r +class OnUserRandom (using coeur: MornyCoeur) { - override def onMessage(using update: Update): Boolean = { + object RandomSelect extends EventListener { - if update.message.text == null then return false - if !(update.message.text startsWith "/") then return false - import cc.sukazyo.cono.morny.util.UseRandom.rand_half - val query = update.message.text substring 1 - val result: String|Null = query match - case USER_OR_QUERY(_con1, _con2) => - if rand_half then _con1 else _con2 - case USER_IF_QUERY(_con) => - // for capability with [[OnQuestionMarkReply]] - if OnQuestionMarkReply.isAllMessageMark(using _con) then return false - (if rand_half then "不" else "") + _con - case _ => null + private val USER_OR_QUERY = "^(.+)(?:还是|or)(.+)$" r + private val USER_IF_QUERY = "^(.+)(?:吗\\?|?|\\?|吗?)$" r - if result == null then return false + override def onMessage (using update: Update): Boolean = { + + if update.message.text == null then return false + if !(update.message.text startsWith "/") then return false + + import cc.sukazyo.cono.morny.util.UseRandom.rand_half + val query = update.message.text substring 1 + val result: String | Null = query match + case USER_OR_QUERY(_con1, _con2) => + if rand_half then _con1 else _con2 + case USER_IF_QUERY(_con) => + // for capability with [[OnQuestionMarkReply]] + if OnQuestionMarkReply.isAllMessageMark(using _con) then return false + (if rand_half then "不" else "") + _con + case _ => null + + if result == null then return false + + coeur.account exec SendMessage( + update.message.chat.id, result + ).replyToMessageId(update.message.messageId) + true + + } - coeur.account exec SendMessage( - update.message.chat.id, result - ).replyToMessageId(update.message.messageId) - true + } + + //noinspection NonAsciiCharacters + object 尊嘟假嘟 extends EventListener { + + private val keywords = Array("尊嘟假嘟", "O.o", "o.O") + + override def onMessage (using event: Update): Boolean = { + + if event.message.text == null then return false + + if !(keywords contains event.message.text) then return false + + import cc.sukazyo.cono.morny.util.UseRandom.rand_half + coeur.account exec SendMessage( + event.message.chat.id, + if rand_half then "尊嘟" else "假嘟" + ).replyToMessageId(event.message.messageId) + true + + } } diff --git a/src/test/scala/cc/sukazyo/cono/morny/test/cc/sukazyo/cono/morny/event/OnQuestionMarkReplyTest.scala b/src/test/scala/cc/sukazyo/cono/morny/test/cc/sukazyo/cono/morny/event/OnQuestionMarkReplyTest.scala new file mode 100644 index 0000000..119e1ab --- /dev/null +++ b/src/test/scala/cc/sukazyo/cono/morny/test/cc/sukazyo/cono/morny/event/OnQuestionMarkReplyTest.scala @@ -0,0 +1,35 @@ +package cc.sukazyo.cono.morny.test.cc.sukazyo.cono.morny.event + +import cc.sukazyo.cono.morny.bot.event.OnQuestionMarkReply +import cc.sukazyo.cono.morny.test.MornyTests +import org.scalatest.prop.TableDrivenPropertyChecks + +class OnQuestionMarkReplyTest extends MornyTests with TableDrivenPropertyChecks { + + "on replying a question mark :" - { + + "on checking if a message is a question mark :" - { + + val examples = Table( + ("text", "is"), + ("回来了", false), + ("为什么?", false), + ("?这不合理", false), + ("??尊嘟假嘟", false), + ("?????", true), + ("?", true), + ("?", true), + ("??❔", true), + ) + forAll(examples) { (text, is) => + + s"$text should checked with result $is" in: + OnQuestionMarkReply.isAllMessageMark(using text) shouldEqual is + + } + + } + + } + +}