Commit 1c1a053b authored by Him188's avatar Him188

Split Messages in separate files

parent 71218943
...@@ -5,6 +5,7 @@ package net.mamoe.mirai.contact ...@@ -5,6 +5,7 @@ package net.mamoe.mirai.contact
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.message.Message import net.mamoe.mirai.message.Message
import net.mamoe.mirai.message.MessageChain import net.mamoe.mirai.message.MessageChain
import net.mamoe.mirai.message.chain
import net.mamoe.mirai.message.singleChain import net.mamoe.mirai.message.singleChain
import net.mamoe.mirai.network.BotSession import net.mamoe.mirai.network.BotSession
import net.mamoe.mirai.withSession import net.mamoe.mirai.withSession
...@@ -40,7 +41,7 @@ interface Contact { ...@@ -40,7 +41,7 @@ interface Contact {
//这两个方法应写为扩展函数, 但为方便 import 还是写在这里 //这两个方法应写为扩展函数, 但为方便 import 还是写在这里
suspend fun sendMessage(plain: String) = sendMessage(plain.singleChain()) suspend fun sendMessage(plain: String) = sendMessage(plain.singleChain())
suspend fun sendMessage(message: Message) = sendMessage(message.singleChain()) suspend fun sendMessage(message: Message) = sendMessage(message.chain())
} }
/** /**
......
...@@ -11,6 +11,7 @@ import net.mamoe.mirai.contact.* ...@@ -11,6 +11,7 @@ import net.mamoe.mirai.contact.*
import net.mamoe.mirai.contact.data.Profile import net.mamoe.mirai.contact.data.Profile
import net.mamoe.mirai.message.Message import net.mamoe.mirai.message.Message
import net.mamoe.mirai.message.MessageChain import net.mamoe.mirai.message.MessageChain
import net.mamoe.mirai.message.chain
import net.mamoe.mirai.message.singleChain import net.mamoe.mirai.message.singleChain
import net.mamoe.mirai.network.protocol.tim.packet.action.RequestProfileDetailsPacket import net.mamoe.mirai.network.protocol.tim.packet.action.RequestProfileDetailsPacket
import net.mamoe.mirai.network.protocol.tim.packet.action.RequestProfileDetailsResponse import net.mamoe.mirai.network.protocol.tim.packet.action.RequestProfileDetailsResponse
...@@ -25,7 +26,7 @@ internal sealed class ContactImpl : Contact { ...@@ -25,7 +26,7 @@ internal sealed class ContactImpl : Contact {
//这两个方法应写为扩展函数, 但为方便 import 还是写在这里 //这两个方法应写为扩展函数, 但为方便 import 还是写在这里
override suspend fun sendMessage(plain: String) = sendMessage(plain.singleChain()) override suspend fun sendMessage(plain: String) = sendMessage(plain.singleChain())
override suspend fun sendMessage(message: Message) = sendMessage(message.singleChain()) override suspend fun sendMessage(message: Message) = sendMessage(message.chain())
} }
@Suppress("MemberVisibilityCanBePrivate", "CanBeParameter") @Suppress("MemberVisibilityCanBePrivate", "CanBeParameter")
......
@file:Suppress("EXPERIMENTAL_API_USAGE")
package net.mamoe.mirai.message
import net.mamoe.mirai.contact.QQ
/**
* At 一个人
*/
inline class At(val target: UInt) : Message {
constructor(target: QQ) : this(target.id)
override val stringValue: String get() = "[@$target]" // TODO: 2019/11/25 使用群名称进行 at. 因为手机端只会显示这个文字
override fun toString(): String = stringValue
companion object Key : Message.Key<At>
}
\ No newline at end of file
package net.mamoe.mirai.message
/**
* QQ 自带表情
*/
inline class Face(val id: FaceId) : Message {
override val stringValue: String get() = "[face${id.value}]"
override fun toString(): String = stringValue
companion object Key : Message.Key<Face>
}
\ No newline at end of file
@file:Suppress("EXPERIMENTAL_API_USAGE")
package net.mamoe.mirai.message
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.network.protocol.tim.packet.action.FriendImagePacket
import net.mamoe.mirai.utils.ExternalImage
import net.mamoe.mirai.utils.io.debugPrintln
import net.mamoe.mirai.utils.io.toUHexString
/**
* 图片消息. 在发送时将会区分群图片和好友图片发送.
* 由接收消息时构建, 可直接发送
*
* @param id 这个图片的 [ImageId]
*/
inline class Image(inline val id: ImageId) : Message {
override val stringValue: String get() = "[${id.value}]"
override fun toString(): String = stringValue
companion object Key : Message.Key<Image>
}
inline val Image.idValue: String get() = id.value
inline class ImageId0x06(override inline val value: String) : ImageId {
override fun toString(): String = "ImageId($value)"
}
/**
* 一般是群的图片的 id.
*/
class ImageId0x03 constructor(override inline val value: String, inline val uniqueId: UInt, inline val height: Int, inline val width: Int) : ImageId {
override fun toString(): String = "ImageId(value=$value, uniqueId=${uniqueId}, height=$height, width=$width)"
val md5: ByteArray
get() = this.value
.substringAfter("{").substringBefore("}")
.replace("-", "")
.chunked(2)
.map { (it[0] + it[1].toString()).toUByte(16).toByte() }
.toByteArray().also { check(it.size == 16); debugPrintln("image md5=" + it.toUHexString()); debugPrintln("imageId=$this") }
}
@Suppress("FunctionName")
fun ImageId(value: String): ImageId = ImageId0x06(value)
@Suppress("FunctionName")
fun ImageId(value: String, uniqueId: UInt, height: Int, width: Int): ImageId = ImageId0x03(value, uniqueId, height, width)
/**
* 图片的标识符. 由图片的数据产生.
* 对于群, [value] 类似于 `{F61593B5-5B98-1798-3F47-2A91D32ED2FC}.jpg`, 由图片文件 MD5 直接产生.
* 对于好友, [value] 类似于 `/01ee6426-5ff1-4cf0-8278-e8634d2909ef`, 由服务器返回.
*
* @see ExternalImage.groupImageId 群图片的 [ImageId] 获取
* @see FriendImagePacket 好友图片的 [ImageId] 获取
*/
interface ImageId {
val value: String
}
fun ImageId.checkLength() = check(value.length == 37 || value.length == 42) { "Illegal ImageId length" }
fun ImageId.requireLength() = require(value.length == 37 || value.length == 42) { "Illegal ImageId length" }
fun ImageId.image(): Image = Image(this)
suspend inline fun ImageId.sendTo(contact: Contact) = contact.sendMessage(this.image())
package net.mamoe.mirai.message
inline class PlainText(override val stringValue: String) : Message {
override operator fun contains(sub: String): Boolean = sub in stringValue
override fun toString(): String = stringValue
companion object Key : Message.Key<PlainText>
}
/**
* 构造 [PlainText]
*/
fun String.toMessage(): PlainText = PlainText(this)
/**
* 得到包含作为 [PlainText] 的 [this] 的 [MessageChain].
*
* @return 唯一成员且不可修改的 [SingleMessageChainImpl]
*
* @see SingleMessageChain
* @see SingleMessageChainImpl
*/
fun String.singleChain(): MessageChain = this.toMessage().chain()
\ No newline at end of file
...@@ -65,7 +65,7 @@ abstract class MessagePacketBase<TSubject : Contact> : EventPacket, BotEvent() { ...@@ -65,7 +65,7 @@ abstract class MessagePacketBase<TSubject : Contact> : EventPacket, BotEvent() {
*/ */
suspend inline fun reply(message: MessageChain) = subject.sendMessage(message) suspend inline fun reply(message: MessageChain) = subject.sendMessage(message)
suspend inline fun reply(message: Message) = subject.sendMessage(message.singleChain()) suspend inline fun reply(message: Message) = subject.sendMessage(message.chain())
suspend inline fun reply(plain: String) = subject.sendMessage(plain.toMessage()) suspend inline fun reply(plain: String) = subject.sendMessage(plain.toMessage())
@JvmName("reply1") @JvmName("reply1")
......
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