From 9d580e9302a899d5fc893056f242753f2369bb10 Mon Sep 17 00:00:00 2001 From: mzdluo123 Date: Mon, 11 May 2020 16:16:10 +0800 Subject: [PATCH 1/3] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index b937e4e78..63922d3fd 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,12 @@ Unix 没有一键包提供. 请使用 wrapper 启动器. 2. 下载 `mirai-console-wrapper-x.x.x.jar` 3. 参照 [wrapper 命令行参数](https://github.com/mirai/mirai-console-wrapper/README.md#命令行参数), 运行 `$ java -jar mirai-console-wrapper-x.x.x.jar` +#### Android + +[MiraiAndroid](https://github.com/mzdluo123/MiraiAndroid) 提供了在Android平台使用mirai-console插件的能力,同时拥有一个便于使用的lua接口 + +你可以点击这里查看[项目详细](https://github.com/mzdluo123/MiraiAndroid) + ### 插件开发与获取 mirai-console 支持 Jar 插件. From 4d061c070eada05624bbb17afcdef356db0f6140 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 12 May 2020 12:15:14 +0800 Subject: [PATCH 2/3] Remove custom prefix before executing command --- .../kotlin/net/mamoe/mirai/console/command/CommandManager.kt | 2 +- .../kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt index 164d6eaca..7b9631341 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt @@ -150,7 +150,7 @@ object CommandManager : Job by { private suspend fun processCommandImpl(sender: CommandSender, fullCommand: String): Boolean { val blocks = fullCommand.split(" ") - val commandHead = blocks[0] //.replace("/", "") + val commandHead = blocks[0].removePrefix(DefaultCommands.commandPrefix) val args = blocks.drop(1) return registeredCommand[commandHead]?.run { try { diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt index 21382fa88..36b5758a6 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/DefaultCommands.kt @@ -30,7 +30,7 @@ import java.util.* * Some defaults commands are recommend to be replaced by plugin provided commands */ internal object DefaultCommands { - private val commandPrefix = "mirai.command.prefix".property() ?: "/" + internal val commandPrefix = "mirai.command.prefix".property() ?: "/" private suspend fun CommandSender.login(account: Long, password: String) { MiraiConsole.logger("[Bot Login]", 0, "login...") try { From 1b86b61cb4409f8c1d222f88cf05cec2ee1bbead Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 12 May 2020 12:30:36 +0800 Subject: [PATCH 3/3] Update command arg parsers --- .../mamoe/mirai/console/utils/CommandArg.kt | 90 ---------------- .../mirai/console/utils/CommandArgParser.kt | 100 ++++++++++++++++++ 2 files changed, 100 insertions(+), 90 deletions(-) delete mode 100644 mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt create mode 100644 mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArgParser.kt diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt deleted file mode 100644 index d335f662a..000000000 --- a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt +++ /dev/null @@ -1,90 +0,0 @@ -package net.mamoe.mirai.console.utils - -import net.mamoe.mirai.Bot -import net.mamoe.mirai.console.command.CommandSender -import net.mamoe.mirai.console.command.GroupContactCommandSender -import net.mamoe.mirai.contact.Group - -/** - * this output type of that arg - * input is always String - */ -interface CommandArg { - operator fun invoke(): T = get() - - fun get(): T - - fun read(s: String, commandSender: CommandSender) -} - - -abstract class CommandArgImpl : CommandArg { - - lateinit var value: T - - override fun get(): T = value - - override fun read(s: String, commandSender: CommandSender) { - value = parse(s, commandSender) - } - - abstract fun parse(s: String, commandSender: CommandSender): T -} - -class IntArg : CommandArgImpl() { - override fun parse(s: String, commandSender: CommandSender): Int = s.toInt() -} - -class LongArg : CommandArgImpl() { - override fun parse(s: String, commandSender: CommandSender): Long = s.toLong() -} - -class DoubleArg : CommandArgImpl() { - override fun parse(s: String, commandSender: CommandSender): Double = s.toDouble() -} - - -class StringArg : CommandArgImpl() { - override fun parse(s: String, commandSender: CommandSender): String = s -} - -/** - * require a bot that already login in console - * input: Bot UIN - * output: Bot - * errors: String->Int convert, Bot Not Exist - */ - -class ExistBotArg : CommandArgImpl() { - override fun parse(s: String, commandSender: CommandSender): Bot { - val uin = try { - s.toLong() - } catch (e: Exception) { - error("无法识别QQ UIN$s") - } - return try { - Bot.getInstance(uin) - } catch (e: NoSuchElementException) { - error("无法找到Bot $uin") - } - } -} - - -class ExistGroupArg : CommandArgImpl() { - - override fun parse(s: String, commandSender: CommandSender): Group { - if ((s === "" || s === "~") && commandSender is GroupContactCommandSender) { - return commandSender.contact as Group - } - - val code = try { - s.toLong() - } catch (e: Exception) { - error("无法识别Group Code$s") - } - - TODO() - - } -} diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArgParser.kt b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArgParser.kt new file mode 100644 index 000000000..6365f68b0 --- /dev/null +++ b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArgParser.kt @@ -0,0 +1,100 @@ +@file:Suppress("NOTHING_TO_INLINE") + +package net.mamoe.mirai.console.utils + +import net.mamoe.mirai.Bot +import net.mamoe.mirai.console.command.CommandSender +import net.mamoe.mirai.console.command.GroupContactCommandSender +import net.mamoe.mirai.contact.Group + +/** + * this output type of that arg + * input is always String + */ +abstract class CommandArgParser { + abstract fun parse(s: String, sender: CommandSender): T + protected inline fun parseError(message: String, cause: Throwable? = null): Nothing { + throw ParserException(message, cause) + } +} + +@Suppress("FunctionName") +inline fun CommandArgParser( + crossinline parser: CommandArgParser.(s: String, sender: CommandSender) -> T +): CommandArgParser { + return object : CommandArgParser() { + override fun parse(s: String, sender: CommandSender): T = parser(s, sender) + } +} + +/** + * 在解析参数时遇到的 _正常_ 错误. 如参数不符合规范. + */ +class ParserException(message: String, cause: Throwable? = null) : RuntimeException(message, cause) + +inline fun Int.Companion.parser(): CommandArgParser = IntArgParser +inline fun Long.Companion.parser(): CommandArgParser = LongArgParser +inline fun Byte.Companion.parser(): CommandArgParser = ByteArgParser +inline fun Short.Companion.parser(): CommandArgParser = ShortArgParser +inline fun Float.Companion.parser(): CommandArgParser = FloatArgParser +inline fun Double.Companion.parser(): CommandArgParser = DoubleArgParser + + +object IntArgParser : CommandArgParser() { + override fun parse(s: String, sender: CommandSender): Int = s.toIntOrNull() ?: parseError("无法解析 $s 为整数") +} + +object LongArgParser : CommandArgParser() { + override fun parse(s: String, sender: CommandSender): Long = s.toLongOrNull() ?: parseError("无法解析 $s 为长整数") +} + +object ShortArgParser : CommandArgParser() { + override fun parse(s: String, sender: CommandSender): Short = s.toShortOrNull() ?: parseError("无法解析 $s 为短整数") +} + +object ByteArgParser : CommandArgParser() { + override fun parse(s: String, sender: CommandSender): Byte = s.toByteOrNull() ?: parseError("无法解析 $s 为字节") +} + +object DoubleArgParser : CommandArgParser() { + override fun parse(s: String, sender: CommandSender): Double = + s.toDoubleOrNull() ?: parseError("无法解析 $s 为小数") +} + +object FloatArgParser : CommandArgParser() { + override fun parse(s: String, sender: CommandSender): Float = + s.toFloatOrNull() ?: parseError("无法解析 $s 为小数") +} + +object StringArgParser : CommandArgParser() { + override fun parse(s: String, sender: CommandSender): String = s +} + +/** + * require a bot that already login in console + * input: Bot UIN + * output: Bot + * errors: String->Int convert, Bot Not Exist + */ +object ExistBotArgParser : CommandArgParser() { + override fun parse(s: String, sender: CommandSender): Bot { + val uin = s.toLongOrNull() ?: parseError("无法识别机器人账号 $s") + return try { + Bot.getInstance(uin) + } catch (e: NoSuchElementException) { + error("无法找到 Bot $uin") + } + } +} + + +object ExistGroupArgParser : CommandArgParser() { + override fun parse(s: String, sender: CommandSender): Group { + if ((s == "" || s == "~") && sender is GroupContactCommandSender) { + return sender.contact as Group + } + + val code = s.toLongOrNull() ?: parseError("无法识别群号码 $s") + TODO() + } +}