Commit 49c80c8e authored by Him188's avatar Him188

Fix bugs

parent 83e68a3e
@file:Suppress("unused")
package net.mamoe.mirai.message
import android.graphics.Bitmap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.network.protocol.tim.packet.action.OverFileSizeMaxException
import net.mamoe.mirai.utils.ExternalImage
import net.mamoe.mirai.utils.sendTo
import net.mamoe.mirai.utils.toExternalImage
import net.mamoe.mirai.utils.upload
import java.io.File
import java.io.IOException
import java.io.InputStream
/*
* 发送图片的一些扩展函数.
*/
/**
* 保存为临时文件然后调用 [File.toExternalImage]
*/
@Throws(IOException::class)
fun Bitmap.toExternalImage(): ExternalImage {
val file = createTempFile().apply { deleteOnExit() }
file.outputStream().use {
this.compress(Bitmap.CompressFormat.PNG, 100, it)
}
return file.toExternalImage()
}
/**
* 在 [IO] 中进行 [InputStream.toExternalImage]
*/
@Suppress("unused")
suspend fun InputStream.suspendToExternalImage() = withContext(IO) { toExternalImage() }
/**
* 在 [Dispatchers.IO] 中将图片发送到指定联系人. 会创建临时文件
* @throws OverFileSizeMaxException
*/
@Throws(OverFileSizeMaxException::class)
suspend fun Bitmap.sendTo(contact: Contact) = withContext(IO) { toExternalImage() }.sendTo(contact)
/**
* 在 [Dispatchers.IO] 中将图片上传后构造 [Image]. 会创建临时文件
* @throws OverFileSizeMaxException
*/
@Throws(OverFileSizeMaxException::class)
suspend fun Bitmap.upload(contact: Contact): Image = withContext(IO) { toExternalImage() }.upload(contact)
/**
* 在 [Dispatchers.IO] 中将图片发送到指定联系人. 会保存临时文件
* @throws OverFileSizeMaxException
*/
@Throws(OverFileSizeMaxException::class)
suspend inline fun Contact.sendImage(bitmap: Bitmap) = bitmap.sendTo(this)
/**
* 在 [Dispatchers.IO] 中将图片发送到指定联系人. 会保存临时文件
* @throws OverFileSizeMaxException
*/
@Throws(OverFileSizeMaxException::class)
suspend inline fun Contact.uploadImage(bitmap: Bitmap): Image = bitmap.upload(this)
\ No newline at end of file
...@@ -11,12 +11,8 @@ import net.mamoe.mirai.Bot ...@@ -11,12 +11,8 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.message.Image import net.mamoe.mirai.message.Image
import net.mamoe.mirai.network.protocol.tim.handler.DataPacketSocketAdapter import net.mamoe.mirai.network.protocol.tim.handler.DataPacketSocketAdapter
import net.mamoe.mirai.network.protocol.tim.packet.SessionKey import net.mamoe.mirai.network.protocol.tim.packet.SessionKey
import net.mamoe.mirai.utils.ExternalImage
import net.mamoe.mirai.utils.InternalAPI import net.mamoe.mirai.utils.InternalAPI
import net.mamoe.mirai.utils.toExternalImage
import java.io.File
import java.io.InputStream import java.io.InputStream
import java.io.OutputStream
/** /**
* Android 平台相关扩展. 详情查看 [BotSessionBase] * Android 平台相关扩展. 详情查看 [BotSessionBase]
...@@ -33,8 +29,6 @@ actual class BotSession actual constructor( ...@@ -33,8 +29,6 @@ actual class BotSession actual constructor(
suspend inline fun Image.downloadAsStream(): InputStream = download().inputStream() suspend inline fun Image.downloadAsStream(): InputStream = download().inputStream()
suspend inline fun Image.downloadAsBitmap(): Bitmap = withContext(Dispatchers.IO) { downloadAsStream().use { BitmapFactory.decodeStream(it) } } suspend inline fun Image.downloadAsBitmap(): Bitmap = withContext(Dispatchers.IO) { downloadAsStream().use { BitmapFactory.decodeStream(it) } }
suspend inline fun Image.downloadAsExternalImage(): ExternalImage = download().use { it.toExternalImage() } //suspend inline fun Image.downloadAsExternalImage(): ExternalImage = download().use { it.toExternalImage() }
suspend inline fun Image.downloadTo(file: File) = file.outputStream().use { downloadTo(it) }
suspend inline fun Image.downloadTo(output: OutputStream) = download().inputStream().use { input -> withContext(Dispatchers.IO) { input.copyTo(output) } }
} }
\ No newline at end of file
package net.mamoe.mirai.network.protocol.tim.packet.event package net.mamoe.mirai.network.protocol.tim.packet.event
import android.graphics.Bitmap
import kotlinx.io.core.Input import kotlinx.io.core.Input
import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.* import net.mamoe.mirai.message.*
...@@ -13,26 +12,26 @@ import java.net.URL ...@@ -13,26 +12,26 @@ import java.net.URL
* 平台相关扩展 * 平台相关扩展
*/ */
@UseExperimental(InternalAPI::class) @UseExperimental(InternalAPI::class)
actual sealed class MessagePacket<TSubject : Contact> : MessagePacketBase<TSubject>() { actual abstract class MessagePacket<TSubject : Contact> : MessagePacketBase<TSubject>() {
suspend inline fun uploadImage(image: Bitmap): Image = subject.uploadImage(image) // suspend inline fun uploadImage(image: Bitmap): Image = subject.uploadImage(image)
suspend inline fun uploadImage(image: URL): Image = subject.uploadImage(image) suspend inline fun uploadImage(image: URL): Image = subject.uploadImage(image)
suspend inline fun uploadImage(image: Input): Image = subject.uploadImage(image) suspend inline fun uploadImage(image: Input): Image = subject.uploadImage(image)
suspend inline fun uploadImage(image: InputStream): Image = subject.uploadImage(image) suspend inline fun uploadImage(image: InputStream): Image = subject.uploadImage(image)
suspend inline fun uploadImage(image: File): Image = subject.uploadImage(image) suspend inline fun uploadImage(image: File): Image = subject.uploadImage(image)
suspend inline fun sendImage(image: Bitmap) = subject.sendImage(image) // suspend inline fun sendImage(image: Bitmap) = subject.sendImage(image)
suspend inline fun sendImage(image: URL) = subject.sendImage(image) suspend inline fun sendImage(image: URL) = subject.sendImage(image)
suspend inline fun sendImage(image: Input) = subject.sendImage(image) suspend inline fun sendImage(image: Input) = subject.sendImage(image)
suspend inline fun sendImage(image: InputStream) = subject.sendImage(image) suspend inline fun sendImage(image: InputStream) = subject.sendImage(image)
suspend inline fun sendImage(image: File) = subject.sendImage(image) suspend inline fun sendImage(image: File) = subject.sendImage(image)
suspend inline fun Bitmap.upload(): Image = upload(subject) // suspend inline fun Bitmap.upload(): Image = upload(subject)
suspend inline fun URL.uploadAsImage(): Image = uploadAsImage(subject) suspend inline fun URL.uploadAsImage(): Image = uploadAsImage(subject)
suspend inline fun Input.uploadAsImage(): Image = uploadAsImage(subject) suspend inline fun Input.uploadAsImage(): Image = uploadAsImage(subject)
suspend inline fun InputStream.uploadAsImage(): Image = uploadAsImage(subject) suspend inline fun InputStream.uploadAsImage(): Image = uploadAsImage(subject)
suspend inline fun File.uploadAsImage(): Image = uploadAsImage(subject) suspend inline fun File.uploadAsImage(): Image = uploadAsImage(subject)
suspend inline fun Bitmap.send() = sendTo(subject) // suspend inline fun Bitmap.send() = sendTo(subject)
suspend inline fun URL.sendAsImage() = sendAsImageTo(subject) suspend inline fun URL.sendAsImage() = sendAsImageTo(subject)
suspend inline fun Input.sendAsImage() = sendAsImageTo(subject) suspend inline fun Input.sendAsImage() = sendAsImageTo(subject)
suspend inline fun InputStream.sendAsImage() = sendAsImageTo(subject) suspend inline fun InputStream.sendAsImage() = sendAsImageTo(subject)
......
...@@ -203,17 +203,17 @@ data class Profile( ...@@ -203,17 +203,17 @@ data class Profile(
override fun toString(): String = "Profile(qq=$qq, " + override fun toString(): String = "Profile(qq=$qq, " +
"nickname=$nickname, " + "nickname=$nickname, " +
"gender=$gender, " + "gender=$gender, " +
(englishName?.plus("englishName=$englishName, ") ?: "") + (englishName?.let { "englishName=$englishName, " } ?: "") +
(chineseName?.plus("chineseName=$chineseName, ") ?: "") + (chineseName?.let { "chineseName=$chineseName, " } ?: "") +
(qAge?.toString()?.plus("qAge=$qAge, ") ?: "") + (qAge?.toString()?.let { "qAge=$qAge, " } ?: "") +
(zipCode?.plus("zipCode=$zipCode, ") ?: "") + (zipCode?.let { "zipCode=$zipCode, " } ?: "") +
(phone?.plus("phone=$phone, ") ?: "") + (phone?.let { "phone=$phone, " } ?: "") +
(birthday?.toString()?.plus("birthday=$birthday, ") ?: "") + (birthday?.toString()?.let { "birthday=$birthday, " } ?: "") +
(personalStatement?.plus("personalStatement=$personalStatement, ") ?: "") + (personalStatement?.let { "personalStatement=$personalStatement, " } ?: "") +
(school?.plus("school=$school, ") ?: "") + (school?.let { "school=$school, " } ?: "") +
(homepage?.plus("homepage=$homepage, ") ?: "") + (homepage?.let { "homepage=$homepage, " } ?: "") +
(email?.plus("email=$email, ") ?: "") + (email?.let { "email=$email, " } ?: "") +
(company?.plus("company=$company?,") ?: "") + (company?.let { "company=$company?," } ?: "") +
")"// 最终会是 ", )", 但这并不影响什么. ")"// 最终会是 ", )", 但这并不影响什么.
} }
......
...@@ -4,9 +4,11 @@ package net.mamoe.mirai.message ...@@ -4,9 +4,11 @@ package net.mamoe.mirai.message
import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.QQ import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.network.protocol.tim.packet.action.FriendImagePacket
import net.mamoe.mirai.utils.ExternalImage import net.mamoe.mirai.utils.ExternalImage
import kotlin.js.JsName import kotlin.js.JsName
import kotlin.jvm.Volatile import kotlin.jvm.Volatile
import kotlin.reflect.KProperty
// region Message Base // region Message Base
/** /**
...@@ -156,6 +158,8 @@ inline class Image(inline val id: ImageId) : Message { ...@@ -156,6 +158,8 @@ inline class Image(inline val id: ImageId) : Message {
companion object Key : Message.Key<Image> companion object Key : Message.Key<Image>
} }
inline val Image.idValue: String get() = id.value
/** /**
* 图片的标识符. 由图片的数据产生. * 图片的标识符. 由图片的数据产生.
* 对于群, [value] 类似于 `{F61593B5-5B98-1798-3F47-2A91D32ED2FC}.jpg`, 由图片文件 MD5 直接产生. * 对于群, [value] 类似于 `{F61593B5-5B98-1798-3F47-2A91D32ED2FC}.jpg`, 由图片文件 MD5 直接产生.
...@@ -171,7 +175,7 @@ fun ImageId.requireLength() = require(value.length == 37 || value.length == 42) ...@@ -171,7 +175,7 @@ fun ImageId.requireLength() = require(value.length == 37 || value.length == 42)
fun ImageId.image(): Image = Image(this) fun ImageId.image(): Image = Image(this)
suspend fun ImageId.sendTo(contact: Contact) = contact.sendMessage(this.image()) suspend inline fun ImageId.sendTo(contact: Contact) = contact.sendMessage(this.image())
// endregion // endregion
...@@ -281,18 +285,18 @@ fun List<Message>.toMessageChain(): MessageChain = MessageChain(this) ...@@ -281,18 +285,18 @@ fun List<Message>.toMessageChain(): MessageChain = MessageChain(this)
/** /**
* 获取第一个 [M] 类型的 [Message] 实例 * 获取第一个 [M] 类型的 [Message] 实例
*/ */
inline fun <reified M : Message> MessageChain.firstOrNull(): Message? = this.firstOrNull { M::class.isInstance(it) } inline fun <reified M : Message> MessageChain.firstOrNull(): Message? = this.firstOrNull { it is M }
/** /**
* 获取第一个 [M] 类型的 [Message] 实例 * 获取第一个 [M] 类型的 [Message] 实例
* @throws [NoSuchElementException] 如果找不到该类型的实例 * @throws [NoSuchElementException] 如果找不到该类型的实例
*/ */
inline fun <reified M : Message> MessageChain.first(): Message = this.first { M::class.isInstance(it) } inline fun <reified M : Message> MessageChain.first(): Message = this.first { it is M }
/** /**
* 获取第一个 [M] 类型的 [Message] 实例 * 获取第一个 [M] 类型的 [Message] 实例
*/ */
inline fun <reified M : Message> MessageChain.any(): Boolean = this.firstOrNull { M::class.isInstance(it) } !== null inline fun <reified M : Message> MessageChain.any(): Boolean = this.firstOrNull { it is M } !== null
/** /**
...@@ -355,8 +359,11 @@ interface MessageChain : Message, MutableList<Message> { ...@@ -355,8 +359,11 @@ interface MessageChain : Message, MutableList<Message> {
*/ */
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
operator fun <M : Message> get(key: Message.Key<M>): M = first(key) operator fun <M : Message> get(key: Message.Key<M>): M = first(key)
} }
inline operator fun <reified T : Message> MessageChain.getValue(thisRef: Any?, property: KProperty<*>): T = this.first<T>() as T
/** /**
* 空的 [Message]. * 空的 [Message].
* *
......
...@@ -67,7 +67,7 @@ object RequestProfileDetailsPacket : SessionPacketFactory<RequestProfileDetailsR ...@@ -67,7 +67,7 @@ object RequestProfileDetailsPacket : SessionPacketFactory<RequestProfileDetailsR
nickname = map[0x4E22u]?.encodeToString() ?: "",//error("Cannot determine nickname") nickname = map[0x4E22u]?.encodeToString() ?: "",//error("Cannot determine nickname")
englishName = map[0x4E54u]?.encodeToString(), englishName = map[0x4E54u]?.encodeToString(),
chineseName = map[0x4E2Au]?.encodeToString(), chineseName = map[0x4E2Au]?.encodeToString(),
qAge = map[0x6597u]?.toUInt()?.toInt(), qAge = map[0x6597u]?.get(0)?.toInt(),
zipCode = map[0x4E25u]?.encodeToString(), zipCode = map[0x4E25u]?.encodeToString(),
phone = map[0x4E27u]?.encodeToString(), phone = map[0x4E27u]?.encodeToString(),
gender = when (map[0x4E29u]?.let { it[0] }?.toUInt()) { gender = when (map[0x4E29u]?.let { it[0] }?.toUInt()) {
......
...@@ -227,6 +227,7 @@ object ImageOverFileSizeMax : ImageResponse { ...@@ -227,6 +227,7 @@ object ImageOverFileSizeMax : ImageResponse {
@AnnotatedId(KnownPacketId.FRIEND_IMAGE_ID) @AnnotatedId(KnownPacketId.FRIEND_IMAGE_ID)
@PacketVersion(date = "2019.11.16", timVersion = "2.3.2 (21173)") @PacketVersion(date = "2019.11.16", timVersion = "2.3.2 (21173)")
object FriendImagePacket : SessionPacketFactory<ImageResponse>() { object FriendImagePacket : SessionPacketFactory<ImageResponse>() {
@Suppress("FunctionName")
fun RequestImageId( fun RequestImageId(
bot: UInt, bot: UInt,
sessionKey: SessionKey, sessionKey: SessionKey,
...@@ -272,6 +273,7 @@ object FriendImagePacket : SessionPacketFactory<ImageResponse>() { ...@@ -272,6 +273,7 @@ object FriendImagePacket : SessionPacketFactory<ImageResponse>() {
} }
@Suppress("FunctionName")
fun RequestImageLink( fun RequestImageLink(
bot: UInt, bot: UInt,
sessionKey: SessionKey, sessionKey: SessionKey,
...@@ -416,7 +418,7 @@ object FriendImagePacket : SessionPacketFactory<ImageResponse>() { ...@@ -416,7 +418,7 @@ object FriendImagePacket : SessionPacketFactory<ImageResponse>() {
discardExact(1) discardExact(1)
discardExact(2)// [A4 04] 后文长度 discardExact(2)// [A4 04] 后文长度
check(readUByte().toUInt() == 0x0Au) { "Illegal identity. Required 0x0Au" } check(readUByte().toUInt() == 0x0Au) { "Illegal identity. Required 0x0Au" }
val imageId = ImageId(readString(readUByte().toInt())) /* val imageId = */ImageId(readString(readUByte().toInt()))
check(readUByte().toUInt() == 0x18u) { "Illegal identity. Required 0x18u" } check(readUByte().toUInt() == 0x18u) { "Illegal identity. Required 0x18u" }
check(readUShort().toUInt() == 0x0032u) { "Illegal identity. Required 0x0032u" } check(readUShort().toUInt() == 0x0032u) { "Illegal identity. Required 0x0032u" }
......
...@@ -23,12 +23,13 @@ import net.mamoe.mirai.utils.io.printTLVMap ...@@ -23,12 +23,13 @@ import net.mamoe.mirai.utils.io.printTLVMap
import net.mamoe.mirai.utils.io.read import net.mamoe.mirai.utils.io.read
import net.mamoe.mirai.utils.io.readTLVMap import net.mamoe.mirai.utils.io.readTLVMap
import net.mamoe.mirai.utils.io.readUShortLVByteArray import net.mamoe.mirai.utils.io.readUShortLVByteArray
import kotlin.jvm.JvmName
/** /**
* 平台相关扩展 * 平台相关扩展
*/ */
@UseExperimental(InternalAPI::class) @UseExperimental(InternalAPI::class)
expect sealed class MessagePacket<TSubject : Contact>() : MessagePacketBase<TSubject> expect abstract class MessagePacket<TSubject : Contact>() : MessagePacketBase<TSubject>
@InternalAPI @InternalAPI
abstract class MessagePacketBase<TSubject : Contact> : EventPacket, BotEvent() { abstract class MessagePacketBase<TSubject : Contact> : EventPacket, BotEvent() {
...@@ -63,8 +64,17 @@ abstract class MessagePacketBase<TSubject : Contact> : EventPacket, BotEvent() { ...@@ -63,8 +64,17 @@ 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 fun reply(message: Message) = subject.sendMessage(message.singleChain()) suspend inline fun reply(message: Message) = subject.sendMessage(message.singleChain())
suspend fun reply(plain: String) = subject.sendMessage(plain.toMessage()) suspend inline fun reply(plain: String) = subject.sendMessage(plain.toMessage())
@JvmName("reply1")
suspend inline fun String.reply() = reply(this)
@JvmName("reply1")
suspend inline fun Message.reply() = reply(this)
@JvmName("reply1")
suspend inline fun MessageChain.reply() = reply(this)
suspend inline fun ExternalImage.send() = this.sendTo(subject) suspend inline fun ExternalImage.send() = this.sendTo(subject)
......
...@@ -2,20 +2,28 @@ ...@@ -2,20 +2,28 @@
package net.mamoe.mirai.network.protocol.tim.packet.event package net.mamoe.mirai.network.protocol.tim.packet.event
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.io.core.Input import kotlinx.io.core.Input
import kotlinx.io.core.use
import kotlinx.io.streams.inputStream
import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.* import net.mamoe.mirai.message.*
import net.mamoe.mirai.utils.ExternalImage
import net.mamoe.mirai.utils.InternalAPI import net.mamoe.mirai.utils.InternalAPI
import net.mamoe.mirai.utils.toExternalImage
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
import java.io.File import java.io.File
import java.io.InputStream import java.io.InputStream
import java.io.OutputStream
import java.net.URL import java.net.URL
import javax.imageio.ImageIO
/** /**
* 平台相关扩展 * JVM 平台相关扩展
*/ */
@UseExperimental(InternalAPI::class) @UseExperimental(InternalAPI::class)
actual sealed class MessagePacket<TSubject : Contact> : MessagePacketBase<TSubject>() { actual abstract class MessagePacket<TSubject : Contact> : MessagePacketBase<TSubject>() {
suspend inline fun uploadImage(image: BufferedImage): Image = subject.uploadImage(image) suspend inline fun uploadImage(image: BufferedImage): Image = subject.uploadImage(image)
suspend inline fun uploadImage(image: URL): Image = subject.uploadImage(image) suspend inline fun uploadImage(image: URL): Image = subject.uploadImage(image)
suspend inline fun uploadImage(image: Input): Image = subject.uploadImage(image) suspend inline fun uploadImage(image: Input): Image = subject.uploadImage(image)
...@@ -39,4 +47,12 @@ actual sealed class MessagePacket<TSubject : Contact> : MessagePacketBase<TSubje ...@@ -39,4 +47,12 @@ actual sealed class MessagePacket<TSubject : Contact> : MessagePacketBase<TSubje
suspend inline fun Input.sendAsImage() = sendAsImageTo(subject) suspend inline fun Input.sendAsImage() = sendAsImageTo(subject)
suspend inline fun InputStream.sendAsImage() = sendAsImageTo(subject) suspend inline fun InputStream.sendAsImage() = sendAsImageTo(subject)
suspend inline fun File.sendAsImage() = sendAsImageTo(subject) suspend inline fun File.sendAsImage() = sendAsImageTo(subject)
suspend inline fun Image.downloadTo(file: File): Long = file.outputStream().use { downloadTo(it) }
suspend inline fun Image.downloadTo(output: OutputStream): Long =
download().inputStream().use { input -> withContext(Dispatchers.IO) { input.copyTo(output) } }
suspend inline fun Image.downloadAsStream(): InputStream = download().inputStream()
suspend inline fun Image.downloadAsExternalImage(): ExternalImage = withContext(Dispatchers.IO) { download().toExternalImage() }
suspend inline fun Image.downloadAsBufferedImage(): BufferedImage = withContext(Dispatchers.IO) { ImageIO.read(downloadAsStream()) }
} }
\ No newline at end of file
...@@ -121,7 +121,7 @@ suspend fun InputStream.suspendToExternalImage(): ExternalImage = withContext(IO ...@@ -121,7 +121,7 @@ suspend fun InputStream.suspendToExternalImage(): ExternalImage = withContext(IO
fun Input.toExternalImage(): ExternalImage { fun Input.toExternalImage(): ExternalImage {
val file = createTempFile().apply { deleteOnExit() } val file = createTempFile().apply { deleteOnExit() }
file.outputStream().use { file.outputStream().use {
this.asStream() this.asStream().transferTo(it)
} }
return file.toExternalImage() return file.toExternalImage()
} }
......
...@@ -12,14 +12,12 @@ import net.mamoe.mirai.event.Subscribable ...@@ -12,14 +12,12 @@ import net.mamoe.mirai.event.Subscribable
import net.mamoe.mirai.event.subscribeAlways import net.mamoe.mirai.event.subscribeAlways
import net.mamoe.mirai.event.subscribeMessages import net.mamoe.mirai.event.subscribeMessages
import net.mamoe.mirai.message.Image import net.mamoe.mirai.message.Image
import net.mamoe.mirai.message.getValue
import net.mamoe.mirai.message.sendAsImageTo import net.mamoe.mirai.message.sendAsImageTo
import net.mamoe.mirai.network.protocol.tim.packet.action.download
import net.mamoe.mirai.network.protocol.tim.packet.action.downloadAsByteArray
import net.mamoe.mirai.network.protocol.tim.packet.action.downloadTo
import net.mamoe.mirai.network.protocol.tim.packet.event.FriendMessage import net.mamoe.mirai.network.protocol.tim.packet.event.FriendMessage
import net.mamoe.mirai.network.protocol.tim.packet.login.requireSuccess import net.mamoe.mirai.network.protocol.tim.packet.login.requireSuccess
import net.mamoe.mirai.utils.currentTime
import java.io.File import java.io.File
import java.util.*
import javax.swing.filechooser.FileSystemView import javax.swing.filechooser.FileSystemView
import kotlin.random.Random import kotlin.random.Random
...@@ -62,16 +60,17 @@ suspend fun main() { ...@@ -62,16 +60,17 @@ suspend fun main() {
bot.getQQ(account.toUInt()) bot.getQQ(account.toUInt())
} else { } else {
sender sender
}.profile.await().toString() }.profile.await().toString().reply()
} }
has<Image> { has<Image> {
if (this is FriendMessage) { if (this is FriendMessage) {
withContext(IO) { withContext(IO) {
reply(message[Image] + " downloading") val image: Image by message
sender.downloadTo(message[Image], File(System.getProperty("user.dir"), "testDownloadedImage${currentTime}.png").also { it.createNewFile() }) reply(image + " downloading")
reply(message[Image].id.value + " downloaded") image.downloadTo(newTestTempFile(suffix = ".png").also { reply("Temp file: ${it.absolutePath}") })
reply(image.id.value + " downloaded")
} }
} }
} }
...@@ -103,4 +102,7 @@ suspend fun main() { ...@@ -103,4 +102,7 @@ suspend fun main() {
} }
bot.network.awaitDisconnection()//等到直到断开连接 bot.network.awaitDisconnection()//等到直到断开连接
} }
\ No newline at end of file
private fun newTestTempFile(filename: String = "${UUID.randomUUID()}", suffix: String = ".tmp"): File =
File(System.getProperty("user.dir"), filename + suffix).also { it.createNewFile(); it.deleteOnExit() }
\ 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