mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-23 06:10:30 +08:00
Adjust function name
This commit is contained in:
parent
3d8a8b5ab5
commit
a42fbe47a4
@ -213,14 +213,14 @@ Mirai 22:04:48 : Packet received: UnknownEventPacket(id=00 D6, identity=(2092749
|
||||
* @param lazyMessage 若需要验证请求时的验证消息.
|
||||
*/
|
||||
suspend fun ContactSystem.addFriend(id: UInt, lazyMessage: () -> String = { "" }, lazyRemark: () -> String = { "" }): AddFriendResult = bot.withSession {
|
||||
when (CanAddFriendPacket(bot.qqAccount, id, bot.sessionKey).sendAndExpect<CanAddFriendResponse>().await()) {
|
||||
when (CanAddFriendPacket(bot.qqAccount, id, bot.sessionKey).sendAndExpect<CanAddFriendResponse>()) {
|
||||
is CanAddFriendResponse.AlreadyAdded -> AddFriendResult.ALREADY_ADDED
|
||||
is CanAddFriendResponse.Rejected -> AddFriendResult.REJECTED
|
||||
|
||||
is CanAddFriendResponse.ReadyToAdd,
|
||||
is CanAddFriendResponse.RequireVerification -> {
|
||||
val key = RequestFriendAdditionKeyPacket(bot.qqAccount, id, sessionKey).sendAndExpect<RequestFriendAdditionKeyPacket.Response>().await().key
|
||||
AddFriendPacket(bot.qqAccount, id, sessionKey, lazyMessage(), lazyRemark(), key).sendAndExpect<AddFriendPacket.Response>().await()
|
||||
val key = RequestFriendAdditionKeyPacket(bot.qqAccount, id, sessionKey).sendAndExpect<RequestFriendAdditionKeyPacket.Response>().key
|
||||
AddFriendPacket(bot.qqAccount, id, sessionKey, lazyMessage(), lazyRemark(), key).sendAndExpect<AddFriendPacket.Response>()
|
||||
return AddFriendResult.WAITING_FOR_APPROVE
|
||||
}
|
||||
//这个做的是需要验证消息的情况, 不确定 ReadyToAdd 的是啥
|
||||
@ -232,13 +232,12 @@ suspend fun ContactSystem.addFriend(id: UInt, lazyMessage: () -> String = { "" }
|
||||
/*is CanAddFriendResponse.ReadyToAdd -> {
|
||||
// TODO: 2019/11/11 这不需要验证信息的情况
|
||||
|
||||
//AddFriendPacket(bot.qqAccount, id, bot.sessionKey, ).sendAndExpect<AddFriendPacket.Response>().await()
|
||||
//AddFriendPacket(bot.qqAccount, id, bot.sessionKey, ).sendAndExpectAsync<AddFriendPacket.Response>().await()
|
||||
TODO()
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
1494695429 同意好友请求后收到以下包:
|
||||
|
||||
|
@ -41,7 +41,7 @@ inline val Bot.qqs: ContactList<QQ>
|
||||
|
||||
/**
|
||||
* 以 [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值.
|
||||
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, 如 [BotSession.sendAndExpect]
|
||||
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, 如 [BotSession.sendAndExpectAsync]
|
||||
*/
|
||||
inline fun <R> Bot.withSession(block: BotSession.() -> R): R = with(this.network.session) { block() }
|
||||
|
||||
|
@ -104,7 +104,7 @@ class Group internal constructor(bot: Bot, val groupId: GroupId) : Contact(bot,
|
||||
|
||||
/**
|
||||
* 以 [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值.
|
||||
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, 如 [BotSession.sendAndExpect]
|
||||
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, 如 [BotSession.sendAndExpectAsync]
|
||||
*/
|
||||
inline fun <R> Contact.withSession(block: BotSession.() -> R): R = bot.withSession(block)
|
||||
|
||||
@ -138,7 +138,7 @@ open class QQ internal constructor(bot: Bot, id: UInt) : Contact(bot, id) {
|
||||
*/
|
||||
suspend fun updateProfile(): Profile = bot.withSession {
|
||||
RequestProfileDetailsPacket(bot.qqAccount, id, sessionKey)
|
||||
.sendAndExpect<RequestProfileDetailsResponse, Profile> { it.profile }
|
||||
.sendAndExpectAsync<RequestProfileDetailsResponse, Profile> { it.profile }
|
||||
.await().let {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
if ((::profile as SuspendLazy<Profile>).isInitialized()) {
|
||||
|
@ -66,7 +66,7 @@ interface BotNetworkHandler<Socket : DataPacketSocketAdapter> : CoroutineScope {
|
||||
/**
|
||||
* 添加一个临时包处理器, 并发送相应的包
|
||||
*
|
||||
* @see [BotSession.sendAndExpect] 发送并期待一个包
|
||||
* @see [BotSession.sendAndExpectAsync] 发送并期待一个包
|
||||
* @see [TemporaryPacketHandler] 临时包处理器
|
||||
*/
|
||||
suspend fun addHandler(temporaryPacketHandler: TemporaryPacketHandler<*, *>)
|
||||
|
@ -4,6 +4,7 @@ package net.mamoe.mirai.network
|
||||
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.Job
|
||||
import net.mamoe.mirai.Bot
|
||||
import net.mamoe.mirai.contact.Group
|
||||
@ -75,7 +76,7 @@ class BotSession(
|
||||
* 实现方法:
|
||||
* ```kotlin
|
||||
* with(session){
|
||||
* ClientPacketXXX(...).sendAndExpect<ServerPacketXXX> {
|
||||
* ClientPacketXXX(...).sendAndExpectAsync<ServerPacketXXX> {
|
||||
* //it: ServerPacketXXX
|
||||
* }
|
||||
* }
|
||||
@ -84,14 +85,14 @@ class BotSession(
|
||||
*
|
||||
* @param checkSequence 是否筛选 `sequenceId`, 即是否筛选发出的包对应的返回包.
|
||||
* @param P 期待的包
|
||||
* @param handler 处理期待的包. 将会在调用本函数的 [coroutineContext] 下执行.
|
||||
* @param handler 处理期待的包. **将会在调用本函数的 [coroutineContext] 下执行.**
|
||||
*
|
||||
* @see Bot.withSession 转换接收器 (receiver, 即 `this` 的指向) 为 [BotSession]
|
||||
* @see Bot.withSession 转换 receiver, 即 `this` 的指向, 为 [BotSession]
|
||||
*/
|
||||
suspend inline fun <reified P : Packet, R> OutgoingPacket.sendAndExpect(
|
||||
suspend inline fun <reified P : Packet, R> OutgoingPacket.sendAndExpectAsync(
|
||||
checkSequence: Boolean = true,
|
||||
noinline handler: suspend (P) -> R
|
||||
): CompletableDeferred<R> {
|
||||
): Deferred<R> {
|
||||
val deferred: CompletableDeferred<R> = CompletableDeferred(coroutineContext[Job])
|
||||
bot.network.addHandler(TemporaryPacketHandler(P::class, deferred, this@BotSession, checkSequence, coroutineContext + deferred).also {
|
||||
it.toSend(this)
|
||||
@ -103,9 +104,36 @@ class BotSession(
|
||||
/**
|
||||
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P].
|
||||
* 将能从本函数的返回值 [CompletableDeferred] 接收到所期待的 [P]
|
||||
* 本函数将能帮助实现清晰的包处理逻辑
|
||||
*
|
||||
* 实现方法:
|
||||
* ```kotlin
|
||||
* with(session){
|
||||
* val deferred = ClientPacketXXX(...).sendAndExpectAsync<ServerPacketXXX>()
|
||||
* // do something
|
||||
*
|
||||
* deferred.await() // or deferred.join()
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
suspend inline fun <reified P : Packet> OutgoingPacket.sendAndExpect(checkSequence: Boolean = true): CompletableDeferred<P> =
|
||||
sendAndExpect<P, P>(checkSequence) { it }
|
||||
suspend inline fun <reified P : Packet> OutgoingPacket.sendAndExpectAsync(checkSequence: Boolean = true): Deferred<P> =
|
||||
sendAndExpectAsync<P, P>(checkSequence) { it }
|
||||
|
||||
/**
|
||||
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P].
|
||||
* 将调用 [CompletableDeferred.await], 挂起等待接收到指定的包后才返回.
|
||||
* 本函数将能帮助实现清晰的包处理逻辑
|
||||
*
|
||||
* 实现方法:
|
||||
* ```kotlin
|
||||
* with(session){
|
||||
* val packet:AddFriendPacket.Response = AddFriendPacket(...).sendAndExpect<AddFriendPacket.Response>()
|
||||
* }
|
||||
* ```
|
||||
* @sample Bot.addFriend 添加好友
|
||||
*/
|
||||
suspend inline fun <reified P : Packet> OutgoingPacket.sendAndExpect(checkSequence: Boolean = true): P =
|
||||
sendAndExpectAsync<P, P>(checkSequence) { it }.await()
|
||||
|
||||
suspend inline fun OutgoingPacket.send() = socket.sendPacket(this)
|
||||
|
||||
|
@ -513,7 +513,7 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot
|
||||
HeartbeatPacket(
|
||||
bot.qqAccount,
|
||||
sessionKey
|
||||
).sendAndExpect<HeartbeatPacketResponse>().join()
|
||||
).sendAndExpectAsync<HeartbeatPacketResponse>().join()
|
||||
} == null) {
|
||||
bot.logger.warning("Heartbeat timed out")
|
||||
bot.reinitializeNetworkHandler(configuration, HeartbeatTimeoutException())
|
||||
|
@ -41,7 +41,7 @@ interface DataPacketSocketAdapter : Closeable {
|
||||
*
|
||||
* 可通过 hook 事件 [ServerPacketReceivedEvent] 来获取服务器返回.
|
||||
*
|
||||
* @see [BotSession.sendAndExpect] kotlin DSL
|
||||
* @see [BotSession.sendAndExpectAsync] kotlin DSL
|
||||
*/
|
||||
suspend fun sendPacket(packet: OutgoingPacket)
|
||||
|
||||
|
@ -21,7 +21,7 @@ import kotlin.reflect.KClass
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @see BotSession.sendAndExpect
|
||||
* @see BotSession.sendAndExpectAsync
|
||||
*/
|
||||
class TemporaryPacketHandler<P : Packet, R>(
|
||||
private val expectationClass: KClass<P>,
|
||||
|
@ -37,7 +37,7 @@ class OverFileSizeMaxException : IllegalStateException()
|
||||
suspend fun Group.uploadImage(image: ExternalImage): ImageId = withSession {
|
||||
val userContext = coroutineContext
|
||||
GroupImageIdRequestPacket(bot.qqAccount, internalId, image, sessionKey)
|
||||
.sendAndExpect<GroupImageIdRequestPacket.Response, Unit> {
|
||||
.sendAndExpectAsync<GroupImageIdRequestPacket.Response, Unit> {
|
||||
withContext(userContext) {
|
||||
when (it) {
|
||||
is GroupImageIdRequestPacket.Response.RequireUpload -> httpClient.postImage(
|
||||
@ -69,8 +69,8 @@ suspend fun Group.uploadImage(image: ExternalImage): ImageId = withSession {
|
||||
*/
|
||||
suspend fun QQ.uploadImage(image: ExternalImage): ImageId = bot.withSession {
|
||||
FriendImageIdRequestPacket(qqAccount, sessionKey, id, image)
|
||||
.sendAndExpect<FriendImageIdRequestPacket.Response, ImageId> {
|
||||
return@sendAndExpect when (it) {
|
||||
.sendAndExpectAsync<FriendImageIdRequestPacket.Response, ImageId> {
|
||||
return@sendAndExpectAsync when (it) {
|
||||
is FriendImageIdRequestPacket.Response.RequireUpload -> {
|
||||
httpClient.postImage(
|
||||
htcmd = "0x6ff0070",
|
||||
|
@ -312,7 +312,7 @@ internal object DebugNetworkHandler : BotNetworkHandler<DataPacketSocketAdapter>
|
||||
get() = bot
|
||||
|
||||
}
|
||||
override val bot: Bot = Bot(0u, "")
|
||||
override val bot: Bot = Bot(qq, "")
|
||||
val session = BotSession(bot, sessionKey, socket, this)
|
||||
val action = ActionPacketHandler(session)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user