Add getMiraiImpl to replace direct calls of MiraiImpl.INSTANCE

This commit is contained in:
Him188 2021-06-29 18:52:43 +08:00
parent 91a9e63877
commit a59d7208cb
4 changed files with 10 additions and 5 deletions

View File

@ -63,6 +63,8 @@ import kotlin.io.use
import kotlin.math.absoluteValue
import kotlin.random.Random
internal fun getMiraiImpl() = Mirai as MiraiImpl
@OptIn(LowLevelApi::class)
// not object for ServiceLoader.
internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@ -823,7 +825,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
return jsonText?.let { json.decodeFromString(GroupHonorListData.serializer(), it) }
}
internal suspend fun uploadMessageHighway(
internal open suspend fun uploadMessageHighway(
bot: Bot,
sendMessageHandler: SendMessageHandler<*>,
message: Collection<ForwardMessage.INode>,

View File

@ -12,6 +12,7 @@ package net.mamoe.mirai.internal.contact
import net.mamoe.mirai.contact.AnonymousMember
import net.mamoe.mirai.data.MemberInfo
import net.mamoe.mirai.internal.MiraiImpl
import net.mamoe.mirai.internal.getMiraiImpl
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.utils.ExternalResource
import kotlin.coroutines.CoroutineContext
@ -24,7 +25,7 @@ internal class AnonymousMemberImpl(
) : AnonymousMember, AbstractMember(group, coroutineContext, memberInfo) {
override suspend fun mute(durationSeconds: Int) {
checkBotPermissionHigherThanThis("mute")
MiraiImpl.muteAnonymousMember(bot, anonymousId, nameCard, group.uin, durationSeconds)
getMiraiImpl().muteAnonymousMember(bot, anonymousId, nameCard, group.uin, durationSeconds)
}
override fun toString(): String = "AnonymousMember($nameCard, $anonymousId)"

View File

@ -16,6 +16,7 @@ import net.mamoe.mirai.contact.*
import net.mamoe.mirai.event.nextEventOrNull
import net.mamoe.mirai.internal.MiraiImpl
import net.mamoe.mirai.internal.asQQAndroidBot
import net.mamoe.mirai.internal.getMiraiImpl
import net.mamoe.mirai.internal.message.*
import net.mamoe.mirai.internal.message.LightMessageRefiner.refineLight
import net.mamoe.mirai.internal.network.Packet
@ -233,7 +234,7 @@ internal abstract class SendMessageHandler<C : Contact> {
open suspend fun uploadLongMessageHighway(
chain: MessageChain,
): String = with(contact) {
return MiraiImpl.uploadMessageHighway(
return getMiraiImpl().uploadMessageHighway(
bot, this@SendMessageHandler,
listOf(
ForwardMessage.Node(
@ -275,7 +276,7 @@ internal suspend fun <C : Contact> SendMessageHandler<C>.transformSpecialMessage
}
}
val resId = MiraiImpl.uploadMessageHighway(
val resId = getMiraiImpl().uploadMessageHighway(
bot = contact.bot,
sendMessageHandler = this,
message = forward.nodeList,

View File

@ -16,6 +16,7 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.internal.AbstractTestWithMiraiImpl
import net.mamoe.mirai.internal.MiraiImpl
import net.mamoe.mirai.internal.MockBot
import net.mamoe.mirai.internal.getMiraiImpl
import net.mamoe.mirai.internal.message.DeepMessageRefiner.refineDeep
import net.mamoe.mirai.internal.message.LightMessageRefiner.refineLight
import net.mamoe.mirai.internal.message.OfflineMessageSourceImplData
@ -267,7 +268,7 @@ internal class MessageRefineTest : AbstractTestWithMiraiImpl() {
@Test
fun `test nested forward refinement`() = runBlockingUnit {
val redefined = MiraiImpl.run { testCases.nestedForward.toForwardMessageNodes(bot) }
val redefined = getMiraiImpl().run { testCases.nestedForward.toForwardMessageNodes(bot) }
assertNodesEquals(
listOf(
ForwardMessage.Node(