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 index 8a121d908..b4b191bcd 100644 --- 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 @@ -2,34 +2,33 @@ package net.mamoe.mirai.console.utils import net.mamoe.mirai.Bot import net.mamoe.mirai.console.command.CommandSender +import net.mamoe.mirai.console.command.BotAware +import net.mamoe.mirai.console.command.ContactCommandSender import net.mamoe.mirai.console.command.GroupContactCommandSender -import net.mamoe.mirai.contact.Group +import net.mamoe.mirai.contact.* +import net.mamoe.mirai.message.data.At +import net.mamoe.mirai.message.data.SingleMessage +import net.mamoe.mirai.message.data.content +import java.lang.NumberFormatException /** * this output type of that arg * input is always String */ interface CommandArg{ - operator fun invoke():T = get() + operator fun invoke(s:String, commandSender: CommandSender):T = parse(s,commandSender) - fun get():T + operator fun invoke(s:SingleMessage, commandSender: CommandSender):T = parse(s,commandSender) - fun read(s:String, commandSender: CommandSender) + fun parse(s:String, commandSender: CommandSender):T + + fun parse(s:SingleMessage, commandSender: CommandSender):T } 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 + override fun parse(s: SingleMessage, commandSender: CommandSender): T = parse(s.content,commandSender) } class IntArg:CommandArgImpl(){ @@ -41,7 +40,6 @@ class IntArg:CommandArgImpl(){ } } } - class LongArg:CommandArgImpl(){ override fun parse(s: String, commandSender: CommandSender): Long { return try{ @@ -51,7 +49,6 @@ class LongArg:CommandArgImpl(){ } } } - class DoubleArg:CommandArgImpl(){ override fun parse(s: String, commandSender: CommandSender): Double { return try{ @@ -61,8 +58,20 @@ class DoubleArg:CommandArgImpl(){ } } } - - +class FloatArg:CommandArgImpl(){ + override fun parse(s: String, commandSender: CommandSender): Float{ + return try{ + s.toFloat() + }catch (e:Exception){ + error("无法识别小数$s") + } + } +} +class BooleanArg:CommandArgImpl(){ + override fun parse(s: String, commandSender: CommandSender): Boolean { + return s.equals("true",true) || s.equals("yes",true) + } +} class StringArg:CommandArgImpl(){ override fun parse(s: String, commandSender: CommandSender): String { return s @@ -75,7 +84,6 @@ class StringArg:CommandArgImpl(){ * output: Bot * errors: String->Int convert, Bot Not Exist */ - class ExistBotArg:CommandArgImpl(){ override fun parse(s: String, commandSender: CommandSender): Bot { val uin = try{ @@ -91,22 +99,69 @@ class ExistBotArg:CommandArgImpl(){ } } - - -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() - +class ExistFriendArg:CommandArgImpl(){ + override fun parse(s: String, commandSender: CommandSender): Friend { + TODO("Not yet implemented") } } + +class ExistGroupArg:CommandArgImpl(){ + override fun parse(s: String, commandSender: CommandSender): Group { + //by default + if ((s == "" || s == "~") && commandSender is GroupContactCommandSender) { + return commandSender.contact as Group + } + //from bot to group + if (commandSender is BotAware) { + val code = try { + s.toLong() + } catch (e: NoSuchElementException) { + error("无法识别Group Code$s") + } + return try { + commandSender.bot.getGroup(code) + } catch (e: NoSuchElementException) { + error("无法找到Group " + code + " from Bot " + commandSender.bot.id) + } + } + //from console/other + return with(s.split(".")) { + if (this.size != 2) { + error("请使用BotQQ号.群号 来表示Bot的一个群") + } + try { + Bot.getInstance(this[0].toLong()).getGroup(this[1].toLong()) + }catch (e:NoSuchElementException){ + error("无法找到" + this[0] + "的" + this[1] + "群") + }catch (e:NumberFormatException){ + error("无法识别群号或机器人UIN") + } + } + } +} + +class ExistMemberArg:CommandArgImpl(){ + //后台: Bot.Group.Member[QQ/名片] + //私聊: Group.Member[QQ/名片] + //群内: Q号 + //群内: 名片 + override fun parse(s: String, commandSender: CommandSender): Member { + if(commandSender !is BotAware){ + with(s.split(".")){ + if(this.size < 3){ + //TODO() + } + } + } + //TODO() + } + + override fun parse(s: SingleMessage, commandSender: CommandSender): Member { + return if(s is At){ + assert(commandSender is GroupContactCommandSender) + ((commandSender as GroupContactCommandSender).contact as Group).members[s.target] + }else{ + error("无法识别Member" + s.content) + } + } +} \ No newline at end of file diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt index 206c01890..9654fc5f1 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt @@ -10,6 +10,7 @@ package net.mamoe.mirai.console.command import kotlinx.coroutines.runBlocking +import net.mamoe.mirai.Bot import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Member @@ -36,6 +37,7 @@ interface CommandSender { fun sendMessageBlocking(message: String) = runBlocking { sendMessage(message) } } + abstract class AbstractCommandSender : CommandSender { internal val builder = StringBuilder() @@ -68,11 +70,20 @@ object ConsoleCommandSender : AbstractCommandSender() { } } +/** + * 指向性CommandSender + * 你可以获得用户在和哪个Bot说指令 + */ +interface BotAware{ + val bot:Bot +} + + /** * 联系人指令执行者. 代表由一个 QQ 用户私聊执行指令 */ @Suppress("MemberVisibilityCanBePrivate") -open class ContactCommandSender(val contact: Contact) : AbstractCommandSender() { +open class ContactCommandSender(override val bot: Bot, val contact: Contact) : AbstractCommandSender(), BotAware{ override suspend fun sendMessage(messageChain: Message) { contact.sendMessage(messageChain) } @@ -86,6 +97,7 @@ open class ContactCommandSender(val contact: Contact) : AbstractCommandSender() * 联系人指令执行者. 代表由一个 QQ 用户 在群里执行指令 */ open class GroupContactCommandSender( + bot: Bot, val realSender: Member, subject: Contact -):ContactCommandSender(subject) \ No newline at end of file +):ContactCommandSender(bot,subject) \ No newline at end of file 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 692bdab84..fdc350b37 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 @@ -62,9 +62,9 @@ object DefaultCommands { startsWith(commandPrefix) { message -> if (bot.checkManager(this.sender.id)) { val sender = if (this is GroupMessageEvent) { - GroupContactCommandSender(this.sender, this.subject) + GroupContactCommandSender(bot,this.sender, this.subject) } else { - ContactCommandSender(this.subject) + ContactCommandSender(bot,this.subject) } CommandManager.runCommand( sender, message