Shortcut for bot.login

This commit is contained in:
Him188 2019-10-03 13:56:01 +08:00
parent 333178388a
commit cca32a037b
4 changed files with 12 additions and 10 deletions

View File

@ -1,8 +1,10 @@
package net.mamoe.mirai
import kotlinx.coroutines.CompletableDeferred
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.network.protocol.tim.packet.ClientPacket
import net.mamoe.mirai.network.protocol.tim.packet.login.LoginState
import net.mamoe.mirai.utils.ContactList
/**
@ -24,10 +26,9 @@ val Bot.qqs: ContactList<QQ> get() = this.contacts.qqs
//NetworkHandler
suspend fun Bot.sendPacket(packet: ClientPacket) {
this.network.socket.sendPacket(packet)
}
suspend fun Bot.sendPacket(packet: ClientPacket) = this.network.socket.sendPacket(packet)
fun Bot.login(touchingTimeoutMillis: Long = 200): CompletableDeferred<LoginState> = this.network.tryLogin()
//BotAccount

View File

@ -54,6 +54,11 @@ interface BotNetworkHandler : Closeable {
*/
val action: ActionPacketHandler
/**
* 尝试登录
*
* @param touchingTimeoutMillis 连接每个服务器的 timeout
*/
fun tryLogin(touchingTimeoutMillis: Long = 200): CompletableDeferred<LoginState>
/**

View File

@ -53,12 +53,6 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler {
temporaryPacketHandlers.add(temporaryPacketHandler)
}
//private | internal
/**
* 尝试登录
*
* @param touchingTimeoutMillis 连接每个服务器的 timeout
*/
override fun tryLogin(touchingTimeoutMillis: Long): CompletableDeferred<LoginState> {
val ipQueue: LinkedList<String> = LinkedList(TIMProtocol.SERVER_IP)
val future = CompletableDeferred<LoginState>()
@ -83,6 +77,7 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler {
return future
}
//private | internal
private fun onLoggedIn(sessionKey: ByteArray) {
val session = LoginSession(bot, sessionKey, socket)
message = MessagePacketHandler(session)

View File

@ -7,6 +7,7 @@ import net.mamoe.mirai.event.events.qq.FriendMessageEvent
import net.mamoe.mirai.event.subscribeAll
import net.mamoe.mirai.event.subscribeAlways
import net.mamoe.mirai.event.subscribeUntilFalse
import net.mamoe.mirai.login
import net.mamoe.mirai.message.defaults.Image
import net.mamoe.mirai.message.defaults.PlainText
import net.mamoe.mirai.network.protocol.tim.packet.login.LoginState
@ -22,7 +23,7 @@ suspend fun main() {
password = "bb22222"
), Console())
bot.network.tryLogin().await().let {
bot.login().await().let {
check(it == LoginState.SUCCESS) { "Login failed: " + it.name }
}