Misc improvements

This commit is contained in:
Him188 2020-03-03 08:43:32 +08:00
parent cadbd7ff3a
commit dc7d73f148
7 changed files with 45 additions and 39 deletions

View File

@ -542,7 +542,7 @@ internal class GroupImpl(
@UseExperimental(MiraiExperimentalAPI::class)
override fun Member(memberInfo: MemberInfo): Member {
return MemberImpl(
bot.QQ(memberInfo) as QQImpl,
bot._lowLevelNewQQQQ(memberInfo) as QQImpl,
this,
this.coroutineContext,
memberInfo

View File

@ -12,6 +12,7 @@ package net.mamoe.mirai.qqandroid
import io.ktor.client.request.get
import io.ktor.client.statement.HttpResponse
import io.ktor.utils.io.ByteReadChannel
import kotlinx.coroutines.CoroutineName
import net.mamoe.mirai.BotAccount
import net.mamoe.mirai.BotImpl
import net.mamoe.mirai.LowLevelAPI
@ -67,14 +68,21 @@ internal abstract class QQAndroidBotBase constructor(
override val friends: ContactList<QQ> = ContactList(LockFreeLinkedList())
override val selfQQ: QQ by lazy {
QQ(object : FriendInfo {
@UseExperimental(LowLevelAPI::class)
_lowLevelNewQQ(object : FriendInfo {
override val uin: Long get() = this@QQAndroidBotBase.uin
override val nick: String get() = this@QQAndroidBotBase.nick
})
}
override fun QQ(friendInfo: FriendInfo): QQ {
return QQImpl(this as QQAndroidBot, coroutineContext, friendInfo.uin, friendInfo)
@LowLevelAPI
override fun _lowLevelNewQQ(friendInfo: FriendInfo): QQ {
return QQImpl(
this as QQAndroidBot,
coroutineContext + CoroutineName("QQ(${friendInfo.uin}"),
friendInfo.uin,
friendInfo
)
}
override fun createNetworkHandler(coroutineContext: CoroutineContext): QQAndroidBotNetworkHandler {

View File

@ -24,5 +24,4 @@ internal actual class QQAndroidBot actual constructor(
context: Context,
account: BotAccount,
configuration: BotConfiguration
) : QQAndroidBotBase(context, account, configuration)
) : QQAndroidBotBase(context, account, configuration)

View File

@ -4,10 +4,8 @@ package net.mamoe.mirai
import io.ktor.utils.io.ByteReadChannel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.data.AddFriendResult
import net.mamoe.mirai.data.FriendInfo
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.MessageChain
@ -127,15 +125,6 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
?: throw NoSuchElementException("No such friend $id for bot ${this.uin}")
}
/**
* 构造一个 [QQ] 对象. 它持有对 [Bot] 的弱引用([WeakRef]).
*
* [Bot] 无法管理这个对象, 但这个对象会以 [Bot] [Job] 作为父 Job.
* 因此, [Bot] 被关闭后, 这个对象也会被关闭.
*/
@Suppress("FunctionName")
actual abstract fun QQ(friendInfo: FriendInfo): QQ
/**
* 机器人加入的群列表.
*/

View File

@ -18,7 +18,6 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.data.AddFriendResult
import net.mamoe.mirai.data.FriendInfo
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.MessageChain
@ -96,7 +95,7 @@ expect abstract class Bot() : CoroutineScope, LowLevelBotAPIAccessor {
// region contacts
/**
* [QQ.id] [Bot.uin] 相同的 [QQ] 实例
* [_lowLevelNewQQ.id] [Bot.uin] 相同的 [_lowLevelNewQQ] 实例
*/
abstract val selfQQ: QQ
@ -136,14 +135,6 @@ expect abstract class Bot() : CoroutineScope, LowLevelBotAPIAccessor {
*/
fun getFriend(id: Long): QQ
/**
* 构造一个 [QQ] 对象. 它持有对 [Bot] 的弱引用([WeakRef]).
*
* [Bot] 无法管理这个对象, 但这个对象会以 [Bot] [Job] 作为父 Job.
* 因此, [Bot] 被关闭后, 这个对象也会被关闭.
*/
abstract fun QQ(friendInfo: FriendInfo): QQ
/**
* 机器人加入的群列表.
*/

View File

@ -9,11 +9,15 @@
package net.mamoe.mirai
import kotlinx.coroutines.Job
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.data.FriendInfo
import net.mamoe.mirai.data.GroupInfo
import net.mamoe.mirai.data.MemberInfo
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import net.mamoe.mirai.utils.WeakRef
/**
* 标示这个 API 是低级的 API.
@ -35,7 +39,16 @@ annotation class LowLevelAPI
interface LowLevelBotAPIAccessor {
/**
* 向服务器查询群列表. 返回值前 32 bits uin, 32 bits groupCode
* 构造一个 [_lowLevelNewQQ] 对象. 它持有对 [Bot] 的弱引用([WeakRef]).
*
* [Bot] 无法管理这个对象, 但这个对象会以 [Bot] [Job] 作为父 Job.
* 因此, [Bot] 被关闭后, 这个对象也会被关闭.
*/
@LowLevelAPI
fun _lowLevelNewQQ(friendInfo: FriendInfo): QQ
/**
* 向服务器查询群列表. 返回值高 32 bits uin, 32 bits groupCode
*/
@LowLevelAPI
suspend fun _lowLevelQueryGroupList(): Sequence<Long>
@ -72,4 +85,21 @@ interface LowLevelBotAPIAccessor {
*/
@LowLevelAPI
suspend fun _lowLevelRecallGroupMessage(groupId: Long, messageId: Long)
}
/**
* 撤回一条群里的消息. 可以是机器人发送也可以是其他群员发送.
*/
@Suppress("FunctionName")
@MiraiExperimentalAPI
@LowLevelAPI
suspend fun LowLevelBotAPIAccessor._lowLevelRecallGroupMessage(
groupId: Long,
messageSequenceId: Int,
messageRandom: Int
) {
this._lowLevelRecallGroupMessage(
groupId,
messageSequenceId.toLong().shl(32) or messageRandom.toLong().and(0xFFFFFFFFL)
)
}

View File

@ -4,10 +4,8 @@ package net.mamoe.mirai
import io.ktor.utils.io.ByteReadChannel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.data.AddFriendResult
import net.mamoe.mirai.data.FriendInfo
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.MessageChain
@ -137,15 +135,6 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
?: throw NoSuchElementException("No such friend $id for bot ${this.uin}")
}
/**
* 构造一个 [QQ] 对象. 它持有对 [Bot] 的弱引用([WeakRef]).
*
* [Bot] 无法管理这个对象, 但这个对象会以 [Bot] [Job] 作为父 Job.
* 因此, [Bot] 被关闭后, 这个对象也会被关闭.
*/
@Suppress("FunctionName")
actual abstract fun QQ(friendInfo: FriendInfo): QQ
/**
* 机器人加入的群列表.
*/