Commit a64123bd authored by Him188's avatar Him188

Cleanup

parent 69954add
@file:Suppress("unused")
package net.mamoe.mirai package net.mamoe.mirai
import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.Group
...@@ -64,10 +66,10 @@ fun Bot.debugPacket(packet: ServerPacket) { ...@@ -64,10 +66,10 @@ fun Bot.debugPacket(packet: ServerPacket) {
private fun print(bot: Bot, value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) { private fun print(bot: Bot, value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) {
val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date()) val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date())
kotlin.io.println("$color[Mirai] $s #R${bot.id}: $value") println("$color[Mirai] $s #R${bot.id}: $value")
} }
private fun print(value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) { private fun print(value: String?, color: LoggerTextFormat = LoggerTextFormat.WHITE) {
val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date()) val s = SimpleDateFormat("MM-dd HH:mm:ss").format(Date())
kotlin.io.println("$color[Mirai] $s : $value") println("$color[Mirai] $s : $value")
} }
\ No newline at end of file
package net.mamoe.mirai.contact package net.mamoe.mirai.contact
import kotlinx.coroutines.CompletableDeferred
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.message.* import net.mamoe.mirai.message.Message
import net.mamoe.mirai.network.LoginSession import net.mamoe.mirai.message.MessageChain
import net.mamoe.mirai.message.PlainText
import net.mamoe.mirai.message.toChain
/** /**
* 联系人. * 联系人.
...@@ -18,13 +19,6 @@ abstract class Contact internal constructor(val bot: Bot, val number: Long) { ...@@ -18,13 +19,6 @@ abstract class Contact internal constructor(val bot: Bot, val number: Long) {
abstract suspend fun sendMessage(message: MessageChain) abstract suspend fun sendMessage(message: MessageChain)
/**
* 上传图片
*/
suspend fun uploadImage(session: LoginSession, image: UnsolvedImage): CompletableDeferred<Unit> {
return image.upload(session, this)
}
suspend fun sendMessage(message: Message) { suspend fun sendMessage(message: Message) {
if (message is MessageChain) { if (message is MessageChain) {
return sendMessage(message) return sendMessage(message)
......
...@@ -10,6 +10,7 @@ import net.mamoe.mirai.network.protocol.tim.packet.image.ServerTryGetImageIDFail ...@@ -10,6 +10,7 @@ import net.mamoe.mirai.network.protocol.tim.packet.image.ServerTryGetImageIDFail
import net.mamoe.mirai.network.protocol.tim.packet.image.ServerTryGetImageIDResponsePacket import net.mamoe.mirai.network.protocol.tim.packet.image.ServerTryGetImageIDResponsePacket
import net.mamoe.mirai.network.protocol.tim.packet.image.ServerTryGetImageIDSuccessPacket import net.mamoe.mirai.network.protocol.tim.packet.image.ServerTryGetImageIDSuccessPacket
import net.mamoe.mirai.network.protocol.tim.packet.md5 import net.mamoe.mirai.network.protocol.tim.packet.md5
import net.mamoe.mirai.qqNumber
import net.mamoe.mirai.utils.ImageNetworkUtils import net.mamoe.mirai.utils.ImageNetworkUtils
import net.mamoe.mirai.utils.toByteArray import net.mamoe.mirai.utils.toByteArray
import net.mamoe.mirai.utils.toUHexString import net.mamoe.mirai.utils.toUHexString
...@@ -31,7 +32,7 @@ class UnsolvedImage(private val filename: String, val image: BufferedImage) { ...@@ -31,7 +32,7 @@ class UnsolvedImage(private val filename: String, val image: BufferedImage) {
suspend fun upload(session: LoginSession, contact: Contact): CompletableDeferred<Unit> { suspend fun upload(session: LoginSession, contact: Contact): CompletableDeferred<Unit> {
return session.expectPacket<ServerTryGetImageIDResponsePacket> { return session.expectPacket<ServerTryGetImageIDResponsePacket> {
toSend { ClientTryGetImageIDPacket(session.bot.account.qqNumber, session.sessionKey, contact.number, image) } toSend { ClientTryGetImageIDPacket(session.bot.qqNumber, session.sessionKey, contact.number, image) }
onExpect { onExpect {
when (it) { when (it) {
...@@ -42,7 +43,7 @@ class UnsolvedImage(private val filename: String, val image: BufferedImage) { ...@@ -42,7 +43,7 @@ class UnsolvedImage(private val filename: String, val image: BufferedImage) {
is ServerTryGetImageIDSuccessPacket -> { is ServerTryGetImageIDSuccessPacket -> {
val data = image.toByteArray() val data = image.toByteArray()
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
if (!ImageNetworkUtils.postImage(it.uKey.toUHexString(), data.size, session.bot.account.qqNumber, contact.number, data)) { if (!ImageNetworkUtils.postImage(it.uKey.toUHexString(), data.size, session.bot.qqNumber, contact.number, data)) {
throw RuntimeException("cannot upload image") throw RuntimeException("cannot upload image")
} }
} }
......
...@@ -55,7 +55,6 @@ class LoginSession( ...@@ -55,7 +55,6 @@ class LoginSession(
* *
* @param P 期待的包 * @param P 期待的包
* @param handlerTemporary 处理器. * @param handlerTemporary 处理器.
* @return future. 可进行超时处理
*/ */
@JvmSynthetic @JvmSynthetic
suspend inline fun <reified P : ServerPacket> expectPacket(handlerTemporary: TemporaryPacketHandler<P>.() -> Unit): CompletableDeferred<Unit> { suspend inline fun <reified P : ServerPacket> expectPacket(handlerTemporary: TemporaryPacketHandler<P>.() -> Unit): CompletableDeferred<Unit> {
...@@ -79,7 +78,6 @@ class LoginSession( ...@@ -79,7 +78,6 @@ class LoginSession(
* @param P 期待的包 * @param P 期待的包
* @param toSend 将要发送的包 * @param toSend 将要发送的包
* @param handler 处理期待的包 * @param handler 处理期待的包
* @return future. 可进行超时处理
*/ */
@JvmSynthetic @JvmSynthetic
suspend inline fun <reified P : ServerPacket> expectPacket(toSend: ClientPacket, noinline handler: suspend (P) -> Unit): CompletableDeferred<Unit> { suspend inline fun <reified P : ServerPacket> expectPacket(toSend: ClientPacket, noinline handler: suspend (P) -> Unit): CompletableDeferred<Unit> {
......
...@@ -125,7 +125,7 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler { ...@@ -125,7 +125,7 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler {
return return
} }
withContext(NetworkScope.coroutineContext + CoroutineExceptionHandler { _, e -> e.printStackTrace() }) { withContext(CoroutineExceptionHandler { _, e -> e.printStackTrace() }) {
launch(this.coroutineContext) { launch(this.coroutineContext) {
loginHandler.onPacketReceived(packet) loginHandler.onPacketReceived(packet)
} }
......
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