Commit 5f6878f0 authored by jiahua.liu's avatar jiahua.liu

Console rebuild

parent c5a760a0
package net.mamoe.mirai.console package net.mamoe.mirai.console
import net.mamoe.mirai.console.pure.MiraiConsoleUIPure
import kotlin.concurrent.thread import kotlin.concurrent.thread
class MiraiConsoleTerminalLoader { class MiraiConsoleTerminalLoader {
......
...@@ -19,6 +19,7 @@ import net.mamoe.mirai.Bot ...@@ -19,6 +19,7 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.cleanPage import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.cleanPage
import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.drawLog import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.drawLog
import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.redrawLogs import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.redrawLogs
import net.mamoe.mirai.console.utils.MiraiConsoleUI
import net.mamoe.mirai.utils.LoginSolver import net.mamoe.mirai.utils.LoginSolver
import net.mamoe.mirai.utils.createCharImg import net.mamoe.mirai.utils.createCharImg
import net.mamoe.mirai.utils.writeChannel import net.mamoe.mirai.utils.writeChannel
...@@ -129,9 +130,7 @@ object MiraiConsoleTerminalUI : MiraiConsoleUI { ...@@ -129,9 +130,7 @@ object MiraiConsoleTerminalUI : MiraiConsoleUI {
requestResult = input requestResult = input
requesting = false requesting = false
} else { } else {
MiraiConsole.CommandListener.commandChannel.send( MiraiConsole.CommandProcessor.runConsoleCommand(commandBuilder.toString())
commandBuilder.toString()
)
} }
} }
......
...@@ -24,7 +24,7 @@ fun ktor(id: String, version: String) = "io.ktor:ktor-$id:$version" ...@@ -24,7 +24,7 @@ fun ktor(id: String, version: String) = "io.ktor:ktor-$id:$version"
tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>() { tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>() {
manifest { manifest {
attributes["Main-Class"] = "net.mamoe.mirai.console.MiraiConsolePureLoader" attributes["Main-Class"] = "net.mamoe.mirai.console.pure.MiraiConsolePureLoader"
} }
} }
......
...@@ -7,17 +7,23 @@ ...@@ -7,17 +7,23 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.console package net.mamoe.mirai.console.command
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import net.mamoe.mirai.Bot import net.mamoe.mirai.console.MiraiConsole
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.Contact
import net.mamoe.mirai.contact.QQ import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.sendMessage import net.mamoe.mirai.contact.sendMessage
import net.mamoe.mirai.message.GroupMessage
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.quote
import net.mamoe.mirai.message.data.toMessage
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import java.lang.StringBuilder
object CommandManager { object CommandManager {
private val registeredCommand: MutableMap<String, Command> = mutableMapOf() private val registeredCommand: MutableMap<String, Command> = mutableMapOf()
...@@ -50,7 +56,10 @@ object CommandManager { ...@@ -50,7 +56,10 @@ object CommandManager {
registeredCommand.remove(commandName) registeredCommand.remove(commandName)
} }
suspend fun runCommand(sender: CommandSender, fullCommand: String): Boolean { /*
* Index: MiraiConsole
* */
internal 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)) {
...@@ -58,6 +67,7 @@ object CommandManager { ...@@ -58,6 +67,7 @@ object CommandManager {
} }
val args = blocks.subList(1, blocks.size) val args = blocks.subList(1, blocks.size)
registeredCommand[commandHead]?.run { registeredCommand[commandHead]?.run {
try {
if (onCommand( if (onCommand(
sender, sender,
blocks.subList(1, blocks.size) blocks.subList(1, blocks.size)
...@@ -65,6 +75,12 @@ object CommandManager { ...@@ -65,6 +75,12 @@ object CommandManager {
) { ) {
PluginManager.onCommand(this, args) PluginManager.onCommand(this, args)
} }
} catch (e: Exception) {
sender.sendMessage("在运行指令时出现了未知错误")
e.printStackTrace()
} finally {
(sender as CommandSenderImpl).flushMessage()
}
} }
return true return true
} }
...@@ -72,25 +88,46 @@ object CommandManager { ...@@ -72,25 +88,46 @@ object CommandManager {
} }
interface CommandSender { interface CommandSender {
/**
* 立刻发送一条Message
*/
suspend fun sendMessage(messageChain: MessageChain) suspend fun sendMessage(messageChain: MessageChain)
suspend fun sendMessage(message: String) suspend fun sendMessage(message: String)
/**
* 写入要发送的内容 所有内容最后会被以一条发出, 不管成功与否
*/
fun appendMessage(message: String)
fun sendMessageBlocking(messageChain: MessageChain) = runBlocking { sendMessage(messageChain) } fun sendMessageBlocking(messageChain: MessageChain) = runBlocking { sendMessage(messageChain) }
fun sendMessageBlocking(message: String) = runBlocking { sendMessage(message) } fun sendMessageBlocking(message: String) = runBlocking { sendMessage(message) }
} }
object ConsoleCommandSender : CommandSender { abstract class CommandSenderImpl : CommandSender {
private val builder = StringBuilder()
override fun appendMessage(message: String) {
builder.append(message).append("\n")
}
internal suspend fun flushMessage() {
if (!builder.isEmpty()) {
sendMessage(builder.toString().removeSuffix("\n"))
}
}
}
object ConsoleCommandSender : CommandSenderImpl() {
override suspend fun sendMessage(messageChain: MessageChain) { override suspend fun sendMessage(messageChain: MessageChain) {
MiraiConsole.logger(messageChain.toString()) MiraiConsole.logger("[Command]", 0, messageChain.toString())
} }
override suspend fun sendMessage(message: String) { override suspend fun sendMessage(message: String) {
MiraiConsole.logger(message) MiraiConsole.logger("[Command]", 0, message)
} }
} }
class ContactCommandSender(val contact: Contact) : CommandSender { open class ContactCommandSender(val contact: Contact) : CommandSenderImpl() {
override suspend fun sendMessage(messageChain: MessageChain) { override suspend fun sendMessage(messageChain: MessageChain) {
contact.sendMessage(messageChain) contact.sendMessage(messageChain)
} }
...@@ -100,6 +137,20 @@ class ContactCommandSender(val contact: Contact) : CommandSender { ...@@ -100,6 +137,20 @@ class ContactCommandSender(val contact: Contact) : CommandSender {
} }
} }
/**
* 弃用中
* */
class GroupCommandSender(val toQuote: GroupMessage, contact: Contact) : ContactCommandSender(contact) {
@MiraiExperimentalAPI
override suspend fun sendMessage(message: String) {
toQuote.quoteReply(message)
}
@MiraiExperimentalAPI
override suspend fun sendMessage(messageChain: MessageChain) {
toQuote.quoteReply(messageChain)
}
}
interface Command { interface Command {
val name: String val name: String
...@@ -163,7 +214,12 @@ class CommandBuilder internal constructor() { ...@@ -163,7 +214,12 @@ class CommandBuilder internal constructor() {
if (alias == null) { if (alias == null) {
alias = listOf() alias = listOf()
} }
return AnonymousCommand(name!!, alias!!, description, onCommand!!).also { it.register() } return AnonymousCommand(
name!!,
alias!!,
description,
onCommand!!
).also { it.register() }
} }
} }
......
...@@ -21,6 +21,7 @@ import org.yaml.snakeyaml.Yaml ...@@ -21,6 +21,7 @@ import org.yaml.snakeyaml.Yaml
import java.io.File import java.io.File
import java.util.* import java.util.*
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import kotlin.collections.LinkedHashMap
import kotlin.properties.ReadWriteProperty import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
...@@ -211,7 +212,15 @@ fun <T : Any> Config._smartCast(propertyName: String, _class: KClass<T>): T { ...@@ -211,7 +212,15 @@ fun <T : Any> Config._smartCast(propertyName: String, _class: KClass<T>): T {
interface ConfigSection : Config, MutableMap<String, Any> { interface ConfigSection : Config, MutableMap<String, Any> {
override fun getConfigSection(key: String): ConfigSection { override fun getConfigSection(key: String): ConfigSection {
return (get(key) ?: error("ConfigSection does not contain $key ")) as ConfigSection val content = get(key) ?: error("ConfigSection does not contain $key ")
if (content is ConfigSection) {
return content
}
return ConfigSectionDelegation(
Collections.synchronizedMap(
(get(key) ?: error("ConfigSection does not contain $key ")) as LinkedHashMap<String, Any>
)
)
} }
override fun getString(key: String): String { override fun getString(key: String): String {
......
...@@ -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.Command import net.mamoe.mirai.console.command.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
......
package net.mamoe.mirai.console package net.mamoe.mirai.console.pure
import net.mamoe.mirai.console.MiraiConsole
import kotlin.concurrent.thread import kotlin.concurrent.thread
class MiraiConsolePureLoader { class MiraiConsolePureLoader {
......
...@@ -7,11 +7,12 @@ ...@@ -7,11 +7,12 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.console package net.mamoe.mirai.console.pure
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.utils.MiraiConsoleUI
import net.mamoe.mirai.utils.DefaultLoginSolver import net.mamoe.mirai.utils.DefaultLoginSolver
import net.mamoe.mirai.utils.LoginSolver import net.mamoe.mirai.utils.LoginSolver
import net.mamoe.mirai.utils.LoginSolverInputReader import net.mamoe.mirai.utils.LoginSolverInputReader
...@@ -29,9 +30,7 @@ class MiraiConsoleUIPure : MiraiConsoleUI { ...@@ -29,9 +30,7 @@ class MiraiConsoleUIPure : MiraiConsoleUI {
requestStr = input requestStr = input
requesting = false requesting = false
} else { } else {
runBlocking { MiraiConsole.CommandProcessor.runConsoleCommandBlocking(input)
MiraiConsole.CommandListener.commandChannel.send(input)
}
} }
} }
} }
......
package net.mamoe.mirai.console.utils
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.MiraiProperties
import net.mamoe.mirai.console.utils.BotManagers.BOT_MANAGERS
import net.mamoe.mirai.console.plugins.ConfigSection
import net.mamoe.mirai.console.plugins.ConfigSectionImpl
import net.mamoe.mirai.console.plugins.loadAsConfig
import net.mamoe.mirai.console.plugins.withDefaultWriteSave
import java.io.File
object BotManagers {
val config = File("${MiraiConsole.path}/bot.yml").loadAsConfig()
val BOT_MANAGERS: ConfigSection by config.withDefaultWriteSave { ConfigSectionImpl() }
}
fun Bot.addManager(long: Long) {
BOT_MANAGERS.putIfAbsent(this.uin.toString(), mutableListOf<Long>())
BOT_MANAGERS[this.uin.toString()] =
(BOT_MANAGERS.getLongList(this.uin.toString()) as MutableList<Long>).apply { add(long) }
BotManagers.config.save()
}
fun Bot.removeManager(long: Long) {
BOT_MANAGERS.putIfAbsent(this.uin.toString(), mutableListOf<Long>())
BOT_MANAGERS[this.uin.toString()] =
(BOT_MANAGERS.getLongList(this.uin.toString()) as MutableList<Long>).apply { add(long) }
BotManagers.config.save()
}
fun Bot.getManagers(): List<Long> {
BOT_MANAGERS.putIfAbsent(this.uin.toString(), mutableListOf<Long>())
return BOT_MANAGERS.getLongList(this.uin.toString())
}
fun Bot.checkManager(long: Long): Boolean {
return this.getManagers().contains(long)
}
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.console package net.mamoe.mirai.console.utils
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.utils.LoginSolver import net.mamoe.mirai.utils.LoginSolver
......
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