Revert prohibition of sending file message: (#1716)

* Revert prohibition of sending file message:
- close #1715
- Report a warning in logging with stacktrace
- Show stacktrace only once

* Update util.kt
This commit is contained in:
Him188 2021-12-06 15:32:51 +00:00 committed by GitHub
parent bc1cce313f
commit 2367ee0fd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 7 deletions

View File

@ -41,11 +41,15 @@ public open class ExceptionCollector {
if (!hashCodes.add(hash(e))) return false // filter out duplications
// we can also check suppressed exceptions of [e] but actual influence would be slight.
beforeCollect(e)
this.last?.let { e.addSuppressed(it) }
this.last?.let { addSuppressed(e, it) }
this.last = e
return true
}
protected open fun addSuppressed(receiver: Throwable, e: Throwable) {
receiver.addSuppressed(e)
}
private fun hash(e: Throwable): Long {
return e.stackTrace.fold(0L) { acc, stackTraceElement ->
acc * 31 + hash(stackTraceElement).toLongUnsigned()

View File

@ -14,12 +14,9 @@ package net.mamoe.mirai.internal.contact
import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.internal.message.LongMessageInternal
import net.mamoe.mirai.internal.message.MiraiInternalMessageFlag
import net.mamoe.mirai.internal.utils.estimateLength
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.utils.cast
import net.mamoe.mirai.utils.castOrNull
import net.mamoe.mirai.utils.verbose
import net.mamoe.mirai.utils.*
internal inline val Group.uin: Long get() = this.cast<GroupImpl>().uin
internal inline val Group.groupCode: Long get() = this.id
@ -34,13 +31,45 @@ internal fun Contact.logMessageSent(message: Message) {
internal fun MessageChain.countImages(): Int = this.count { it is Image }
private val logger by lazy { MiraiLogger.Factory.create(SendMessageHandler::class) }
// Fixme: Remove in the future, see #1715
private val fileMessageWarningShown = object : ExceptionCollector() {
override fun addSuppressed(receiver: Throwable, e: Throwable) {
}
}
// Fixme: Remove in the future, see #1715
private val ALLOW_SENDING_FILE_MESSAGE = systemProp("mirai.message.allow.sending.file.message", false)
internal fun Message.verifySendingValid() {
fun fail(msg: String): Nothing = throw IllegalArgumentException(msg)
// fun fail(msg: String): Nothing = throw IllegalArgumentException(msg)
when (this) {
is MessageChain -> {
this.forEach { it.verifySendingValid() }
}
is FileMessage -> fail("Sending FileMessage is not in support")
is FileMessage -> {
// Fixme: https://github.com/mamoe/mirai/issues/1715
if (!ALLOW_SENDING_FILE_MESSAGE) {
val e =
Exception("This stacktrace might help you find your code causing this problem. It is shown once for each distinct line.")
val log =
"Sending FileMessage manually is error-prone and is planned to be prohibited in the future. " +
"Please use AbsoluteFolder.uploadNewFile (recommended) or RemoteFile.uploadAndSend instead (deprecated)." +
"You can add JVM argument '-Dmirai.message.allow.sending.file.message=true' to ignore this warning, " +
"however, your code might not work in the future."
// Show stacktrace for each call only once.
if (fileMessageWarningShown.collect(e)) {
logger.warning(log, e)
} else {
logger.warning(log)
}
}
// fail("Sending FileMessage is not in support")
}
}
}