Przeglądaj źródła

Misc improvements

Him188 6 lat temu
rodzic
commit
dc7d73f148

+ 1 - 1
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt

@@ -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

+ 11 - 3
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt

@@ -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() = [email protected]
             override val nick: String get() = [email protected]
         })
     }
 
-    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 {

+ 1 - 2
mirai-core-qqandroid/src/jvmMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt

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

+ 0 - 11
mirai-core/src/androidMain/kotlin/net/mamoe/mirai/Bot.kt

@@ -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
-
     /**
      * 机器人加入的群列表.
      */

+ 1 - 10
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt

@@ -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
-
     /**
      * 机器人加入的群列表.
      */

+ 31 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/lowLevelApi.kt

@@ -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)
+    )
 }

+ 0 - 11
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/Bot.kt

@@ -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
-
     /**
      * 机器人加入的群列表.
      */