Commit 828a4794 authored by Him188's avatar Him188

Add easier shortcuts

parent 00f33120
......@@ -99,13 +99,9 @@ suspend inline fun Bot.alsoLogin(noinline configuration: BotConfiguration.() ->
/**
* 使用默认的配置 ([BotConfiguration.Default]) 登录, 返回 [this]
*/
@UseExperimental(ExperimentalContracts::class)
suspend inline fun Bot.alsoLogin(lazyMessageWhenLoginFailed: (LoginResult) -> String): Bot {
contract {
callsInPlace(lazyMessageWhenLoginFailed, InvocationKind.AT_MOST_ONCE)
}
suspend inline fun Bot.alsoLogin(message: String): Bot {
return this.apply {
login().requireSuccess(lazyMessageWhenLoginFailed)
login().requireSuccess { message } // requireSuccess is inline, so no performance waste
}
}
......
......@@ -6,17 +6,15 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.withTimeoutOrNull
import net.mamoe.mirai.Bot
import net.mamoe.mirai.BotAccount
import net.mamoe.mirai.alsoLogin
import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.event.*
import net.mamoe.mirai.login
import net.mamoe.mirai.message.*
import net.mamoe.mirai.network.protocol.tim.packet.action.uploadImage
import net.mamoe.mirai.network.protocol.tim.packet.event.FriendMessage
import net.mamoe.mirai.network.protocol.tim.packet.event.GroupMessage
import net.mamoe.mirai.network.protocol.tim.packet.login.ifFail
import net.mamoe.mirai.utils.suspendToExternalImage
import java.io.File
import kotlin.system.exitProcess
private fun readTestAccount(): BotAccount? {
val file = File("testAccount.txt")
......@@ -39,14 +37,9 @@ suspend fun main() {
id = 1994701121u,
password = "123456"
)
)
// 覆盖默认的配置
bot.login {
).alsoLogin {
// 覆盖默认的配置
randomDeviceName = false
}.ifFail {
bot.logger.error("Login failed: $it")
exitProcess(1)
}
bot.messageDSL()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment