From 067a949630f0da26da91f38eed22cc61bd765b8a Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 29 Feb 2020 13:58:27 +0800 Subject: [PATCH] Rename `MessageSource.senderId` to `MessageSource.qqId` --- .../net/mamoe/mirai/qqandroid/QQAndroidBot.kt | 54 ++++++++++++------- .../qqandroid/message/MessageSourceFromMsg.kt | 4 +- .../protocol/packet/chat/PbMessageSvc.kt | 3 +- .../packet/chat/receive/MessageSvc.kt | 8 +-- .../commonMain/kotlin/net.mamoe.mirai/Bot.kt | 28 +++++----- .../net.mamoe.mirai/message/MessageReceipt.kt | 6 +-- .../message/data/MessageSource.kt | 9 +++- 7 files changed, 67 insertions(+), 45 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt index 6b0a475f3..40cb15ecf 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt @@ -122,38 +122,54 @@ internal abstract class QQAndroidBotBase constructor( } override suspend fun recall(source: MessageSource) { - if (source.senderId != uin) { + if (source.qqId != uin && source.groupId != 0L) { getGroup(source.groupId).checkBotPermissionOperator() } source.ensureSequenceIdAvailable() network.run { - val response: PbMessageSvc.PbMsgWithDraw.Response = if (source.groupId == 0L) { - PbMessageSvc.PbMsgWithDraw.Friend(bot.client, source.senderId, source.sequenceId, source.messageRandom, source.time) - .sendAndExpect() - } else { + val response: PbMessageSvc.PbMsgWithDraw.Response = + if (source.groupId == 0L) { + PbMessageSvc.PbMsgWithDraw.Friend( + bot.client, + source.qqId, + source.sequenceId, + source.messageRandom, + source.time + ) + .sendAndExpect() + } else { + PbMessageSvc.PbMsgWithDraw.Group( + bot.client, + source.groupId, + source.sequenceId, + source.messageRandom + ) + .sendAndExpect() + } - PbMessageSvc.PbMsgWithDraw.Group(bot.client, source.groupId, source.sequenceId, source.messageRandom) - .sendAndExpect() - } - - check(response is PbMessageSvc.PbMsgWithDraw.Response.Success) { "Failed to recall message #${source.sequenceId}: $response" } + check(response is PbMessageSvc.PbMsgWithDraw.Response.Success) { "Failed to recall message #${source.id}: $response" } } } - override suspend fun recall(groupId: Long, senderId: Long, messageId: Long) { - if (senderId != uin) { - getGroup(groupId).checkBotPermissionOperator() - } - - val sequenceId = (messageId shr 32).toInt() - + override suspend fun _lowLevelRecallFriendMessage(friendId: Long, messageId: Long) { network.run { val response: PbMessageSvc.PbMsgWithDraw.Response = - PbMessageSvc.PbMsgWithDraw.Group(bot.client, groupId, sequenceId, messageId.toInt()) + PbMessageSvc.PbMsgWithDraw.Friend(client, friendId, (messageId shr 32).toInt(), messageId.toInt(), 0) .sendAndExpect() - check(response is PbMessageSvc.PbMsgWithDraw.Response.Success) { "Failed to recall message #$sequenceId: $response" } + + check(response is PbMessageSvc.PbMsgWithDraw.Response.Success) { "Failed to recall message #${messageId}: $response" } + } + } + + override suspend fun _lowLevelRecallGroupMessage(groupId: Long, messageId: Long) { + network.run { + val response: PbMessageSvc.PbMsgWithDraw.Response = + PbMessageSvc.PbMsgWithDraw.Group(client, groupId, (messageId shr 32).toInt(), messageId.toInt()) + .sendAndExpect() + + check(response is PbMessageSvc.PbMsgWithDraw.Response.Success) { "Failed to recall message #${messageId}: $response" } } } diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt index ac7ffb316..f663c748a 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt @@ -33,7 +33,7 @@ internal inline class MessageSourceFromServer( } // override val sourceMessage: MessageChain get() = delegate.toMessageChain() - override val senderId: Long get() = delegate.senderUin + override val qqId: Long get() = delegate.senderUin override val groupId: Long get() = Group.calculateGroupCodeByGroupUin(delegate.toUin) override fun toString(): String = "" @@ -52,7 +52,7 @@ internal inline class MessageSourceFromMsg( } // override val sourceMessage: MessageChain get() = delegate.toMessageChain() - override val senderId: Long get() = delegate.msgHead.fromUin + override val qqId: Long get() = delegate.msgHead.fromUin override val groupId: Long get() = delegate.msgHead.groupInfo?.groupCode ?: 0 fun toJceData(): ImMsgBody.SourceMsg { diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/PbMessageSvc.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/PbMessageSvc.kt index 88dd705db..098d9a17a 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/PbMessageSvc.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/PbMessageSvc.kt @@ -76,8 +76,7 @@ internal class PbMessageSvc { toUin: Long, messageSequenceId: Int, // 56639 messageRandom: Int, // 921878719 - time: Long, - messageType: Int = 0 + time: Long ): OutgoingPacket = buildOutgoingUniPacket(client) { writeProtoBuf( MsgSvc.PbMsgWithDrawReq.serializer(), diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt index 41aa13650..02151f526 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt @@ -272,7 +272,7 @@ internal class MessageSvc { internal class MessageSourceFromSendFriend( val messageRandom: Int, override val time: Long, - override val senderId: Long, + override val qqId: Long, override val groupId: Long, val sequenceId: Int ) : MessageSource { @@ -293,7 +293,7 @@ internal class MessageSvc { internal class MessageSourceFromSendGroup( val messageRandom: Int, override val time: Long, - override val senderId: Long, + override val qqId: Long, override val groupId: Long// , // override val sourceMessage: MessageChain ) : MessageSource { @@ -330,7 +330,7 @@ internal class MessageSvc { ): OutgoingPacket { val source = MessageSourceFromSendFriend( messageRandom = Random.nextInt().absoluteValue, - senderId = client.uin, + qqId = toUin, time = currentTimeSeconds + client.timeDifference, groupId = 0, sequenceId = client.atomicNextMessageSequenceId() @@ -379,7 +379,7 @@ internal class MessageSvc { val source = MessageSourceFromSendGroup( messageRandom = Random.nextInt().absoluteValue, - senderId = client.uin, + qqId = client.uin, time = currentTimeSeconds + client.timeDifference, groupId = groupCode//, // sourceMessage = message diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt index a912ecc9e..e8e3023e0 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt @@ -21,6 +21,7 @@ import net.mamoe.mirai.data.AddFriendResult import net.mamoe.mirai.data.FriendInfo import net.mamoe.mirai.data.GroupInfo import net.mamoe.mirai.data.MemberInfo +import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.data.Image import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageSource @@ -205,30 +206,29 @@ abstract class Bot : CoroutineScope { // region actions /** - * 撤回这条消息. + * 撤回这条消息. 可撤回自己 2 分钟内发出的消息, 和任意时间的群成员的消息. * * [Bot] 撤回自己的消息不需要权限. * [Bot] 撤回群员的消息需要管理员权限. * + * @param source 消息源. 可从 [MessageReceipt.source] 获得, 或从消息事件中的 [MessageChain] 获得. + * * @throws PermissionDeniedException 当 [Bot] 无权限操作时 * @see Bot.recall (扩展函数) 接受参数 [MessageChain] - */// source.groupId, source.sequenceId, source.messageUid + */ abstract suspend fun recall(source: MessageSource) /** - * 撤回一条消息. 可撤回自己 2 分钟内发出的消息, 和任意时间的群成员的消息. - * - * [Bot] 撤回自己的消息不需要权限. - * [Bot] 撤回群员的消息需要管理员权限. - * - * @param senderId 这条消息的发送人. 可以为 [Bot.uin] 或 [Member.id] - * @param messageId 即 [MessageSource.id] - * - * @throws PermissionDeniedException 当 [Bot] 无权限操作时 - * @see Bot.recall (扩展函数) 接受参数 [MessageChain] - * @see recall 请优先使用这个函数 + * 撤回一条由机器人发送给好友的消息 */ - abstract suspend fun recall(groupId: Long, senderId: Long, messageId: Long) + @MiraiExperimentalAPI + abstract suspend fun _lowLevelRecallFriendMessage(friendId: Long, messageId: Long) + + /** + * 撤回一条群里的消息. 可以是机器人发送也可以是其他群员发送. + */ + @MiraiExperimentalAPI + abstract suspend fun _lowLevelRecallGroupMessage(groupId: Long, messageId: Long) /** * 获取图片下载链接 diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt index d99c8b325..982fc5231 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt @@ -72,7 +72,7 @@ open class MessageReceipt( } /** - * 撤回这条消息. [recall] 或 [recallIn] 只能被调用一次. + * 在一段时间后撤回这条消息.. [recall] 或 [recallIn] 只能被调用一次. * * @param millis 延迟时间, 单位为毫秒 * @@ -91,7 +91,7 @@ open class MessageReceipt( } /** - * 引用这条消息. 仅群消息能被引用 + * 引用这条消息. * * @see MessageChain.quote 引用一条消息 * @@ -105,7 +105,7 @@ open class MessageReceipt( } /** - * 引用这条消息并回复. 仅群消息能被引用 + * 引用这条消息并回复. * * @see MessageChain.quote 引用一条消息 * diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt index c4858bb84..fb29d28d9 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt @@ -13,6 +13,7 @@ package net.mamoe.mirai.message.data import net.mamoe.mirai.Bot +import net.mamoe.mirai.contact.QQ import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName @@ -49,8 +50,14 @@ interface MessageSource : Message, MessageMetadata { val time: Long /** - * 发送人号码 + * 与这个消息相关的 [QQ] 的 [QQ.id] + * + * 群消息时为发送人的 id. 好友消息时为消息发送目标好友的 id */ + val qqId: Long + + @Suppress("unused") // TODO: 2020/2/29 0.25: 删除 `MessageSource.senderId` + @Deprecated("使用 qqId. 此 API 将在 0.25 删除", level = DeprecationLevel.HIDDEN, replaceWith = ReplaceWith("this.qqId")) val senderId: Long /**