mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-19 18:54:43 +08:00
Command Builder
This commit is contained in:
parent
0e39c300ad
commit
155ad75c9b
@ -12,6 +12,10 @@ import net.mamoe.mirai.plugin.PluginManager
|
||||
object CommandManager {
|
||||
private val registeredCommand: MutableMap<String, ICommand> = mutableMapOf()
|
||||
|
||||
fun getCommands(): Collection<ICommand> {
|
||||
return registeredCommand.values
|
||||
}
|
||||
|
||||
|
||||
fun register(command: ICommand) {
|
||||
val allNames = mutableListOf(command.name).also { it.addAll(command.alias) }
|
||||
@ -32,6 +36,10 @@ object CommandManager {
|
||||
}
|
||||
}
|
||||
|
||||
fun unregister(commandName: String) {
|
||||
registeredCommand.remove(commandName)
|
||||
}
|
||||
|
||||
fun runCommand(fullCommand: String): Boolean {
|
||||
val blocks = fullCommand.split(" ")
|
||||
val commandHead = blocks[0].replace("/", "")
|
||||
@ -99,6 +107,10 @@ class CommandBuilder internal constructor() {
|
||||
var description: String = ""
|
||||
var onCommand: (ICommand.(args: List<String>) -> Boolean)? = null
|
||||
|
||||
fun onCommand(commandProcess: ICommand.(args: List<String>) -> Boolean) {
|
||||
onCommand = commandProcess
|
||||
}
|
||||
|
||||
fun register(): ICommand {
|
||||
if (name == null || onCommand == null) {
|
||||
error("CommandBuilder not complete")
|
||||
@ -111,8 +123,6 @@ class CommandBuilder internal constructor() {
|
||||
}
|
||||
|
||||
fun buildCommand(builder: CommandBuilder.() -> Unit): ICommand {
|
||||
val builder2 = CommandBuilder()
|
||||
builder.invoke(builder2)
|
||||
return builder2.register()
|
||||
return CommandBuilder().apply(builder).register()
|
||||
}
|
||||
|
||||
|
@ -12,14 +12,25 @@ import kotlinx.serialization.UnstableDefault
|
||||
import net.mamoe.mirai.Bot
|
||||
import net.mamoe.mirai.alsoLogin
|
||||
import net.mamoe.mirai.api.http.generateSessionKey
|
||||
import net.mamoe.mirai.contact.sendMessage
|
||||
import net.mamoe.mirai.plugin.*
|
||||
import java.io.File
|
||||
import kotlin.concurrent.thread
|
||||
import kotlin.math.log
|
||||
|
||||
object MiraiConsole {
|
||||
val bots
|
||||
get() = Bot.instances
|
||||
|
||||
fun getBotByUIN(uin: Long): Bot? {
|
||||
bots.forEach {
|
||||
if (it.get()?.uin == uin) {
|
||||
return it.get()
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
val pluginManager: PluginManager
|
||||
get() = PluginManager
|
||||
|
||||
@ -46,7 +57,6 @@ object MiraiConsole {
|
||||
DefaultCommands()
|
||||
pluginManager.loadPlugins()
|
||||
CommandListener.start()
|
||||
println(MiraiProperties.HTTP_API_ENABLE)
|
||||
logger("\"/login qqnumber qqpassword \" to login a bot")
|
||||
logger("\"/login qq号 qq密码 \" 来登陆一个BOT")
|
||||
|
||||
@ -64,19 +74,19 @@ object MiraiConsole {
|
||||
buildCommand {
|
||||
name = "login"
|
||||
description = "Mirai-Console default bot login command"
|
||||
onCommand = {
|
||||
onCommand {
|
||||
if (it.size < 2) {
|
||||
logger("\"/login qqnumber qqpassword \" to login a bot")
|
||||
logger("\"/login qq号 qq密码 \" 来登录一个BOT")
|
||||
false
|
||||
return@onCommand false
|
||||
}
|
||||
val qqNumber = it[0].toLong()
|
||||
val qqPassword = it[1]
|
||||
println("login...")
|
||||
logger("login...")
|
||||
try {
|
||||
runBlocking {
|
||||
Bot(qqNumber, qqPassword).alsoLogin()
|
||||
println("$qqNumber login successed")
|
||||
println("$qqNumber login successes")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("$qqNumber login failed")
|
||||
@ -88,13 +98,23 @@ object MiraiConsole {
|
||||
buildCommand {
|
||||
name = "status"
|
||||
description = "Mirai-Console default status command"
|
||||
onCommand = {
|
||||
onCommand {
|
||||
when (it.size) {
|
||||
0 -> {
|
||||
|
||||
logger("当前有" + bots.size + "个BOT在线")
|
||||
}
|
||||
1 -> {
|
||||
|
||||
val bot = it[0]
|
||||
var find = false
|
||||
bots.forEach {
|
||||
if (it.get()?.uin.toString().contains(bot)) {
|
||||
find = true
|
||||
logger("" + it.get()?.uin + ": 在线中; 好友数量:" + it.get()?.qqs?.size + "; 群组数量:" + it.get()?.groups?.size)
|
||||
}
|
||||
}
|
||||
if (!find) {
|
||||
logger("没有找到BOT$bot")
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
@ -105,14 +125,36 @@ object MiraiConsole {
|
||||
buildCommand {
|
||||
name = "say"
|
||||
description = "Mirai-Console default say command"
|
||||
onCommand = {
|
||||
when (it.size) {
|
||||
0 -> {
|
||||
|
||||
onCommand {
|
||||
if (it.size < 2) {
|
||||
logger("say [好友qq号或者群号] [文本消息] //将默认使用第一个BOT")
|
||||
logger("say [bot号] [好友qq号或者群号] [文本消息]")
|
||||
return@onCommand false
|
||||
}
|
||||
val bot: Bot? = if (it.size == 2) {
|
||||
if (bots.size == 0) {
|
||||
logger("还没有BOT登陆")
|
||||
return@onCommand false
|
||||
}
|
||||
1 -> {
|
||||
|
||||
bots[0].get()
|
||||
} else {
|
||||
getBotByUIN(it[0].toLong())
|
||||
}
|
||||
if (bot == null) {
|
||||
logger("没有找到BOT")
|
||||
return@onCommand false
|
||||
}
|
||||
val target = it[it.size - 2].toLong()
|
||||
val message = it[it.size - 1]
|
||||
try {
|
||||
val contact = bot[target]
|
||||
runBlocking {
|
||||
contact.sendMessage(message)
|
||||
logger("消息已推送")
|
||||
}
|
||||
} catch (e: NoSuchElementException) {
|
||||
logger("没有找到群或好友 号码为${target}")
|
||||
return@onCommand false
|
||||
}
|
||||
true
|
||||
}
|
||||
@ -123,16 +165,14 @@ object MiraiConsole {
|
||||
name = "plugins"
|
||||
alias = listOf("plugin")
|
||||
description = "show all plugins"
|
||||
onCommand = {
|
||||
when (it.size) {
|
||||
0 -> {
|
||||
|
||||
}
|
||||
1 -> {
|
||||
|
||||
onCommand {
|
||||
PluginManager.getAllPluginDescriptions().let {
|
||||
println("loaded " + it.size + " plugins")
|
||||
it.forEach {
|
||||
logger("\t" + it.name + " v" + it.version + " by" + it.author + " " + it.info)
|
||||
}
|
||||
true
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
@ -140,13 +180,11 @@ object MiraiConsole {
|
||||
name = "command"
|
||||
alias = listOf("commands", "help", "helps")
|
||||
description = "show all commands"
|
||||
onCommand = {
|
||||
when (it.size) {
|
||||
0 -> {
|
||||
|
||||
}
|
||||
1 -> {
|
||||
|
||||
onCommand {
|
||||
CommandManager.getCommands().let {
|
||||
println("currently have " + it.size + " commands")
|
||||
it.toSet().forEach {
|
||||
logger("\t" + it.name + " :" + it.description)
|
||||
}
|
||||
}
|
||||
true
|
||||
@ -155,16 +193,13 @@ object MiraiConsole {
|
||||
|
||||
buildCommand {
|
||||
name = "about"
|
||||
description = ""
|
||||
onCommand = {
|
||||
when (it.size) {
|
||||
0 -> {
|
||||
|
||||
}
|
||||
1 -> {
|
||||
|
||||
}
|
||||
}
|
||||
description = "About Mirai-Console"
|
||||
onCommand {
|
||||
logger("v${version} $build is still in testing stage, majority feature is available")
|
||||
logger("now running under " + System.getProperty("user.dir"))
|
||||
logger("在Github中获取项目最新进展: https://github.com/mamoe/mirai")
|
||||
logger("Mirai为开源项目,请自觉遵守开源项目协议")
|
||||
logger("Powered by Mamoe Technology")
|
||||
true
|
||||
}
|
||||
}
|
||||
@ -180,10 +215,12 @@ object MiraiConsole {
|
||||
}
|
||||
|
||||
tailrec fun processNextCommandLine() {
|
||||
val fullCommand = readLine()
|
||||
if (fullCommand != null && fullCommand.startsWith("/")) {
|
||||
var fullCommand = readLine()
|
||||
if (fullCommand != null) {
|
||||
if (!fullCommand.startsWith("/")) {
|
||||
fullCommand = "/$fullCommand"
|
||||
}
|
||||
if (!CommandManager.runCommand(fullCommand)) {
|
||||
logger("unknown command $fullCommand")
|
||||
logger("未知指令 $fullCommand")
|
||||
}
|
||||
}
|
||||
|
@ -163,6 +163,7 @@ object PluginManager {
|
||||
|
||||
//已完成加载的
|
||||
private val nameToPluginBaseMap: MutableMap<String, PluginBase> = mutableMapOf()
|
||||
private val pluginDescriptions: MutableMap<String, PluginDescription> = mutableMapOf()
|
||||
|
||||
fun onCommand(command: ICommand, args: List<String>) {
|
||||
this.nameToPluginBaseMap.values.forEach {
|
||||
@ -170,6 +171,10 @@ object PluginManager {
|
||||
}
|
||||
}
|
||||
|
||||
fun getAllPluginDescriptions(): Collection<PluginDescription> {
|
||||
return pluginDescriptions.values
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试加载全部插件
|
||||
*/
|
||||
@ -177,6 +182,8 @@ object PluginManager {
|
||||
val pluginsFound: MutableMap<String, PluginDescription> = mutableMapOf()
|
||||
val pluginsLocation: MutableMap<String, File> = mutableMapOf()
|
||||
|
||||
logger.info("""开始加载${this.pluginsPath}下的插件""")
|
||||
|
||||
File(pluginsPath).listFiles()?.forEach { file ->
|
||||
if (file != null && file.extension == "jar") {
|
||||
val jar = JarFile(file)
|
||||
@ -270,6 +277,7 @@ object PluginManager {
|
||||
logger.info(description.info)
|
||||
|
||||
nameToPluginBaseMap[description.name] = plugin
|
||||
pluginDescriptions[description.name] = description
|
||||
plugin.init(description)
|
||||
true
|
||||
} catch (e: ClassCastException) {
|
||||
@ -290,6 +298,9 @@ object PluginManager {
|
||||
nameToPluginBaseMap.values.forEach {
|
||||
it.enable()
|
||||
}
|
||||
|
||||
logger.info("""加载了${this.nameToPluginBaseMap.size}个插件""")
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user