Unified message logs

This commit is contained in:
Him188 2020-04-06 20:33:26 +08:00
parent c722e97f07
commit 560c46ede4
4 changed files with 42 additions and 57 deletions

View File

@ -279,6 +279,14 @@ internal class GroupImpl(
override suspend fun sendMessage(message: Message): MessageReceipt<Group> {
check(!isBotMuted) { throw BotIsBeingMutedException(this) }
return sendMessageImpl(message).also {
logMessageSent(message)
}
}
@OptIn(MiraiExperimentalAPI::class)
private suspend fun sendMessageImpl(message: Message): MessageReceipt<Group> {
val msg: MessageChain
if (message !is LongMessage) {
@ -322,7 +330,13 @@ internal class GroupImpl(
if (response is MessageSvc.PbSendMsg.Response.Failed) {
when (response.resultType) {
120 -> error("bot is being muted.")
34 -> error("internal error: send message failed, illegal arguments: $response")
34 -> {
kotlin.runCatching { // allow retry once
return bot.lowLevelSendLongGroupMessage(id, msg)
}.getOrElse {
throw IllegalStateException("internal error: send message failed(34)", it)
}
}
else -> error("send message failed: $response")
}
}

View File

@ -19,19 +19,15 @@ import net.mamoe.mirai.data.MemberInfo
import net.mamoe.mirai.data.PreviousNameList
import net.mamoe.mirai.data.Profile
import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.event.events.MemberCardChangeEvent
import net.mamoe.mirai.event.events.MemberLeaveEvent
import net.mamoe.mirai.event.events.MemberSpecialTitleChangeEvent
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.OfflineFriendImage
import net.mamoe.mirai.message.data.QuoteReply
import net.mamoe.mirai.message.data.asMessageChain
import net.mamoe.mirai.qqandroid.QQAndroidBot
import net.mamoe.mirai.qqandroid.message.MessageSourceToFriendImpl
import net.mamoe.mirai.qqandroid.message.ensureSequenceIdAvailable
import net.mamoe.mirai.qqandroid.message.firstIsInstanceOrNull
import net.mamoe.mirai.qqandroid.network.protocol.data.jce.StTroopMemberInfo
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.TroopManagement
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc
import net.mamoe.mirai.utils.*
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.JvmSynthetic
@ -59,27 +55,13 @@ internal class MemberImpl constructor(
@MiraiExperimentalAPI
override suspend fun queryRemark(): FriendNameRemark = qq.queryRemark()
@OptIn(MiraiInternalAPI::class)
@JvmSynthetic
@Suppress("DuplicatedCode")
@Suppress("DuplicatedCode", "UNCHECKED_CAST")
override suspend fun sendMessage(message: Message): MessageReceipt<Member> {
val event = MessageSendEvent.FriendMessageSendEvent(this, message.asMessageChain()).broadcast()
if (event.isCancelled) {
throw EventCancelledException("cancelled by FriendMessageSendEvent")
}
lateinit var source: MessageSourceToFriendImpl
event.message.firstIsInstanceOrNull<QuoteReply>()?.source?.ensureSequenceIdAvailable()
bot.network.run {
check(
MessageSvc.PbSendMsg.createToFriend(
bot.client,
this@MemberImpl,
event.message
) {
source = it
}.sendAndExpect<MessageSvc.PbSendMsg.Response>() is MessageSvc.PbSendMsg.Response.SUCCESS
) { "send message failed" }
}
return MessageReceipt(source, this, null)
return sendMessageImpl(message).also {
logMessageSent(message)
} as MessageReceipt<Member>
}
@JvmSynthetic
@ -162,7 +144,7 @@ internal class MemberImpl constructor(
net.mamoe.mirai.event.events.MemberMuteEvent(this@MemberImpl, durationSeconds, null).broadcast()
}
private fun checkBotPermissionHigherThanThis(){
private fun checkBotPermissionHigherThanThis() {
check(group.botPermission > this.permission) {
throw PermissionDeniedException(
"`kick` operation requires bot to have a higher permission than the target member, " +

View File

@ -26,20 +26,13 @@ import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.event.events.BeforeImageUploadEvent
import net.mamoe.mirai.event.events.EventCancelledException
import net.mamoe.mirai.event.events.ImageUploadEvent
import net.mamoe.mirai.event.events.MessageSendEvent
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.OfflineFriendImage
import net.mamoe.mirai.message.data.QuoteReply
import net.mamoe.mirai.message.data.asMessageChain
import net.mamoe.mirai.qqandroid.QQAndroidBot
import net.mamoe.mirai.qqandroid.message.MessageSourceToFriendImpl
import net.mamoe.mirai.qqandroid.message.ensureSequenceIdAvailable
import net.mamoe.mirai.qqandroid.message.firstIsInstanceOrNull
import net.mamoe.mirai.qqandroid.network.highway.postImage
import net.mamoe.mirai.qqandroid.network.protocol.data.proto.Cmd0x352
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.image.LongConn
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc
import net.mamoe.mirai.qqandroid.utils.MiraiPlatformUtils
import net.mamoe.mirai.qqandroid.utils.toUHexString
import net.mamoe.mirai.utils.*
@ -80,25 +73,9 @@ internal class QQImpl(
@JvmSynthetic
@Suppress("DuplicatedCode")
override suspend fun sendMessage(message: Message): MessageReceipt<QQ> {
val event = MessageSendEvent.FriendMessageSendEvent(this, message.asMessageChain()).broadcast()
if (event.isCancelled) {
throw EventCancelledException("cancelled by FriendMessageSendEvent")
return sendMessageImpl(message).also {
logMessageSent(message)
}
event.message.firstIsInstanceOrNull<QuoteReply>()?.source?.ensureSequenceIdAvailable()
lateinit var source: MessageSourceToFriendImpl
bot.network.run {
check(
MessageSvc.PbSendMsg.createToFriend(
bot.client,
this@QQImpl,
event.message
) {
source = it
}
.sendAndExpect<MessageSvc.PbSendMsg.Response>() is MessageSvc.PbSendMsg.Response.SUCCESS
) { "send message failed" }
}
return MessageReceipt(source, this, null)
}
@JvmSynthetic

View File

@ -21,6 +21,8 @@ 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.network.BotNetworkHandler
import net.mamoe.mirai.network.WrongPasswordException
import net.mamoe.mirai.qqandroid.QQAndroidBot
@ -445,11 +447,21 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
) {
// highest priority: pass to listeners (attached by sendAndExpect).
if (packet != null && (bot.logger.isEnabled || logger.isEnabled)) {
val logMessage = "Received: ${packet.toString().replace("\n", """\n""").replace("\r", "")}"
fun String.singleLine(): String {
return this.replace("\n", """\n""").replace("\r", "")
}
if (packet is Event) {
bot.logger.verbose(logMessage)
} else logger.verbose(logMessage)
when (packet) {
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 Event -> bot.logger.verbose("Event: ${packet.toString().singleLine()}")
else -> logger.verbose("Event: ${packet.toString().singleLine()}")
}
}
packetListeners.forEach { listener ->