command API change

This commit is contained in:
jiahua.liu 2020-03-22 21:21:52 +08:00
parent 90fc27d612
commit 1171267ecc
4 changed files with 59 additions and 24 deletions

View File

@ -48,13 +48,25 @@ interface Command {
/** /**
* 注册这个指令 * 注册这个指令
*/ */
inline fun Command.register() = CommandManager.register(this) inline fun Command.register(commandOwner: CommandOwner) = CommandManager.register(commandOwner,this)
/** /**
* 构造并注册一个指令 * 构造并注册一个指令
*/ */
@Deprecated("this will be removed in next few release")
object AnonymousCommandOwner:CommandOwner
@Deprecated("this will become internal in next few release, please use PluginBase.registerCommand() for plugin")
inline fun registerCommand(builder: CommandBuilder.() -> Unit): Command { inline fun registerCommand(builder: CommandBuilder.() -> Unit): Command {
return CommandBuilder().apply(builder).register() return CommandBuilder().apply(builder).register(AnonymousCommandOwner)
}
internal inline fun registerConsoleCommands(builder: CommandBuilder.() -> Unit):Command{
return CommandBuilder().apply(builder).register(ConsoleCommandOwner)
}
inline fun PluginBase.registerCommand(builder: CommandBuilder.() -> Unit):Command{
return CommandBuilder().apply(builder).register(this.asCommandOwner())
} }
@ -113,7 +125,7 @@ internal class AnonymousCommand internal constructor(
} }
@PublishedApi @PublishedApi
internal fun CommandBuilder.register(): AnonymousCommand { internal fun CommandBuilder.register(commandOwner: CommandOwner): AnonymousCommand {
if (name == null || onCommand == null) { if (name == null || onCommand == null) {
error("CommandBuilder not complete") error("CommandBuilder not complete")
} }
@ -126,5 +138,5 @@ internal fun CommandBuilder.register(): AnonymousCommand {
description, description,
usage, usage,
onCommand!! onCommand!!
).also { it.register() } ).also { it.register(commandOwner) }
} }

View File

