@@ -14,12 +14,9 @@ package net.mamoe.mirai.internal.contact
14
14
import net.mamoe.mirai.Bot
15
15
import net.mamoe.mirai.contact.*
16
16
import net.mamoe.mirai.internal.message.LongMessageInternal
17
- import net.mamoe.mirai.internal.message.MiraiInternalMessageFlag
18
17
import net.mamoe.mirai.internal.utils.estimateLength
19
18
import net.mamoe.mirai.message.data.*
20
- import net.mamoe.mirai.utils.cast
21
- import net.mamoe.mirai.utils.castOrNull
22
- import net.mamoe.mirai.utils.verbose
19
+ import net.mamoe.mirai.utils.*
23
20
24
21
internal inline val Group .uin: Long get() = this .cast<GroupImpl >().uin
25
22
internal inline val Group .groupCode: Long get() = this .id
@@ -34,13 +31,45 @@ internal fun Contact.logMessageSent(message: Message) {
34
31
35
32
internal fun MessageChain.countImages (): Int = this .count { it is Image }
36
33
34
+ private val logger by lazy { MiraiLogger .Factory .create(SendMessageHandler ::class ) }
35
+
36
+ // Fixme: Remove in the future, see #1715
37
+ private val fileMessageWarningShown = object : ExceptionCollector () {
38
+ override fun addSuppressed (receiver : Throwable , e : Throwable ) {
39
+ }
40
+ }
41
+
42
+ // Fixme: Remove in the future, see #1715
43
+ private val ALLOW_SENDING_FILE_MESSAGE = systemProp(" mirai.message.allow.sending.file.message" , false )
44
+
37
45
internal fun Message.verifySendingValid () {
38
- fun fail (msg : String ): Nothing = throw IllegalArgumentException (msg)
46
+ // fun fail(msg: String): Nothing = throw IllegalArgumentException(msg)
39
47
when (this ) {
40
48
is MessageChain -> {
41
49
this .forEach { it.verifySendingValid() }
42
50
}
43
- is FileMessage -> fail(" Sending FileMessage is not in support" )
51
+ is FileMessage -> {
52
+ // Fixme: https://github.com/mamoe/mirai/issues/1715
53
+
54
+ if (! ALLOW_SENDING_FILE_MESSAGE ) {
55
+ val e =
56
+ Exception (" This stacktrace might help you find your code causing this problem. It is shown once for each distinct line." )
57
+ val log =
58
+ " Sending FileMessage manually is error-prone and is planned to be prohibited in the future. " +
59
+ " Please use AbsoluteFolder.uploadNewFile (recommended) or RemoteFile.uploadAndSend instead (deprecated)." +
60
+ " You can add JVM argument '-Dmirai.message.allow.sending.file.message=true' to ignore this warning, " +
61
+ " however, your code might not work in the future."
62
+
63
+ // Show stacktrace for each call only once.
64
+ if (fileMessageWarningShown.collect(e)) {
65
+ logger.warning(log, e)
66
+ } else {
67
+ logger.warning(log)
68
+ }
69
+ }
70
+
71
+ // fail("Sending FileMessage is not in support")
72
+ }
44
73
}
45
74
}
46
75
0 commit comments