From 1a5355db89630a88a79d7367a4b2dc376fab9889 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 9 Nov 2020 09:53:47 +0800 Subject: [PATCH] CommandConfig, support custom commandPrefix, close #131 --- .../MiraiConsoleImplementationBridge.kt | 2 ++ .../internal/command/CommandManagerImpl.kt | 2 +- .../src/internal/command/CommnadConfig.kt | 30 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 backend/mirai-console/src/internal/command/CommnadConfig.kt diff --git a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt index 7ce79204e..11e148b50 100644 --- a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt @@ -28,6 +28,7 @@ import net.mamoe.mirai.console.data.PluginDataStorage import net.mamoe.mirai.console.extensions.PermissionServiceProvider import net.mamoe.mirai.console.extensions.PostStartupExtension import net.mamoe.mirai.console.extensions.SingletonExtensionSelector +import net.mamoe.mirai.console.internal.command.CommandConfig import net.mamoe.mirai.console.internal.command.CommandManagerImpl import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig import net.mamoe.mirai.console.internal.data.builtins.ConsoleDataScope @@ -139,6 +140,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI phase `load configurations`@{ mainLogger.verbose { "Loading configurations..." } + ConsoleDataScope.addAndReloadConfig(CommandConfig) ConsoleDataScope.reloadAll() } diff --git a/backend/mirai-console/src/internal/command/CommandManagerImpl.kt b/backend/mirai-console/src/internal/command/CommandManagerImpl.kt index 82e472477..9b6f3ecd5 100644 --- a/backend/mirai-console/src/internal/command/CommandManagerImpl.kt +++ b/backend/mirai-console/src/internal/command/CommandManagerImpl.kt @@ -103,7 +103,7 @@ internal object CommandManagerImpl : CommandManager, CoroutineScope by MiraiCons override fun getRegisteredCommands(owner: CommandOwner): List = _registeredCommands.filter { it.owner == owner } override val allRegisteredCommands: List get() = _registeredCommands.toList() // copy - override val commandPrefix: String get() = "/" + override val commandPrefix: String get() = CommandConfig.commandPrefix override fun unregisterAllCommands(owner: CommandOwner) { for (registeredCommand in getRegisteredCommands(owner)) { unregisterCommand(registeredCommand) diff --git a/backend/mirai-console/src/internal/command/CommnadConfig.kt b/backend/mirai-console/src/internal/command/CommnadConfig.kt new file mode 100644 index 000000000..6e0a571d6 --- /dev/null +++ b/backend/mirai-console/src/internal/command/CommnadConfig.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.internal.command + +import net.mamoe.mirai.console.data.AutoSavePluginConfig +import net.mamoe.mirai.console.data.ValueDescription +import net.mamoe.mirai.console.data.ValueName +import net.mamoe.mirai.console.data.value + +@ValueDescription(""" + 内置指令系统配置 +""") +internal object CommandConfig : AutoSavePluginConfig("Command") { + override fun shouldPerformAutoSaveWheneverChanged(): Boolean { + return false + } + + @ValueDescription(""" + 指令前缀, 默认 "/" + """) + @ValueName("commandPrefix") + val commandPrefix: String by value("/") +} \ No newline at end of file