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

View File

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

View File

@ -53,12 +53,6 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler {
temporaryPacketHandlers.add(temporaryPacketHandler) temporaryPacketHandlers.add(temporaryPacketHandler)
} }
//private | internal
/**
* 尝试登录
*
* @param touchingTimeoutMillis 连接每个服务器的 timeout
*/
override fun tryLogin(touchingTimeoutMillis: Long): CompletableDeferred<LoginState> { override fun tryLogin(touchingTimeoutMillis: Long): CompletableDeferred<LoginState> {
val ipQueue: LinkedList<String> = LinkedList(TIMProtocol.SERVER_IP) val ipQueue: LinkedList<String> = LinkedList(TIMProtocol.SERVER_IP)
val future = CompletableDeferred<LoginState>() val future = CompletableDeferred<LoginState>()
@ -83,6 +77,7 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler {
return future return future
} }
//private | internal
private fun onLoggedIn(sessionKey: ByteArray) { private fun onLoggedIn(sessionKey: ByteArray) {
val session = LoginSession(bot, sessionKey, socket) val session = LoginSession(bot, sessionKey, socket)
message = MessagePacketHandler(session) 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.subscribeAll
import net.mamoe.mirai.event.subscribeAlways import net.mamoe.mirai.event.subscribeAlways
import net.mamoe.mirai.event.subscribeUntilFalse import net.mamoe.mirai.event.subscribeUntilFalse
import net.mamoe.mirai.login
import net.mamoe.mirai.message.defaults.Image import net.mamoe.mirai.message.defaults.Image
import net.mamoe.mirai.message.defaults.PlainText import net.mamoe.mirai.message.defaults.PlainText
import net.mamoe.mirai.network.protocol.tim.packet.login.LoginState import net.mamoe.mirai.network.protocol.tim.packet.login.LoginState
@ -22,7 +23,7 @@ suspend fun main() {
password = "bb22222" password = "bb22222"
), Console()) ), Console())
bot.network.tryLogin().await().let { bot.login().await().let {
check(it == LoginState.SUCCESS) { "Login failed: " + it.name } check(it == LoginState.SUCCESS) { "Login failed: " + it.name }
} }