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 {
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
internal fun CommandBuilder.register(): AnonymousCommand {
internal fun CommandBuilder.register(commandOwner: CommandOwner): AnonymousCommand {
if (name == null || onCommand == null) {
error("CommandBuilder not complete")
}
@ -126,5 +138,5 @@ internal fun CommandBuilder.register(): AnonymousCommand {
description,
usage,
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 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 {
GlobalScope.launch(start = CoroutineStart.LAZY) {
processCommandQueue()
@ -25,10 +32,24 @@ object CommandManager : Job by {
}() {
private val registeredCommand: MutableMap<String, Command> = mutableMapOf()
val commands: Collection<Command> get() = registeredCommand.values
private val pluginCommands:MutableMap<PluginBase,MutableCollection<Command>> = mutableMapOf()
fun reload(){
registeredCommand.clear()
DefaultCommands()
internal fun clearPluginsCommands(){
pluginCommands.values.forEach {a ->
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] 重名时
*/
fun register(command: Command) {
fun register(commandOwner: CommandOwner, command: Command) {
val allNames = mutableListOf(command.name).also { it.addAll(command.alias) }
allNames.forEach {
if (registeredCommand.containsKey(it)) {
@ -46,6 +67,10 @@ object CommandManager : Job by {
allNames.forEach {
registeredCommand[it] = command
}
if(commandOwner is PluginCommandOwner){
pluginCommands.putIfAbsent(commandOwner.pluginBase, mutableSetOf())
pluginCommands[commandOwner.pluginBase]!!.add(command)
}
}
fun unregister(command: Command) {

View File

@ -30,7 +30,7 @@ import java.util.*
object DefaultCommands {
operator fun invoke() {
registerCommand {
registerConsoleCommands {
name = "manager"
description = "Add a manager"
onCommand { it ->
@ -98,7 +98,7 @@ object DefaultCommands {
}
}
registerCommand {
registerConsoleCommands{
name = "login"
description = "机器人登录"
onCommand {
@ -158,7 +158,7 @@ object DefaultCommands {
}
}
registerCommand {
registerConsoleCommands {
name = "status"
description = "获取状态"
onCommand { args ->
@ -188,7 +188,7 @@ object DefaultCommands {
}
registerCommand {
registerConsoleCommands {
name = "say"
description = "聊天功能演示"
onCommand {
@ -225,7 +225,7 @@ object DefaultCommands {
}
registerCommand {
registerConsoleCommands {
name = "plugins"
alias = listOf("plugin")
description = "获取插件列表"
@ -240,7 +240,7 @@ object DefaultCommands {
}
}
registerCommand {
registerConsoleCommands {
name = "command"
alias = listOf("commands", "help", "helps")
description = "获取指令列表"
@ -258,7 +258,7 @@ object DefaultCommands {
}
}
registerCommand {
registerConsoleCommands {
name = "about"
description = "About Mirai-Console"
onCommand {
@ -271,7 +271,7 @@ object DefaultCommands {
}
}
registerCommand {
registerConsoleCommands {
name = "reload"
alias = listOf("reloadPlugins")
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.CommandManager
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.io.encodeToString
import java.io.File
@ -212,6 +211,7 @@ object PluginManager {
} catch (ignored: Throwable) {
logger.info(ignored)
logger.info(it.pluginName + " failed to load, disabling it")
logger.info(it.pluginName + " 推荐立即删除/替换并重启")
if (ignored is CancellationException) {
disablePlugin(it,ignored)
}else{
@ -226,6 +226,7 @@ object PluginManager {
} catch (ignored: Throwable) {
logger.info(ignored)
logger.info(it.pluginName + " failed to enable, disabling it")
logger.info(it.pluginName + " 推荐立即删除/替换并重启")
if (ignored is CancellationException) {
disablePlugin(it,ignored)
}else{
@ -237,28 +238,25 @@ object PluginManager {
logger.info("""加载了${nameToPluginBaseMap.size}个插件""")
}
/**
* 请注意 这个方法不会移除该指令已注册的指令
*/
fun disablePlugin(
private fun disablePlugin(
plugin:PluginBase,
exception: CancellationException? = null
){
CommandManager.clearPluginCommands(plugin)
plugin.disable(exception)
nameToPluginBaseMap.remove(plugin.pluginName)
pluginDescriptions.remove(plugin.pluginName)
plugin.disable(exception)
}
@JvmOverloads
fun disablePlugins(throwable: CancellationException? = null) {
CommandManager.clearPluginsCommands()
nameToPluginBaseMap.values.forEach {
it.disable(throwable)
}
nameToPluginBaseMap.clear()
pluginDescriptions.clear()
CommandManager.reload()
}