Commit 17aff7d5 authored by Him188's avatar Him188

Make `quoteReply` common to MessagePacket

parent c441043d
......@@ -212,7 +212,6 @@ internal class MessageSvc {
msg.msgHead.fromUin == bot.uin -> null
!bot.firstLoginSucceed -> null
else -> FriendMessage(
bot,
bot.getFriend(msg.msgHead.fromUin),
msg.toMessageChain()
)
......
......@@ -17,7 +17,6 @@ import net.mamoe.mirai.utils.getValue
import net.mamoe.mirai.utils.unsafeWeakRef
class FriendMessage(
bot: Bot,
sender: QQ,
override val message: MessageChain
) : MessagePacket<QQ, QQ>(), BroadcastControllable {
......
......@@ -43,18 +43,6 @@ class GroupMessage(
inline fun Long.member(): Member = group[this]
/**
* 给这个消息事件的主体发送引用回复消息
* 对于好友消息事件, 这个方法将会给好友 ([subject]) 发送消息
* 对于群消息事件, 这个方法将会给群 ([subject]) 发送消息
*/
suspend inline fun quoteReply(message: MessageChain): MessageReceipt<Group> = reply(this.message.quote() + message)
suspend inline fun quoteReply(message: Message): MessageReceipt<Group> = reply(this.message.quote() + message)
suspend inline fun quoteReply(plain: String): MessageReceipt<Group> = reply(this.message.quote() + plain)
@JvmName("reply2")
suspend inline fun String.quoteReply(): MessageReceipt<Group> = quoteReply(this)
......
......@@ -91,27 +91,34 @@ abstract class MessagePacketBase<TSender : QQ, TSubject : Contact> : Packet, Bot
suspend inline fun MessageChain.reply(): MessageReceipt<TSubject> = reply(this)
// endregion
// region
/**
* 引用这个消息. 当且仅当消息为群消息时可用. 否则将会抛出 [IllegalArgumentException]
*/
inline fun MessageChain.quote(): MessageChain = this.quote(sender as? Member ?: error("only group message can be quoted"))
// endregion
// region 上传图片
suspend inline fun ExternalImage.upload(): Image = this.upload(subject)
// endregion
// region 发送图片
suspend inline fun ExternalImage.send() = this.sendTo(subject)
suspend inline fun ExternalImage.send(): MessageReceipt<TSubject> = this.sendTo(subject)
suspend inline fun Image.send() = this.sendTo(subject)
suspend inline fun Message.send() = this.sendTo(subject)
suspend inline fun String.send() = this.toMessage().sendTo(subject)
suspend inline fun Image.send(): MessageReceipt<TSubject> = this.sendTo(subject)
suspend inline fun Message.send(): MessageReceipt<TSubject> = this.sendTo(subject)
suspend inline fun String.send(): MessageReceipt<TSubject> = this.toMessage().sendTo(subject)
// endregion
/**
* 给这个消息事件的主体发送引用回复消息
* 对于好友消息事件, 这个方法将会给好友 ([subject]) 发送消息
* 对于群消息事件, 这个方法将会给群 ([subject]) 发送消息
*/
suspend inline fun quoteReply(message: MessageChain): MessageReceipt<TSubject> = reply(this.message.quote() + message)
suspend inline fun quoteReply(message: Message): MessageReceipt<TSubject> = reply(this.message.quote() + message)
suspend inline fun quoteReply(plain: String): MessageReceipt<TSubject> = reply(this.message.quote() + plain)
/**
* 引用这个消息. 当且仅当消息为群消息时可用. 否则将会抛出 [IllegalArgumentException]
*/
inline fun MessageChain.quote(): MessageChain = this.quote(sender)
operator fun <M : Message> get(at: Message.Key<M>): M {
return this.message[at]
}
......
......@@ -13,6 +13,7 @@ package net.mamoe.mirai.message.data
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.sendMessage
import net.mamoe.mirai.message.MessageReceipt
import kotlin.jvm.JvmSynthetic
/**
......@@ -108,4 +109,4 @@ interface SingleOnly : Message
/**
* 将 [this] 发送给指定联系人
*/
suspend inline fun Message.sendTo(contact: Contact) = contact.sendMessage(this)
\ No newline at end of file
suspend inline fun <C : Contact> Message.sendTo(contact: C): MessageReceipt<C> = contact.sendMessage(this)
\ No newline at end of file
......@@ -13,6 +13,7 @@
package net.mamoe.mirai.message.data
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.utils.MiraiInternalAPI
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
......@@ -32,10 +33,15 @@ class QuoteReply @MiraiInternalAPI constructor(val source: MessageSource) : Mess
* 引用这条消息.
* 返回 `[QuoteReply] + [At] + [PlainText]`(必要的结构)
*/
fun MessageChain.quote(sender: Member): MessageChain {
@UseExperimental(MiraiInternalAPI::class)
fun MessageChain.quote(sender: QQ): MessageChain {
this.firstOrNull<MessageSource>()?.let {
@UseExperimental(MiraiInternalAPI::class)
return QuoteReply(it) + sender.at() + " " // required
return if (it.groupId == 0L) {
QuoteReply(it) + " " // required
} else {
check(sender is Member) { "sender must be Member to quote a GroupMessage" }
QuoteReply(it) + sender.at() + " " // required
}
}
error("cannot find MessageSource")
}
......@@ -44,7 +50,12 @@ fun MessageChain.quote(sender: Member): MessageChain {
* 引用这条消息.
* 返回 `[QuoteReply] + [At] + [PlainText]`(必要的结构)
*/
fun MessageSource.quote(sender: Member): MessageChain {
@UseExperimental(MiraiInternalAPI::class)
return QuoteReply(this) + sender.at() + " " // required
@UseExperimental(MiraiInternalAPI::class)
fun MessageSource.quote(sender: QQ): MessageChain {
return if (this.groupId == 0L) {
QuoteReply(this) + " " // required
} else {
check(sender is Member) { "sender must be Member to quote a GroupMessage" }
QuoteReply(this) + sender.at() + " " // required
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment