From 2b95bea8da0776b22b22cdf6045735a909277af9 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 13 Apr 2020 10:27:22 +0800 Subject: [PATCH] Unified logMessageReceived --- .../net/mamoe/mirai/qqandroid/contact/util.kt | 20 ++++++++++++++++++- .../network/QQAndroidBotNetworkHandler.kt | 16 +++------------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/util.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/util.kt index 589370925..2fc27621f 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/util.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/util.kt @@ -14,7 +14,7 @@ import net.mamoe.mirai.contact.QQ import net.mamoe.mirai.event.broadcast import net.mamoe.mirai.event.events.EventCancelledException import net.mamoe.mirai.event.events.MessageSendEvent -import net.mamoe.mirai.message.MessageReceipt +import net.mamoe.mirai.message.* import net.mamoe.mirai.message.data.LongMessage import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.QuoteReply @@ -28,6 +28,7 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc import net.mamoe.mirai.utils.LockFreeLinkedList import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiInternalAPI +import net.mamoe.mirai.utils.verbose @OptIn(MiraiInternalAPI::class) internal suspend fun QQ.sendMessageImpl(message: Message): MessageReceipt { @@ -58,6 +59,23 @@ internal fun Contact.logMessageSent(message: Message) { } } +@OptIn(MiraiInternalAPI::class, MiraiExperimentalAPI::class) +internal fun ContactMessage.logMessageReceived() { + when (this) { + is GroupMessage -> bot.logger.verbose { + "[${group.name.singleLine()}(${group.id})] ${senderName.singleLine()}(${sender.id}) -> ${message.toString() + .singleLine()}" + } + is TempMessage -> bot.logger.verbose { + "[${group.name.singleLine()}(${group.id})] ${senderName.singleLine()}(Temp ${sender.id}) -> ${message.toString() + .singleLine()}" + } + is FriendMessage -> bot.logger.verbose { + "${sender.nick.singleLine()}(${sender.id}) -> ${message.toString().singleLine()}" + } + } +} + internal fun String.singleLine(): String { return this.replace("\n", """\n""").replace("\r", "") diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt index 5cd2638f4..16c408dff 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt @@ -20,16 +20,12 @@ import kotlinx.io.core.use import net.mamoe.mirai.event.* import net.mamoe.mirai.event.events.BotOfflineEvent import net.mamoe.mirai.event.events.BotOnlineEvent -import net.mamoe.mirai.message.FriendMessage -import net.mamoe.mirai.message.GroupMessage +import net.mamoe.mirai.message.ContactMessage import net.mamoe.mirai.network.BotNetworkHandler import net.mamoe.mirai.network.UnsupportedSMSLoginException import net.mamoe.mirai.network.WrongPasswordException import net.mamoe.mirai.qqandroid.QQAndroidBot -import net.mamoe.mirai.qqandroid.contact.FriendInfoImpl -import net.mamoe.mirai.qqandroid.contact.GroupImpl -import net.mamoe.mirai.qqandroid.contact.QQImpl -import net.mamoe.mirai.qqandroid.contact.singleLine +import net.mamoe.mirai.qqandroid.contact.* import net.mamoe.mirai.qqandroid.event.PacketReceivedEvent import net.mamoe.mirai.qqandroid.network.protocol.data.jce.StTroopNum import net.mamoe.mirai.qqandroid.network.protocol.data.proto.MsgSvc @@ -451,13 +447,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler is Packet.NoLog -> { // nothing to do } - is GroupMessage -> bot.logger.verbose { - "[${packet.group.name}(${packet.group.id})] ${packet.senderName}(${packet.sender.id}) -> ${packet.message.toString() - .singleLine()}" - } - is FriendMessage -> bot.logger.verbose { - "${packet.sender.nick}(${packet.sender.id}) -> ${packet.message.toString().singleLine()}" - } + is ContactMessage -> packet.logMessageReceived() is Event -> bot.logger.verbose { "Event: ${packet.toString().singleLine()}" } else -> logger.verbose { "Packet: ${packet.toString().singleLine()}" } }