From 50332baad5980080d1f36299b7d1bc7cbe5352a5 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 14 May 2020 16:58:03 +0800 Subject: [PATCH] Introduce `subject` to `UserCommandSender` --- .../mirai/console/command/CommandSender.kt | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt index 0ae71d792..5f168a279 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt @@ -13,10 +13,8 @@ package net.mamoe.mirai.console.command import kotlinx.coroutines.runBlocking import net.mamoe.mirai.Bot -import net.mamoe.mirai.contact.Friend -import net.mamoe.mirai.contact.Group -import net.mamoe.mirai.contact.Member -import net.mamoe.mirai.contact.User +import net.mamoe.mirai.contact.* +import net.mamoe.mirai.message.MessageEvent import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.PlainText @@ -45,6 +43,13 @@ interface CommandSender { fun sendMessageBlocking(message: String) = runBlocking { sendMessage(message) } } +/** + * 可以知道其 [Bot] 的 [CommandSender] + */ +interface BotAwareCommandSender : CommandSender { + override val bot: Bot +} + suspend inline fun CommandSender.sendMessage(message: String) = sendMessage(PlainText(message)) abstract class AbstractCommandSender : CommandSender { @@ -95,13 +100,18 @@ inline fun User.asCommandSender(): UserCommandSender { * 代表一个用户私聊机器人执行指令 * @see User.asCommandSender */ -sealed class UserCommandSender : AbstractCommandSender() { +sealed class UserCommandSender : AbstractCommandSender(), BotAwareCommandSender { abstract val user: User + /** + * @see MessageEvent.subject + */ + abstract val subject: Contact + final override val bot: Bot get() = user.bot final override suspend fun sendMessage(message: Message) { - user.sendMessage(message) + subject.sendMessage(message) } } @@ -109,7 +119,9 @@ sealed class UserCommandSender : AbstractCommandSender() { * 代表一个用户私聊机器人执行指令 * @see Friend.asCommandSender */ -class FriendCommandSender(override val user: Friend) : UserCommandSender() +class FriendCommandSender(override val user: Friend) : UserCommandSender() { + override val subject: Contact get() = user +} /** * 代表一个群成员在群内执行指令. @@ -117,4 +129,5 @@ class FriendCommandSender(override val user: Friend) : UserCommandSender() */ class MemberCommandSender(override val user: Member) : UserCommandSender() { inline val group: Group get() = user.group + override val subject: Contact get() = group } \ No newline at end of file