Commit 3f2845b3 authored by jiahua.liu's avatar jiahua.liu

rebuild Command

parent e9c79429
...@@ -9,17 +9,25 @@ ...@@ -9,17 +9,25 @@
package net.mamoe.mirai.console package net.mamoe.mirai.console
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.plugins.PluginManager import net.mamoe.mirai.console.plugins.PluginManager
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.contact.sendMessage
import net.mamoe.mirai.message.data.MessageChain
object CommandManager { object CommandManager {
private val registeredCommand: MutableMap<String, ICommand> = mutableMapOf() private val registeredCommand: MutableMap<String, Command> = mutableMapOf()
fun getCommands(): Collection<ICommand> { fun getCommands(): Collection<Command> {
return registeredCommand.values return registeredCommand.values
} }
fun register(command: ICommand) { fun register(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)) {
...@@ -31,7 +39,7 @@ object CommandManager { ...@@ -31,7 +39,7 @@ object CommandManager {
} }
} }
fun unregister(command: ICommand) { fun unregister(command: Command) {
val allNames = mutableListOf<String>(command.name).also { it.addAll(command.alias) } val allNames = mutableListOf<String>(command.name).also { it.addAll(command.alias) }
allNames.forEach { allNames.forEach {
registeredCommand.remove(it) registeredCommand.remove(it)
...@@ -42,7 +50,7 @@ object CommandManager { ...@@ -42,7 +50,7 @@ object CommandManager {
registeredCommand.remove(commandName) registeredCommand.remove(commandName)
} }
suspend fun runCommand(fullCommand: String): Boolean { suspend fun runCommand(sender: CommandSender, fullCommand: String): Boolean {
val blocks = fullCommand.split(" ") val blocks = fullCommand.split(" ")
val commandHead = blocks[0].replace("/", "") val commandHead = blocks[0].replace("/", "")
if (!registeredCommand.containsKey(commandHead)) { if (!registeredCommand.containsKey(commandHead)) {
...@@ -51,6 +59,7 @@ object CommandManager { ...@@ -51,6 +59,7 @@ object CommandManager {
val args = blocks.subList(1, blocks.size) val args = blocks.subList(1, blocks.size)
registeredCommand[commandHead]?.run { registeredCommand[commandHead]?.run {
if (onCommand( if (onCommand(
sender,
blocks.subList(1, blocks.size) blocks.subList(1, blocks.size)
) )
) { ) {
...@@ -62,26 +71,60 @@ object CommandManager { ...@@ -62,26 +71,60 @@ object CommandManager {
} }
interface ICommand { interface CommandSender {
suspend fun sendMessage(messageChain: MessageChain)
suspend fun sendMessage(message: String)
fun sendMessageBlocking(messageChain: MessageChain) = runBlocking { sendMessage(messageChain) }
fun sendMessageBlocking(message: String) = runBlocking { sendMessage(message) }
}
object ConsoleCommandSender : CommandSender {
override suspend fun sendMessage(messageChain: MessageChain) {
MiraiConsole.logger(messageChain.toString())
}
override suspend fun sendMessage(message: String) {
MiraiConsole.logger(message)
}
}
class ContactCommandSender(val contact: Contact) : CommandSender {
override suspend fun sendMessage(messageChain: MessageChain) {
contact.sendMessage(messageChain)
}
override suspend fun sendMessage(message: String) {
contact.sendMessage(message)
}
}
interface Command {
val name: String val name: String
val alias: List<String> val alias: List<String>
val description: String val description: String
suspend fun onCommand(args: List<String>): Boolean suspend fun onCommand(sender: CommandSender, args: List<String>): Boolean
fun register() fun register()
} }
abstract class Command( abstract class BlockingCommand(
override val name: String, override val name: String,
override val alias: List<String> = listOf(), override val alias: List<String> = listOf(),
override val description: String = "" override val description: String = ""
) : ICommand { ) : Command {
/** /**
* 最高优先级监听器 * 最高优先级监听器
* 如果 return `false` 这次指令不会被 [PluginBase] 的全局 onCommand 监听器监听 * 如果 return `false` 这次指令不会被 [PluginBase] 的全局 onCommand 监听器监听
* */ * */
open override suspend fun onCommand(args: List<String>): Boolean { final override suspend fun onCommand(sender: CommandSender, args: List<String>): Boolean {
return true return withContext(Dispatchers.IO) {
onCommandBlocking(sender, args)
} }
}
abstract fun onCommandBlocking(sender: CommandSender, args: List<String>): Boolean
override fun register() { override fun register() {
CommandManager.register(this) CommandManager.register(this)
...@@ -92,10 +135,10 @@ class AnonymousCommand internal constructor( ...@@ -92,10 +135,10 @@ class AnonymousCommand internal constructor(
override val name: String, override val name: String,
override val alias: List<String>, override val alias: List<String>,
override val description: String, override val description: String,
val onCommand: suspend ICommand.(args: List<String>) -> Boolean val onCommand: suspend CommandSender.(args: List<String>) -> Boolean
) : ICommand { ) : Command {
override suspend fun onCommand(args: List<String>): Boolean { override suspend fun onCommand(sender: CommandSender, args: List<String>): Boolean {
return onCommand.invoke(this, args) return onCommand.invoke(sender, args)
} }
override fun register() { override fun register() {
...@@ -107,13 +150,13 @@ class CommandBuilder internal constructor() { ...@@ -107,13 +150,13 @@ class CommandBuilder internal constructor() {
var name: String? = null var name: String? = null
var alias: List<String>? = null var alias: List<String>? = null
var description: String = "" var description: String = ""
var onCommand: (suspend ICommand.(args: List<String>) -> Boolean)? = null var onCommand: (suspend CommandSender.(args: List<String>) -> Boolean)? = null
fun onCommand(commandProcess: suspend ICommand.(args: List<String>) -> Boolean) { fun onCommand(commandProcess: suspend CommandSender.(args: List<String>) -> Boolean) {
onCommand = commandProcess onCommand = commandProcess
} }
fun register(): ICommand { fun register(): Command {
if (name == null || onCommand == null) { if (name == null || onCommand == null) {
error("net.mamoe.mirai.CommandBuilder not complete") error("net.mamoe.mirai.CommandBuilder not complete")
} }
...@@ -124,7 +167,7 @@ class CommandBuilder internal constructor() { ...@@ -124,7 +167,7 @@ class CommandBuilder internal constructor() {
} }
} }
fun registerCommand(builder: CommandBuilder.() -> Unit): ICommand { fun registerCommand(builder: CommandBuilder.() -> Unit): Command {
return CommandBuilder().apply(builder).register() return CommandBuilder().apply(builder).register()
} }
...@@ -50,11 +50,11 @@ object MiraiConsole { ...@@ -50,11 +50,11 @@ object MiraiConsole {
var path: String = System.getProperty("user.dir") var path: String = System.getProperty("user.dir")
val version = "v0.01" private val version = "v0.01"
var coreVersion = "v0.18.0" private var coreVersion = "v0.18.0"
val build = "Zeta" private val build = "Zeta"
var allDown = false private var allDown = false
lateinit var frontEnd: MiraiConsoleUI lateinit var frontEnd: MiraiConsoleUI
fun start( fun start(
...@@ -314,7 +314,7 @@ object MiraiConsole { ...@@ -314,7 +314,7 @@ object MiraiConsole {
if (!fullCommand.startsWith("/")) { if (!fullCommand.startsWith("/")) {
fullCommand = "/$fullCommand" fullCommand = "/$fullCommand"
} }
if (!CommandManager.runCommand(fullCommand)) { if (!CommandManager.runCommand(ConsoleCommandSender, fullCommand)) {
logger("未知指令 $fullCommand") logger("未知指令 $fullCommand")
} }
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
package net.mamoe.mirai.console.plugins package net.mamoe.mirai.console.plugins
import net.mamoe.mirai.console.ICommand import net.mamoe.mirai.console.Command
import kotlinx.coroutines.* import kotlinx.coroutines.*
import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.DefaultLogger
...@@ -58,7 +58,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope { ...@@ -58,7 +58,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
/** /**
* 当任意指令被使用 * 当任意指令被使用
*/ */
open fun onCommand(command: ICommand, args: List<String>) { open fun onCommand(command: Command, args: List<String>) {
} }
...@@ -173,7 +173,7 @@ object PluginManager { ...@@ -173,7 +173,7 @@ object PluginManager {
private val nameToPluginBaseMap: MutableMap<String, PluginBase> = mutableMapOf() private val nameToPluginBaseMap: MutableMap<String, PluginBase> = mutableMapOf()
private val pluginDescriptions: MutableMap<String, PluginDescription> = mutableMapOf() private val pluginDescriptions: MutableMap<String, PluginDescription> = mutableMapOf()
fun onCommand(command: ICommand, args: List<String>) { fun onCommand(command: Command, args: List<String>) {
nameToPluginBaseMap.values.forEach { nameToPluginBaseMap.values.forEach {
it.onCommand(command, args) it.onCommand(command, args)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment