diff --git a/gradle.properties b/gradle.properties index a21d2a6..9f87499 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.1 +VERSION = 1.1.1.xiongan-dev1 USE_DELTA = false VERSION_DELTA = diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventEnv.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventEnv.scala new file mode 100644 index 0000000..ec34522 --- /dev/null +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventEnv.scala @@ -0,0 +1,43 @@ +package cc.sukazyo.cono.morny.bot.api + +import com.pengrad.telegrambot.model.Update + +import scala.collection.mutable +import scala.reflect.ClassTag +import scala.util.boundary + +class EventEnv ( + + val update: Update + +) { + + private var _isOk: Int = 0 + private val variables: mutable.HashMap[Class[?], Any] = mutable.HashMap.empty + + def isEventOk: Boolean = _isOk > 0 + + //noinspection UnitMethodIsParameterless + def setEventOk: Unit = + _isOk = _isOk + 1 + + def provide (i: Any): Unit = + variables += (i.getClass -> i) + + def use [T] (t: Class[T]): ConsumeProvider[T] = ConsumeProvider(t) + + class ConsumeProvider[T] (t: Class[T]) { + def consume (consumer: T => Unit): ConsumeResult = { + variables get t match + case Some(i) => consumer(i.asInstanceOf[T]); ConsumeResult(true) + case None => ConsumeResult(false) + } + } + + class ConsumeResult (success: Boolean) { + def onfail (processor: => Unit): Unit = { + if !success then processor + } + } + +} diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventListener.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventListener.scala index 0a3e1ac..3c2fbb9 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventListener.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventListener.scala @@ -1,22 +1,20 @@ package cc.sukazyo.cono.morny.bot.api -import com.pengrad.telegrambot.model.Update - trait EventListener () { - def onMessage (using Update): Boolean = false - def onEditedMessage (using Update): Boolean = false - def onChannelPost (using Update): Boolean = false - def onEditedChannelPost (using Update): Boolean = false - def onInlineQuery (using Update): Boolean = false - def onChosenInlineResult (using Update): Boolean = false - def onCallbackQuery (using Update): Boolean = false - def onShippingQuery (using Update): Boolean = false - def onPreCheckoutQuery (using Update): Boolean = false - def onPoll (using Update): Boolean = false - def onPollAnswer (using Update): Boolean = false - def onMyChatMemberUpdated (using Update): Boolean = false - def onChatMemberUpdated (using Update): Boolean = false - def onChatJoinRequest (using Update): Boolean = false + def onMessage (using EventEnv): Unit = {} + def onEditedMessage (using EventEnv): Unit = {} + def onChannelPost (using EventEnv): Unit = {} + def onEditedChannelPost (using EventEnv): Unit = {} + def onInlineQuery (using EventEnv): Unit = {} + def onChosenInlineResult (using EventEnv): Unit = {} + def onCallbackQuery (using EventEnv): Unit = {} + def onShippingQuery (using EventEnv): Unit = {} + def onPreCheckoutQuery (using EventEnv): Unit = {} + def onPoll (using EventEnv): Unit = {} + def onPollAnswer (using EventEnv): Unit = {} + def onMyChatMemberUpdated (using EventEnv): Unit = {} + def onChatMemberUpdated (using EventEnv): Unit = {} + def onChatJoinRequest (using EventEnv): Unit = {} } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventListenerManager.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventListenerManager.scala index 5448204..de846ab 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventListenerManager.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/api/EventListenerManager.scala @@ -9,6 +9,7 @@ import com.pengrad.telegrambot.UpdatesListener import scala.collection.mutable import scala.language.postfixOps +import scala.util.boundary /** Contains a [[mutable.Queue]] of [[EventListener]], and delivery telegram [[Update]]. * @@ -23,46 +24,43 @@ class EventListenerManager (using coeur: MornyCoeur) extends UpdatesListener { def register (listeners: EventListener*): Unit = this.listeners ++= listeners - private class EventRunner (using event: Update) extends Thread { - this setName s"evt-${event.updateId()}-nn" + private class EventRunner (using update: Update) extends Thread { + this setName s"upd-${update.updateId()}-nn" private def updateThreadName (t: String): Unit = - this setName s"evt-${event.updateId()}-$t" + this setName s"upd-${update.updateId()}-$t" override def run (): Unit = { - for (i <- listeners) { - object status: - var _status = 0 - def isOk: Boolean = _status > 0 - def check (u: Boolean): Unit = if u then _status = _status + 1 + given env: EventEnv = EventEnv(update) + boundary { for (i <- listeners) { try { updateThreadName("message") - if event.message ne null then status check i.onMessage + if update.message ne null then i.onMessage updateThreadName("edited-message") - if event.editedMessage ne null then status check i.onEditedMessage + if update.editedMessage ne null then i.onEditedMessage updateThreadName("channel-post") - if event.channelPost ne null then status check i.onChannelPost + if update.channelPost ne null then i.onChannelPost updateThreadName("edited-channel-post") - if event.editedChannelPost ne null then status check i.onEditedChannelPost + if update.editedChannelPost ne null then i.onEditedChannelPost updateThreadName("inline-query") - if event.inlineQuery ne null then status check i.onInlineQuery + if update.inlineQuery ne null then i.onInlineQuery updateThreadName("chosen-inline-result") - if event.chosenInlineResult ne null then status check i.onChosenInlineResult + if update.chosenInlineResult ne null then i.onChosenInlineResult updateThreadName("callback-query") - if event.callbackQuery ne null then status check i.onCallbackQuery + if update.callbackQuery ne null then i.onCallbackQuery updateThreadName("shipping-query") - if event.shippingQuery ne null then status check i.onShippingQuery + if update.shippingQuery ne null then i.onShippingQuery updateThreadName("pre-checkout-query") - if event.preCheckoutQuery ne null then status check i.onPreCheckoutQuery + if update.preCheckoutQuery ne null then i.onPreCheckoutQuery updateThreadName("poll") - if event.poll ne null then status check i.onPoll + if update.poll ne null then i.onPoll updateThreadName("poll-answer") - if event.pollAnswer ne null then status check i.onPollAnswer + if update.pollAnswer ne null then i.onPollAnswer updateThreadName("my-chat-member") - if event.myChatMember ne null then status check i.onMyChatMemberUpdated + if update.myChatMember ne null then i.onMyChatMemberUpdated updateThreadName("chat-member") - if event.chatMember ne null then status check i.onChatMemberUpdated + if update.chatMember ne null then i.onChatMemberUpdated updateThreadName("chat-join-request") - if event.chatJoinRequest ne null then status check i.onChatJoinRequest + if update.chatJoinRequest ne null then i.onChatJoinRequest } catch case e => { val errorMessage = StringBuilder() errorMessage ++= "Event throws unexpected exception:\n" @@ -77,8 +75,8 @@ class EventListenerManager (using coeur: MornyCoeur) extends UpdatesListener { logger error errorMessage.toString coeur.daemons.reporter.exception(e, "on event running") } - if (status isOk) return - } + if env.isEventOk then boundary.break() + }} } } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala index 38b8f95..e079dd0 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.event import cc.sukazyo.cono.morny.MornyCoeur -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.bot.query.{InlineQueryUnit, MornyQueries} import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec import com.pengrad.telegrambot.model.Update @@ -14,7 +14,8 @@ import scala.reflect.ClassTag class MornyOnInlineQuery (using queryManager: MornyQueries) (using coeur: MornyCoeur) extends EventListener { - override def onInlineQuery (using update: Update): Boolean = { + override def onInlineQuery (using event: EventEnv): Unit = { + import event.update val results: List[InlineQueryUnit[_]] = queryManager query update @@ -27,12 +28,13 @@ class MornyOnInlineQuery (using queryManager: MornyQueries) (using coeur: MornyC resultAnswers += r.result } - if (results isEmpty) return false + if (results isEmpty) return; coeur.account exec AnswerInlineQuery( update.inlineQuery.id, resultAnswers toArray:_* ).cacheTime(cacheTime).isPersonal(isPersonal) - true + + event.setEventOk } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnTelegramCommand.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnTelegramCommand.scala index 541070b..66c0f66 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnTelegramCommand.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnTelegramCommand.scala @@ -1,6 +1,6 @@ package cc.sukazyo.cono.morny.bot.event -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.Log.logger import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.bot.command.MornyCommands @@ -9,7 +9,8 @@ import com.pengrad.telegrambot.model.{Message, Update} class MornyOnTelegramCommand (using commandManager: MornyCommands) (using coeur: MornyCoeur) extends EventListener { - override def onMessage (using update: Update): Boolean = { + override def onMessage (using event: EventEnv): Unit = { + given update: Update = event.update def _isCommandMessage(message: Message): Boolean = if message.text eq null then false @@ -17,17 +18,19 @@ class MornyOnTelegramCommand (using commandManager: MornyCommands) (using coeur: else if message.text startsWith "/ " then false else true - if !_isCommandMessage(update.message) then return false + if !_isCommandMessage(update.message) then return val inputCommand = InputCommand(update.message.text drop 1) + event provide inputCommand + logger trace ":provided InputCommand for event" + if (!(inputCommand.command matches "^\\w+$")) logger debug "not command" - false else if ((inputCommand.target ne null) && (inputCommand.target != coeur.username)) logger debug "not morny command" - false else logger debug "is command" - commandManager.execute(using inputCommand) + if commandManager.execute(using inputCommand) then + event.setEventOk } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnUpdateTimestampOffsetLock.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnUpdateTimestampOffsetLock.scala index fc9f575..ae44432 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnUpdateTimestampOffsetLock.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnUpdateTimestampOffsetLock.scala @@ -1,17 +1,18 @@ package cc.sukazyo.cono.morny.bot.event -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.MornyCoeur import com.pengrad.telegrambot.model.Update class MornyOnUpdateTimestampOffsetLock (using coeur: MornyCoeur) extends EventListener { - private def isOutdated (timestamp: Int): Boolean = - coeur.config.eventIgnoreOutdated && (timestamp < (coeur.coeurStartTimestamp/1000)) + private def checkOutdated (timestamp: Int)(using event: EventEnv): Unit = + if coeur.config.eventIgnoreOutdated && (timestamp < (coeur.coeurStartTimestamp/1000)) then + event.setEventOk - override def onMessage (using update: Update): Boolean = isOutdated(update.message.date) - override def onEditedMessage (using update: Update): Boolean = isOutdated(update.editedMessage.date) - override def onChannelPost (using update: Update): Boolean = isOutdated(update.channelPost.date) - override def onEditedChannelPost (using update: Update): Boolean = isOutdated(update.editedChannelPost.date) + override def onMessage (using event: EventEnv): Unit = checkOutdated(event.update.message.date) + override def onEditedMessage (using event: EventEnv): Unit = checkOutdated(event.update.editedMessage.date) + override def onChannelPost (using event: EventEnv): Unit = checkOutdated(event.update.channelPost.date) + override def onEditedChannelPost (using event: EventEnv): Unit = checkOutdated(event.update.editedChannelPost.date) } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnCallMe.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnCallMe.scala index 244ee66..4f80a69 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnCallMe.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnCallMe.scala @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.event import cc.sukazyo.cono.morny.MornyCoeur -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.data.TelegramStickers import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramFormatter.* import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec @@ -15,10 +15,11 @@ class OnCallMe (using coeur: MornyCoeur) extends EventListener { private val me = coeur.config.trustedMaster - override def onMessage (using update: Update): Boolean = { + override def onMessage (using event: EventEnv): Unit = { + import event.update - if update.message.text == null then return false - if update.message.chat.`type` != (Chat.Type Private) then return false + if update.message.text == null then return; + if update.message.chat.`type` != (Chat.Type Private) then return //noinspection ScalaUnnecessaryParentheses val success = if me == -1 then false else @@ -32,7 +33,7 @@ class OnCallMe (using coeur: MornyCoeur) extends EventListener { case cc if cc startsWith "cc::" => requestCustom(update.message) case _ => - return false + return; if success then coeur.account exec SendSticker( @@ -45,7 +46,7 @@ class OnCallMe (using coeur: MornyCoeur) extends EventListener { TelegramStickers ID_501 ).replyToMessageId(update.message.messageId) - true + event.setEventOk } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.scala index db920c0..00a2ef8 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.scala @@ -1,6 +1,6 @@ package cc.sukazyo.cono.morny.bot.event -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.data.TelegramStickers import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec @@ -52,26 +52,28 @@ class OnCallMsgSend (using coeur: MornyCoeur) extends EventListener { case _ => null } - override def onMessage (using update: Update): Boolean = { + override def onMessage (using event: EventEnv): Unit = { + import event.update val message = update.message - if message.chat.`type` != Chat.Type.Private then return false - if message.text eq null then return false - if !(message.text startsWith "*msg") then return false + if message.chat.`type` != Chat.Type.Private then return; + if message.text eq null then return; + if !(message.text startsWith "*msg") then return; if (!(coeur.trusted isTrusted message.from.id)) coeur.account exec SendSticker( message.chat.id, TelegramStickers ID_403 ).replyToMessageId(message.messageId) - return true + event.setEventOk + return; if (message.text == "*msgsend") { - if (message.replyToMessage eq null) return answer404 + if (message.replyToMessage eq null) { answer404; return } val messageToSend = MessageToSend from message.replyToMessage - if ((messageToSend eq null) || (messageToSend.message eq null)) return answer404 + if ((messageToSend eq null) || (messageToSend.message eq null)) { answer404; return } val sendResponse = coeur.account execute messageToSend.toSendMessage() if (sendResponse isOk) { @@ -89,20 +91,21 @@ class OnCallMsgSend (using coeur: MornyCoeur) extends EventListener { ).replyToMessageId(update.message.messageId).parseMode(ParseMode HTML) } - return true + event.setEventOk + return } val messageToSend: MessageToSend = val raw: Message = if (message.text == "*msg") - if message.replyToMessage eq null then return answer404 + if message.replyToMessage eq null then { answer404; return } else message.replyToMessage else if (message.text startsWith "*msg") message - else return answer404 + else { answer404; return } val _toSend = MessageToSend from raw - if _toSend eq null then return answer404 + if _toSend eq null then { answer404; return } else _toSend val targetChatResponse = coeur.account execute GetChat(messageToSend.targetId) @@ -128,7 +131,7 @@ class OnCallMsgSend (using coeur: MornyCoeur) extends EventListener { ).parseMode(ParseMode HTML).replyToMessageId(update.message.messageId) } - if messageToSend.message eq null then return true + if messageToSend.message eq null then { answer404; return } val testSendResponse = coeur.account execute messageToSend.toSendMessage(update.message.chat.id).replyToMessageId(update.message.messageId) if (!(testSendResponse isOk)) @@ -140,15 +143,15 @@ class OnCallMsgSend (using coeur: MornyCoeur) extends EventListener { .stripMargin ).parseMode(ParseMode HTML).replyToMessageId(update.message.messageId) - true + event.setEventOk } - private def answer404 (using update: Update): Boolean = + private def answer404 (using event: EventEnv): Unit = coeur.account exec SendSticker( - update.message.chat.id, + event.update.message.chat.id, TelegramStickers ID_404 - ).replyToMessageId(update.message.messageId) - true + ).replyToMessageId(event.update.message.messageId) + event.setEventOk } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.scala index d6bc1f6..13f78ae 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.scala @@ -1,6 +1,6 @@ package cc.sukazyo.cono.morny.bot.event -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.Log.logger import cc.sukazyo.cono.morny.MornyCoeur import com.google.gson.GsonBuilder @@ -13,35 +13,38 @@ import scala.language.postfixOps class OnEventHackHandle (using coeur: MornyCoeur) extends EventListener { - import coeur.daemons.eventHack.trigger + private def trigger (chat_id: Long, from_id: Long)(using event: EventEnv): Unit = + given Update = event.update + if coeur.daemons.eventHack.trigger(chat_id, from_id) then + event.setEventOk - override def onMessage (using update: Update): Boolean = - trigger(update.message.chat.id, update.message.from.id) - override def onEditedMessage (using update: Update): Boolean = - trigger(update.editedMessage.chat.id, update.editedMessage.from.id) - override def onChannelPost (using update: Update): Boolean = - trigger(update.channelPost.chat.id, 0) - override def onEditedChannelPost (using update: Update): Boolean = - trigger(update.editedChannelPost.chat.id, 0) - override def onInlineQuery (using update: Update): Boolean = - trigger(0, update.inlineQuery.from.id) - override def onChosenInlineResult (using update: Update): Boolean = - trigger(0, update.chosenInlineResult.from.id) - override def onCallbackQuery (using update: Update): Boolean = - trigger(0, update.callbackQuery.from.id) - override def onShippingQuery (using update: Update): Boolean = - trigger(0, update.shippingQuery.from.id) - override def onPreCheckoutQuery (using update: Update): Boolean = - trigger(0, update.preCheckoutQuery.from.id) - override def onPoll (using update: Update): Boolean = + override def onMessage (using event: EventEnv): Unit = + trigger(event.update.message.chat.id, event.update.message.from.id) + override def onEditedMessage (using event: EventEnv): Unit = + trigger(event.update.editedMessage.chat.id, event.update.editedMessage.from.id) + override def onChannelPost (using event: EventEnv): Unit = + trigger(event.update.channelPost.chat.id, 0) + override def onEditedChannelPost (using event: EventEnv): Unit = + trigger(event.update.editedChannelPost.chat.id, 0) + override def onInlineQuery (using event: EventEnv): Unit = + trigger(0, event.update.inlineQuery.from.id) + override def onChosenInlineResult (using event: EventEnv): Unit = + trigger(0, event.update.chosenInlineResult.from.id) + override def onCallbackQuery (using event: EventEnv): Unit = + trigger(0, event.update.callbackQuery.from.id) + override def onShippingQuery (using event: EventEnv): Unit = + trigger(0, event.update.shippingQuery.from.id) + override def onPreCheckoutQuery (using event: EventEnv): Unit = + trigger(0, event.update.preCheckoutQuery.from.id) + override def onPoll (using event: EventEnv): Unit = trigger(0, 0) - override def onPollAnswer (using update: Update): Boolean = - trigger(0, update.pollAnswer.user.id) - override def onMyChatMemberUpdated (using update: Update): Boolean = - trigger(update.myChatMember.chat.id, update.myChatMember.from.id) - override def onChatMemberUpdated (using update: Update): Boolean = - trigger(update.chatMember.chat.id, update.chatMember.from.id) - override def onChatJoinRequest (using update: Update): Boolean = - trigger(update.chatJoinRequest.chat.id, update.chatJoinRequest.from.id) + override def onPollAnswer (using event: EventEnv): Unit = + trigger(0, event.update.pollAnswer.user.id) + override def onMyChatMemberUpdated (using event: EventEnv): Unit = + trigger(event.update.myChatMember.chat.id, event.update.myChatMember.from.id) + override def onChatMemberUpdated (using event: EventEnv): Unit = + trigger(event.update.chatMember.chat.id, event.update.chatMember.from.id) + override def onChatJoinRequest (using event: EventEnv): Unit = + trigger(event.update.chatJoinRequest.chat.id, event.update.chatJoinRequest.from.id) } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnMedicationNotifyApply.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnMedicationNotifyApply.scala index 25b29c1..ed24c3b 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnMedicationNotifyApply.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnMedicationNotifyApply.scala @@ -1,21 +1,21 @@ package cc.sukazyo.cono.morny.bot.event -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.daemon.{MedicationTimer, MornyDaemons} import com.pengrad.telegrambot.model.{Message, Update} class OnMedicationNotifyApply (using coeur: MornyCoeur) extends EventListener { - override def onEditedMessage (using event: Update): Boolean = - editedMessageProcess(event.editedMessage) - override def onEditedChannelPost (using event: Update): Boolean = - editedMessageProcess(event.editedChannelPost) + override def onEditedMessage (using event: EventEnv): Unit = + editedMessageProcess(event.update.editedMessage) + override def onEditedChannelPost (using event: EventEnv): Unit = + editedMessageProcess(event.update.editedChannelPost) - private def editedMessageProcess (edited: Message): Boolean = { - if edited.chat.id != coeur.config.medicationNotifyToChat then return false + private def editedMessageProcess (edited: Message)(using event: EventEnv): Unit = { + if edited.chat.id != coeur.config.medicationNotifyToChat then return; coeur.daemons.medicationTimer.refreshNotificationWrite(edited) - true + event.setEventOk } } 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 a56e42d..516499e 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 @@ -1,6 +1,6 @@ package cc.sukazyo.cono.morny.bot.event -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.bot.event.OnQuestionMarkReply.isAllMessageMark import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec @@ -12,19 +12,20 @@ import scala.util.boundary class OnQuestionMarkReply (using coeur: MornyCoeur) extends EventListener { - override def onMessage (using event: Update): Boolean = { + override def onMessage (using event: EventEnv): Unit = { + import event.update - if event.message.text eq null then return false + if update.message.text eq null then return import cc.sukazyo.cono.morny.util.UseMath.over import cc.sukazyo.cono.morny.util.UseRandom.chance_is - if (1 over 8) chance_is false then return false - if !isAllMessageMark(using event.message.text) then return false + if (1 over 8) chance_is false then return; + if !isAllMessageMark(using update.message.text) then return; coeur.account exec SendMessage( - event.message.chat.id, event.message.text - ).replyToMessageId(event.message.messageId) - true + update.message.chat.id, update.message.text + ).replyToMessageId(update.message.messageId) + event.setEventOk } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala index d1c97da..6667a15 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala @@ -1,23 +1,28 @@ package cc.sukazyo.cono.morny.bot.event -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.bot.command.MornyCommands import cc.sukazyo.cono.morny.util.tgapi.InputCommand +import cc.sukazyo.cono.morny.Log.logger import cc.sukazyo.cono.morny.MornyCoeur import com.pengrad.telegrambot.model.Update class OnUniMeowTrigger (using commands: MornyCommands) (using coeur: MornyCoeur) extends EventListener { - override def onMessage (using update: Update): Boolean = { + override def onMessage (using event: EventEnv): Unit = { - if update.message.text eq null then return false - var ok = false - for ((name, command) <- commands.commands_uni) - val _name = "/"+name - if (_name == update.message.text) - command.execute(using InputCommand(_name)) - ok = true - ok + event use classOf[InputCommand] consume { input => + logger trace s"got input command {$input} from event-context" + + for ((name, command_instance) <- commands.commands_uni) { + logger trace s"checking uni-meow $name" + if (name == input.command) + logger trace "checked" + command_instance.execute(using input, event.update) + event.setEventOk + } + + } onfail { logger trace "not command (for uni-meow)" } } 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 caa2dc8..10bd70a 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 @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.event import cc.sukazyo.cono.morny.MornyCoeur -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec import com.pengrad.telegrambot.model.Update import com.pengrad.telegrambot.request.SendMessage @@ -17,10 +17,11 @@ class OnUserRandom (using coeur: MornyCoeur) { private val USER_OR_QUERY = "^(.+)(?:还是|or)(.+)$" r private val USER_IF_QUERY = "^(.+)(?:吗\\?|?|\\?|吗?)$" r - override def onMessage (using update: Update): Boolean = { + override def onMessage (using event: EventEnv): Unit = { + import event.update - if update.message.text == null then return false - if !(update.message.text startsWith "/") then return false + if update.message.text == null then return; + if !(update.message.text startsWith "/") then return import cc.sukazyo.cono.morny.util.UseRandom.rand_half val query = update.message.text substring 1 @@ -29,17 +30,17 @@ class OnUserRandom (using coeur: MornyCoeur) { 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 OnQuestionMarkReply.isAllMessageMark(using _con) then return; (if rand_half then "不" else "") + _con case _ => null - //noinspection DuplicatedCode - if result == null then return false + if result == null then return; coeur.account exec SendMessage( - update.message.chat.id, result + update.message.chat.id, + result ).replyToMessageId(update.message.messageId) - true + event.setEventOk } @@ -51,23 +52,23 @@ class OnUserRandom (using coeur: MornyCoeur) { private val word_pattern = "^([\\w\\W]*)?(?:尊嘟假嘟|(?:O\\.o|o\\.O))$"r private val keywords = Array("尊嘟假嘟", "O.o", "o.O") - override def onMessage (using event: Update): Boolean = { + override def onMessage (using event: EventEnv): Unit = { + import event.update - if event.message.text == null then return false + if update.message.text == null then return var result: String|Null = null import cc.sukazyo.cono.morny.util.UseRandom.rand_half for (k <- keywords) - if event.message.text endsWith k then + if update.message.text endsWith k then result = if rand_half then "尊嘟" else "假嘟" - //noinspection DuplicatedCode - if result == null then return false + if result == null then return; coeur.account exec SendMessage( - event.message.chat.id, + update.message.chat.id, result - ).replyToMessageId(event.message.messageId) - true + ).replyToMessageId(update.message.messageId) + event.setEventOk } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.scala index eb3e119..7e42632 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.scala @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.event import cc.sukazyo.cono.morny.MornyCoeur -import cc.sukazyo.cono.morny.bot.api.EventListener +import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramFormatter.* import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramParseEscape.escapeHtml as h import cc.sukazyo.cono.morny.util.UniversalCommand @@ -16,10 +16,11 @@ class OnUserSlashAction (using coeur: MornyCoeur) extends EventListener { private val TG_FORMAT = "^\\w+(@\\w+)?$"r - override def onMessage (using update: Update): Boolean = { + override def onMessage (using event: EventEnv): Unit = { + import event.update val text = update.message.text - if text == null then return false + if text == null then return; if (text startsWith "/") { @@ -39,14 +40,14 @@ class OnUserSlashAction (using coeur: MornyCoeur) extends EventListener { actions(0) match // ignore Telegram command like case TG_FORMAT(_) => - return false + return; // ignore Path link - case x if x contains "/" => return false + case x if x contains "/" => return; case _ => val isHardParse = actions(0) isBlank def hp_len(i: Int) = if isHardParse then i+1 else i - if isHardParse && actions.length < 2 then return false + if isHardParse && actions.length < 2 then return val v_verb = actions(hp_len(0)) val hasObject = actions.length != hp_len(1) val v_object = @@ -70,9 +71,9 @@ class OnUserSlashAction (using coeur: MornyCoeur) extends EventListener { if hasObject then h(v_object+" ") else "" ) ).parseMode(ParseMode HTML).replyToMessageId(update.message.messageId) - true + event.setEventOk - } else false + } } diff --git a/src/test/scala/live/LiveMain.scala b/src/test/scala/live/LiveMain.scala index 2b7de09..f877dc3 100644 --- a/src/test/scala/live/LiveMain.scala +++ b/src/test/scala/live/LiveMain.scala @@ -1,9 +1,14 @@ package live +import cc.sukazyo.cono.morny.bot.api.EventEnv import cc.sukazyo.cono.morny.test.utils.BiliToolTest @main def LiveMain (args: String*): Unit = { - org.scalatest.run(BiliToolTest()) + val env: EventEnv = EventEnv(null) + + env provide "abcdefg" + + env use classOf[String] consume { (str: String) => println(str) } onfail { println("no str found in the env") } }