diff --git a/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api b/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api index 116e6ee68..0182ed58e 100644 --- a/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api +++ b/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api @@ -64,7 +64,9 @@ public abstract interface class net/mamoe/mirai/BotFactory$BotConfigurationLambd } public final class net/mamoe/mirai/BotFactory$INSTANCE : net/mamoe/mirai/BotFactory { + public final synthetic fun newBot (JLjava/lang/String;Lkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/Bot; public fun newBot (JLjava/lang/String;Lnet/mamoe/mirai/utils/BotConfiguration;)Lnet/mamoe/mirai/Bot; + public final synthetic fun newBot (J[BLkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/Bot; public fun newBot (J[BLnet/mamoe/mirai/utils/BotConfiguration;)Lnet/mamoe/mirai/Bot; } diff --git a/binary-compatibility-validator/api/binary-compatibility-validator.api b/binary-compatibility-validator/api/binary-compatibility-validator.api index 437bda100..f2608d6f8 100644 --- a/binary-compatibility-validator/api/binary-compatibility-validator.api +++ b/binary-compatibility-validator/api/binary-compatibility-validator.api @@ -64,7 +64,9 @@ public abstract interface class net/mamoe/mirai/BotFactory$BotConfigurationLambd } public final class net/mamoe/mirai/BotFactory$INSTANCE : net/mamoe/mirai/BotFactory { + public final synthetic fun newBot (JLjava/lang/String;Lkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/Bot; public fun newBot (JLjava/lang/String;Lnet/mamoe/mirai/utils/BotConfiguration;)Lnet/mamoe/mirai/Bot; + public final synthetic fun newBot (J[BLkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/Bot; public fun newBot (J[BLnet/mamoe/mirai/utils/BotConfiguration;)Lnet/mamoe/mirai/Bot; } diff --git a/mirai-core-api/src/commonMain/kotlin/BotFactory.kt b/mirai-core-api/src/commonMain/kotlin/BotFactory.kt index bd0512ce6..0c7004c07 100644 --- a/mirai-core-api/src/commonMain/kotlin/BotFactory.kt +++ b/mirai-core-api/src/commonMain/kotlin/BotFactory.kt @@ -118,5 +118,46 @@ public interface BotFactory { override fun newBot(qq: Long, passwordMd5: ByteArray, configuration: BotConfiguration): Bot { return Mirai.BotFactory.newBot(qq, passwordMd5, configuration) } + + /** + * 使用指定的 [配置][configuration] 构造 [Bot] 实例 + * + * ``` + * newBot(123, "") { + * // this: BotConfiguration + * fileBasedDeviceInfo() + * } + * ``` + * + * @since 2.7 + */ + @JvmSynthetic + public inline fun newBot( + qq: Long, + password: String, + configuration: BotConfiguration.() -> Unit /* = BotConfiguration.() -> Unit */ + ): Bot = newBot(qq, password, configuration.run { BotConfiguration().apply(configuration) }) + + // implementation notes: this is inline for `inheritCoroutineContext()` + // see https://github.com/mamoe/mirai/commit/0dbb448cad1ed4773d48ccb8c0b497841bc9fa4c#r50249446 + + /** + * 使用指定的 [配置][configuration] 构造 [Bot] 实例 + * + * ``` + * newBot(123, password) { + * // this: BotConfiguration + * fileBasedDeviceInfo() + * } + * ``` + * + * @since 2.7 + */ + @JvmSynthetic + public inline fun newBot( + qq: Long, + passwordMd5: ByteArray, + configuration: BotConfiguration.() -> Unit /* = BotConfiguration.() -> Unit */ + ): Bot = newBot(qq, passwordMd5, configuration.run { BotConfiguration().apply(configuration) }) } } \ No newline at end of file diff --git a/mirai-core/src/jvmTest/kotlin/BotFactoryTest.kt b/mirai-core/src/jvmTest/kotlin/BotFactoryTest.kt new file mode 100644 index 000000000..bb08b7894 --- /dev/null +++ b/mirai-core/src/jvmTest/kotlin/BotFactoryTest.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.internal + +import kotlinx.coroutines.runBlocking +import net.mamoe.mirai.BotFactory +import kotlin.test.Test + +internal class BotFactoryTest { + + @Test + fun `inlined newBot in Kotlin`() { + runBlocking { + BotFactory.newBot(123, "") { + inheritCoroutineContext() + }.close() + } + } +} \ No newline at end of file