Add inline BotFactory.newBot for Kotlin DSL. fix #1233 (#1234)

This commit is contained in:
Him188 2021-05-02 13:51:32 +08:00 committed by GitHub
parent 0baaf9d587
commit 57eb716e31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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) })
}
}

View File

@ -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()
}
}
}