@ -18,6 +18,13 @@ import net.mamoe.mirai.console.plugins.PluginBase
import net.mamoe.mirai.console.plugins.PluginManager import net.mamoe.mirai.console.plugins.PluginManager
import java.util.concurrent.Executors import java.util.concurrent.Executors
interface CommandOwner
class PluginCommandOwner(val pluginBase: PluginBase):CommandOwner
internal object ConsoleCommandOwner:CommandOwner
fun PluginBase.asCommandOwner() = PluginCommandOwner(this)
object CommandManager : Job by { object CommandManager : Job by {
GlobalScope.launch(start = CoroutineStart.LAZY) { GlobalScope.launch(start = CoroutineStart.LAZY) {
processCommandQueue() processCommandQueue()
@ -25,10 +32,24 @@ object CommandManager : Job by {
}() { }() {
private val registeredCommand: MutableMap<String, Command> = mutableMapOf() private val registeredCommand: MutableMap<String, Command> = mutableMapOf()
val commands: Collection<Command> get() = registeredCommand.values val commands: Collection<Command> get() = registeredCommand.values
private val pluginCommands:MutableMap<PluginBase,MutableCollection<Command>> = mutableMapOf()
fun reload(){ internal fun clearPluginsCommands(){
registeredCommand.clear() pluginCommands.values.forEach {a ->
DefaultCommands() a.forEach{
unregister(it)
}
}
pluginCommands.clear()
}
internal fun clearPluginCommands(
pluginBase: PluginBase
){
pluginCommands[pluginBase]?.run {
this.forEach { unregister(it) }
this.clear()
}
} }
/** /**
@ -36,7 +57,7 @@ object CommandManager : Job by {
* *
* @throws IllegalStateException 当已注册的指令与 [command] 重名时 * @throws IllegalStateException 当已注册的指令与 [command] 重名时
*/ */
fun register(command: Command) { fun register(commandOwner: CommandOwner, command: Command) {
val allNames = mutableListOf(command.name).also { it.addAll(command.alias) } val allNames = mutableListOf(command.name).also { it.addAll(command.alias) }
allNames.forEach { allNames.forEach {
if (registeredCommand.containsKey(it)) { if (registeredCommand.containsKey(it)) {
@ -46,6 +67,10 @@ object CommandManager : Job by {
allNames.forEach { allNames.forEach {
registeredCommand[it] = command registeredCommand[it] = command
} }
if(commandOwner is PluginCommandOwner){
pluginCommands.putIfAbsent(commandOwner.pluginBase, mutableSetOf())
pluginCommands[commandOwner.pluginBase]!!.add(command)
}
} }
fun unregister(command: Command) { fun unregister(command: Command) {

View File

@ -30,7 +30,7 @@ import java.util.*
object DefaultCommands { object DefaultCommands {
operator fun invoke() { operator fun invoke() {
registerCommand { registerConsoleCommands {
name = "manager" name = "manager"
description = "Add a manager" description = "Add a manager"
onCommand { it -> onCommand { it ->
@ -98,7 +98,7 @@ object DefaultCommands {
} }
} }
registerCommand { registerConsoleCommands{
name = "login" name = "login"
description = "机器人登录" description = "机器人登录"
onCommand { onCommand {
@ -158,7 +158,7 @@ object DefaultCommands {
} }
} }
registerCommand { registerConsoleCommands {
name = "status" name = "status"
description = "获取状态" description = "获取状态"
onCommand { args -> onCommand { args ->
@ -188,7 +188,7 @@ object DefaultCommands {
} }
registerCommand { registerConsoleCommands {
name = "say" name = "say"
description = "聊天功能演示" description = "聊天功能演示"
onCommand { onCommand {
@ -225,7 +225,7 @@ object DefaultCommands {
} }
registerCommand { registerConsoleCommands {
name = "plugins" name = "plugins"
alias = listOf("plugin") alias = listOf("plugin")
description = "获取插件列表" description = "获取插件列表"
@ -240,7 +240,7 @@ object DefaultCommands {
} }
} }
registerCommand { registerConsoleCommands {
name = "command" name = "command"
alias = listOf("commands", "help", "helps") alias = listOf("commands", "help", "helps")
description = "获取指令列表" description = "获取指令列表"
@ -258,7 +258,7 @@ object DefaultCommands {
} }
} }
registerCommand { registerConsoleCommands {
name = "about" name = "about"
description = "About Mirai-Console" description = "About Mirai-Console"
onCommand { onCommand {
@ -271,7 +271,7 @@ object DefaultCommands {
} }
} }
registerCommand { registerConsoleCommands {
name = "reload" name = "reload"
alias = listOf("reloadPlugins") alias = listOf("reloadPlugins")
description = "重新加载全部插件" description = "重新加载全部插件"

View File

@ -16,7 +16,6 @@ import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.command.Command import net.mamoe.mirai.console.command.Command
import net.mamoe.mirai.console.command.CommandManager import net.mamoe.mirai.console.command.CommandManager
import net.mamoe.mirai.console.command.CommandSender import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.DefaultCommands
import net.mamoe.mirai.utils.SimpleLogger import net.mamoe.mirai.utils.SimpleLogger
import net.mamoe.mirai.utils.io.encodeToString import net.mamoe.mirai.utils.io.encodeToString
import java.io.File import java.io.File
@ -212,6 +211,7 @@ object PluginManager {
} catch (ignored: Throwable) { } catch (ignored: Throwable) {
logger.info(ignored) logger.info(ignored)
logger.info(it.pluginName + " failed to load, disabling it") logger.info(it.pluginName + " failed to load, disabling it")
logger.info(it.pluginName + " 推荐立即删除/替换并重启")
if (ignored is CancellationException) { if (ignored is CancellationException) {
disablePlugin(it,ignored) disablePlugin(it,ignored)
}else{ }else{
@ -226,6 +226,7 @@ object PluginManager {
} catch (ignored: Throwable) { } catch (ignored: Throwable) {
logger.info(ignored) logger.info(ignored)
logger.info(it.pluginName + " failed to enable, disabling it") logger.info(it.pluginName + " failed to enable, disabling it")
logger.info(it.pluginName + " 推荐立即删除/替换并重启")
if (ignored is CancellationException) { if (ignored is CancellationException) {
disablePlugin(it,ignored) disablePlugin(it,ignored)
}else{ }else{
@ -237,28 +238,25 @@ object PluginManager {
logger.info("""加载了${nameToPluginBaseMap.size}个插件""") logger.info("""加载了${nameToPluginBaseMap.size}个插件""")
} }
private fun disablePlugin(
/**
* 请注意 这个方法不会移除该指令已注册的指令
*/
fun disablePlugin(
plugin:PluginBase, plugin:PluginBase,
exception: CancellationException? = null exception: CancellationException? = null
){ ){
CommandManager.clearPluginCommands(plugin)
plugin.disable(exception)
nameToPluginBaseMap.remove(plugin.pluginName) nameToPluginBaseMap.remove(plugin.pluginName)
pluginDescriptions.remove(plugin.pluginName) pluginDescriptions.remove(plugin.pluginName)
plugin.disable(exception)
} }
@JvmOverloads @JvmOverloads
fun disablePlugins(throwable: CancellationException? = null) { fun disablePlugins(throwable: CancellationException? = null) {
CommandManager.clearPluginsCommands()
nameToPluginBaseMap.values.forEach { nameToPluginBaseMap.values.forEach {
it.disable(throwable) it.disable(throwable)
} }
nameToPluginBaseMap.clear() nameToPluginBaseMap.clear()
pluginDescriptions.clear() pluginDescriptions.clear()
CommandManager.reload()
} }