From 083e5d1b2fcaad6c76293cb62201d67ad34a6d8c Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 23 Dec 2023 17:00:54 +0800 Subject: [PATCH] uni_meow modularize --- project/MornyConfiguration.scala | 2 +- .../cc/sukazyo/cono/morny/MornyCoeur.scala | 31 +++++++++++++ .../sukazyo/cono/morny/MornyCoreModule.scala | 17 +------ .../cc/sukazyo/cono/morny/ServerMain.scala | 2 +- .../bot/command/MornyCommandManager.scala | 5 ++- .../BotEventUniMeowTrigger.scala} | 7 ++- .../cono/morny/uni_meow/ModuleUniMeow.scala | 45 +++++++++++++++++++ .../uni_meow/UniMeowCommandManager.scala | 17 +++++++ .../{bot/command => uni_meow}/喵呜.scala | 3 +- .../{bot/command => uni_meow}/私わね.scala | 3 +- 10 files changed, 107 insertions(+), 25 deletions(-) rename src/main/scala/cc/sukazyo/cono/morny/{bot/event/OnUniMeowTrigger.scala => uni_meow/BotEventUniMeowTrigger.scala} (70%) create mode 100644 src/main/scala/cc/sukazyo/cono/morny/uni_meow/ModuleUniMeow.scala create mode 100644 src/main/scala/cc/sukazyo/cono/morny/uni_meow/UniMeowCommandManager.scala rename src/main/scala/cc/sukazyo/cono/morny/{bot/command => uni_meow}/喵呜.scala (94%) rename src/main/scala/cc/sukazyo/cono/morny/{bot/command => uni_meow}/私わね.scala (87%) diff --git a/project/MornyConfiguration.scala b/project/MornyConfiguration.scala index 041d5f6..98ea059 100644 --- a/project/MornyConfiguration.scala +++ b/project/MornyConfiguration.scala @@ -8,7 +8,7 @@ object MornyConfiguration { val MORNY_CODE_STORE = "https://github.com/Eyre-S/Coeur-Morny-Cono" val MORNY_COMMIT_PATH = "https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s" - val VERSION = "2.0.0-alpha6" + val VERSION = "2.0.0-alpha7" val VERSION_DELTA: Option[String] = None val CODENAME = "guanggu" diff --git a/src/main/scala/cc/sukazyo/cono/morny/MornyCoeur.scala b/src/main/scala/cc/sukazyo/cono/morny/MornyCoeur.scala index 9d29c51..00d99dc 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/MornyCoeur.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/MornyCoeur.scala @@ -21,6 +21,12 @@ object MornyCoeur { val THREAD_SERVER_EXIT = "system-exit" + /** A tag that shows current [[MornyCoeur!]] is running under + * test mode. + * + * @see [[MornyCoeur.testRun]] for more introduction for test mode. + * @since 2.0.0 + */ object TestRun case class OnInitializingPreContext ( @@ -72,6 +78,31 @@ object MornyCoeur { } +/** The Coeur (Main Class) of Morny. + * + * ## Lifecycle + * + * todo... + * + * ## Test Mode + * + * Coeur supports a special launch mode called test mode.When running + * under test mode, this instance will only run till the initialize + * is complete and exit immediately. In the lifecycle, it is run till + * [[MornyModule.onInitializingPost]] event. + * + * In this way, it will be available to see if the initialize result is + * under expected while not really start the service (aka. will not + * take side effect). + * + * This mode can be enabled by the parameter [[testRun]]. When exited + * under this mode, the [[exitReason]] will be a special value [[TestRun]]. + * + * @param modules all morny modules. + * @param config the immutable [[MornyConfig]] instance, will use all + * over this Morny (Coeur) instance. + * @param testRun if the instance should run under test mode. + */ class MornyCoeur (modules: List[MornyModule])(using val config: MornyConfig)(testRun: Boolean = false) { given MornyCoeur = this diff --git a/src/main/scala/cc/sukazyo/cono/morny/MornyCoreModule.scala b/src/main/scala/cc/sukazyo/cono/morny/MornyCoreModule.scala index 69833ac..0f7d7cc 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/MornyCoreModule.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/MornyCoreModule.scala @@ -25,7 +25,6 @@ class MornyCoreModule extends MornyModule { eventManager.register( // ACTIVITY_RECORDER // KUOHUANHUAN_NEED_SLEEP - OnUniMeowTrigger(using commandManager), $OnUserRandom.RandomSelect, //noinspection NonAsciiCharacters $OnUserRandom.尊嘟假嘟, @@ -44,8 +43,6 @@ class MornyCoreModule extends MornyModule { val $MornyInformationOlds = MornyInformationOlds(using $MornyInformation) val $MornyManagers = MornyManagers() //noinspection NonAsciiCharacters - val $喵呜 = 喵呜() - //noinspection NonAsciiCharacters val $创 = 创() commandManager.register( @@ -69,21 +66,9 @@ class MornyCoreModule extends MornyModule { Testing(), DirectMsgClear(), - - //noinspection NonAsciiCharacters - 私わね(), - //noinspection NonAsciiCharacters - $喵呜.Progynova, //noinspection NonAsciiCharacters $创.Chuang - - ) - //noinspection NonAsciiCharacters - commandManager.registerForUni( - $喵呜.抱抱, - $喵呜.揉揉, - $喵呜.贴贴, - $喵呜.蹭蹭 + ) queryManager.register( diff --git a/src/main/scala/cc/sukazyo/cono/morny/ServerMain.scala b/src/main/scala/cc/sukazyo/cono/morny/ServerMain.scala index d1ec249..383277b 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/ServerMain.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/ServerMain.scala @@ -159,7 +159,7 @@ object ServerMain { try MornyCoeur( - MornyCoreModule() :: Nil + ServerModulesLoader.load() )(using config build)( testRun = mode_testRun ) diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyCommandManager.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyCommandManager.scala index 78a3f81..77248c0 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyCommandManager.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/command/MornyCommandManager.scala @@ -3,6 +3,7 @@ package cc.sukazyo.cono.morny.bot.command import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.data.TelegramStickers import cc.sukazyo.cono.morny.Log.logger +import cc.sukazyo.cono.morny.bot.command.MornyCommandManager.CommandMap import cc.sukazyo.cono.morny.util.tgapi.InputCommand import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec import com.pengrad.telegrambot.model.{BotCommand, DeleteMyCommands, Update} @@ -12,9 +13,11 @@ import scala.collection.mutable import scala.collection.mutable.ArrayBuffer import scala.language.postfixOps +object MornyCommandManager: + type CommandMap = mutable.SeqMap[String, ISimpleCommand] + class MornyCommandManager (using coeur: MornyCoeur) { - private type CommandMap = mutable.SeqMap[String, ISimpleCommand] private val commands: CommandMap = mutable.SeqMap.empty def register [T <: ISimpleCommand] (commands: T*): Unit = for (i <- commands) diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala b/src/main/scala/cc/sukazyo/cono/morny/uni_meow/BotEventUniMeowTrigger.scala similarity index 70% rename from src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala rename to src/main/scala/cc/sukazyo/cono/morny/uni_meow/BotEventUniMeowTrigger.scala index 45fe637..755d636 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnUniMeowTrigger.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/uni_meow/BotEventUniMeowTrigger.scala @@ -1,11 +1,10 @@ -package cc.sukazyo.cono.morny.bot.event +package cc.sukazyo.cono.morny.uni_meow import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} -import cc.sukazyo.cono.morny.bot.command.MornyCommandManager import cc.sukazyo.cono.morny.util.tgapi.InputCommand import cc.sukazyo.cono.morny.Log.logger -class OnUniMeowTrigger (using commands: MornyCommandManager) extends EventListener { +class BotEventUniMeowTrigger (using commands: UniMeowCommandManager) extends EventListener { override def onMessage (using event: EventEnv): Unit = { import event.* @@ -13,7 +12,7 @@ class OnUniMeowTrigger (using commands: MornyCommandManager) extends EventListen givenCxt >> { (input: InputCommand) => logger trace s"got input command {$input} from event-context" - for ((name, command_instance) <- commands.commands_uni) { + for ((name, command_instance) <- commands.commands) { logger trace s"checking uni-meow $name" if (name == input.command) logger trace "checked" diff --git a/src/main/scala/cc/sukazyo/cono/morny/uni_meow/ModuleUniMeow.scala b/src/main/scala/cc/sukazyo/cono/morny/uni_meow/ModuleUniMeow.scala new file mode 100644 index 0000000..f5afb11 --- /dev/null +++ b/src/main/scala/cc/sukazyo/cono/morny/uni_meow/ModuleUniMeow.scala @@ -0,0 +1,45 @@ +package cc.sukazyo.cono.morny.uni_meow + +import cc.sukazyo.cono.morny.{MornyCoeur, MornyModule, MornySystem} + +class ModuleUniMeow extends MornyModule { + + override val id: String = "coeur.uni_meow" + override val name: String = "Coeur Uni-Meow Commands" + override val version: String = MornySystem.VERSION + override val description: String | Null = + // language=Markdown + """Provides support for unicode command in Telegram. Also provided some + |interesting commands. + | + |Requires *Group Privacy* set to `disabled` on the bot account.""".stripMargin + + val uni_commands: UniMeowCommandManager = UniMeowCommandManager() + + override def onInitializing (using MornyCoeur)(cxt: MornyCoeur.OnInitializingContext): Unit = { + import cxt.* + + externalContext << uni_commands + givenCxt << uni_commands + eventManager.register( + BotEventUniMeowTrigger(using uni_commands) + ) + + //noinspection NonAsciiCharacters + val $喵呜 = 喵呜() + //noinspection NonAsciiCharacters + uni_commands.register( + $喵呜.抱抱, + $喵呜.揉揉, + $喵呜.贴贴, + $喵呜.蹭蹭 + ) + //noinspection NonAsciiCharacters + commandManager.register( + $喵呜.Progynova, + 私わね() + ) + + } + +} diff --git a/src/main/scala/cc/sukazyo/cono/morny/uni_meow/UniMeowCommandManager.scala b/src/main/scala/cc/sukazyo/cono/morny/uni_meow/UniMeowCommandManager.scala new file mode 100644 index 0000000..95c23af --- /dev/null +++ b/src/main/scala/cc/sukazyo/cono/morny/uni_meow/UniMeowCommandManager.scala @@ -0,0 +1,17 @@ +package cc.sukazyo.cono.morny.uni_meow + +import cc.sukazyo.cono.morny.bot.command.ISimpleCommand +import cc.sukazyo.cono.morny.bot.command.MornyCommandManager.CommandMap + +import scala.collection.mutable + +class UniMeowCommandManager { + + private[uni_meow] val commands: CommandMap = mutable.SeqMap.empty + def register [T <: ISimpleCommand] (commands: T*): Unit = + for (i <- commands) + this.commands += (i.name -> i) + for (alias <- i.aliases) + this.commands += (alias.name -> i) + +} diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/command/喵呜.scala b/src/main/scala/cc/sukazyo/cono/morny/uni_meow/喵呜.scala similarity index 94% rename from src/main/scala/cc/sukazyo/cono/morny/bot/command/喵呜.scala rename to src/main/scala/cc/sukazyo/cono/morny/uni_meow/喵呜.scala index 9c877f7..9a24de5 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/command/喵呜.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/uni_meow/喵呜.scala @@ -1,6 +1,7 @@ -package cc.sukazyo.cono.morny.bot.command +package cc.sukazyo.cono.morny.uni_meow import cc.sukazyo.cono.morny.MornyCoeur +import cc.sukazyo.cono.morny.bot.command.{ICommandAlias, ISimpleCommand, ITelegramCommand} import cc.sukazyo.cono.morny.data.TelegramStickers import cc.sukazyo.cono.morny.util.tgapi.InputCommand import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/command/私わね.scala b/src/main/scala/cc/sukazyo/cono/morny/uni_meow/私わね.scala similarity index 87% rename from src/main/scala/cc/sukazyo/cono/morny/bot/command/私わね.scala rename to src/main/scala/cc/sukazyo/cono/morny/uni_meow/私わね.scala index 52cd803..0078b83 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/command/私わね.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/uni_meow/私わね.scala @@ -1,6 +1,7 @@ -package cc.sukazyo.cono.morny.bot.command +package cc.sukazyo.cono.morny.uni_meow import cc.sukazyo.cono.morny.MornyCoeur +import cc.sukazyo.cono.morny.bot.command.{ICommandAlias, ISimpleCommand} import cc.sukazyo.cono.morny.util.tgapi.InputCommand import cc.sukazyo.cono.morny.util.UseMath.over import cc.sukazyo.cono.morny.util.UseRandom.*