Introduce subject to UserCommandSender

This commit is contained in:
Him188 2020-05-14 16:58:03 +08:00
parent ffe9f5f992
commit 50332baad5

View File

@ -13,10 +13,8 @@ package net.mamoe.mirai.console.command
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.Friend import net.mamoe.mirai.contact.*
import net.mamoe.mirai.contact.Group import net.mamoe.mirai.message.MessageEvent
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.User
import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.PlainText import net.mamoe.mirai.message.data.PlainText
@ -45,6 +43,13 @@ interface CommandSender {
fun sendMessageBlocking(message: String) = runBlocking { sendMessage(message) } 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)) suspend inline fun CommandSender.sendMessage(message: String) = sendMessage(PlainText(message))
abstract class AbstractCommandSender : CommandSender { abstract class AbstractCommandSender : CommandSender {
@ -95,13 +100,18 @@ inline fun User.asCommandSender(): UserCommandSender {
* 代表一个用户私聊机器人执行指令 * 代表一个用户私聊机器人执行指令
* @see User.asCommandSender * @see User.asCommandSender
*/ */
sealed class UserCommandSender : AbstractCommandSender() { sealed class UserCommandSender : AbstractCommandSender(), BotAwareCommandSender {
abstract val user: User abstract val user: User
/**
* @see MessageEvent.subject
*/
abstract val subject: Contact
final override val bot: Bot get() = user.bot final override val bot: Bot get() = user.bot
final override suspend fun sendMessage(message: Message) { final override suspend fun sendMessage(message: Message) {
user.sendMessage(message) subject.sendMessage(message)
} }
} }
@ -109,7 +119,9 @@ sealed class UserCommandSender : AbstractCommandSender() {
* 代表一个用户私聊机器人执行指令 * 代表一个用户私聊机器人执行指令
* @see Friend.asCommandSender * @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() { class MemberCommandSender(override val user: Member) : UserCommandSender() {
inline val group: Group get() = user.group inline val group: Group get() = user.group
override val subject: Contact get() = group
} }