diff --git a/build.gradle.kts b/build.gradle.kts index d91f6eb..a5c2034 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,13 +1,13 @@ plugins { - val kotlinVersion = "1.6.10" + val kotlinVersion = "1.7.0" kotlin("jvm") version kotlinVersion kotlin("plugin.serialization") version kotlinVersion - id("net.mamoe.mirai-console") version "2.11.1" + id("net.mamoe.mirai-console") version "2.12.0" } group = "top.jie65535" -version = "0.1.0" +version = "0.1.1" repositories { maven("https://maven.aliyun.com/repository/public") // 阿里云国内代理仓库 diff --git a/src/main/kotlin/PluginCommand.kt b/src/main/kotlin/PluginCommand.kt new file mode 100644 index 0000000..f97fe33 --- /dev/null +++ b/src/main/kotlin/PluginCommand.kt @@ -0,0 +1,30 @@ +package top.jie65535.j24 + +import net.mamoe.mirai.console.command.CommandSender +import net.mamoe.mirai.console.command.CompositeCommand +import net.mamoe.mirai.console.command.getGroupOrNull +import net.mamoe.mirai.contact.Group + +object PluginCommand : CompositeCommand( + PluginMain, "j24" +) { + @SubCommand + suspend fun CommandSender.enable(group: Group? = getGroupOrNull()) { + if (group == null) { + sendMessage("必须指定群") + return + } + PluginConfig.enabledGroups.add(group.id) + sendMessage("OK") + } + + @SubCommand + suspend fun CommandSender.disable(group: Group? = getGroupOrNull()) { + if (group == null) { + sendMessage("必须指定群") + return + } + PluginConfig.enabledGroups.remove(group.id) + sendMessage("OK") + } +} \ No newline at end of file diff --git a/src/main/kotlin/PluginConfig.kt b/src/main/kotlin/PluginConfig.kt new file mode 100644 index 0000000..4c2033a --- /dev/null +++ b/src/main/kotlin/PluginConfig.kt @@ -0,0 +1,8 @@ +package top.jie65535.j24 + +import net.mamoe.mirai.console.data.AutoSavePluginConfig +import net.mamoe.mirai.console.data.value + +object PluginConfig : AutoSavePluginConfig("config") { + val enabledGroups: MutableSet by value() +} \ No newline at end of file diff --git a/src/main/kotlin/PluginMain.kt b/src/main/kotlin/PluginMain.kt index dc19093..af22c24 100644 --- a/src/main/kotlin/PluginMain.kt +++ b/src/main/kotlin/PluginMain.kt @@ -1,16 +1,17 @@ package top.jie65535.j24 +import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin import net.mamoe.mirai.event.GlobalEventChannel -import net.mamoe.mirai.event.subscribeMessages +import net.mamoe.mirai.event.subscribeGroupMessages import net.mamoe.mirai.utils.info object PluginMain : KotlinPlugin( JvmPluginDescription( id = "top.jie65535.j24", name = "J 24点游戏", - version = "0.1.0" + version = "0.1.1" ) { author("jie65535") info("24点游戏") @@ -20,15 +21,19 @@ object PluginMain : KotlinPlugin( override fun onEnable() { logger.info { "Plugin loaded" } + PluginCommand.register() + PluginConfig.reload() + val eventChannel = GlobalEventChannel.parentScope(this) - eventChannel.subscribeMessages { - startsWith("24点") quoteReply { + eventChannel.subscribeGroupMessages { + startsWith("24点") and content { PluginConfig.enabledGroups.contains(this.group.id) } quoteReply { val game = Point24() games[this.sender.id] = game "你抽到了 [${game.points[0]}] [${game.points[1]}] [${game.points[2]}] [${game.points[3]}]\n" + "请用以上四组数字组合成结果为24的算式,以“答”开头验证" } - startsWith("答") quoteReply { + + startsWith("答") and content { PluginConfig.enabledGroups.contains(this.group.id) } quoteReply { val game = games[sender.id] if (game == null) { "你还没有抽数字哦,说“24点”来开始游戏吧" @@ -42,6 +47,7 @@ object PluginMain : KotlinPlugin( "答错了,计算结果为 $result" } } catch (e: Throwable) { +// logger.error(e) "错误:${e.message}" } } diff --git a/src/main/kotlin/Point24.kt b/src/main/kotlin/Point24.kt index 669fe9d..40837e8 100644 --- a/src/main/kotlin/Point24.kt +++ b/src/main/kotlin/Point24.kt @@ -25,7 +25,12 @@ class Point24 { return (args[0].toInt() and args[1].toInt()).toDouble() } }, - object : Operator("|", 2, true, Operator.PRECEDENCE_ADDITION - 3) { + object : Operator("^", 2, true, Operator.PRECEDENCE_ADDITION - 3) { + override fun apply(vararg args: Double): Double { + return (args[0].toInt() xor args[1].toInt()).toDouble() + } + }, + object : Operator("|", 2, true, Operator.PRECEDENCE_ADDITION - 4) { override fun apply(vararg args: Double): Double { return (args[0].toInt() or args[1].toInt()).toDouble() }