From 4e2108d841a3eba87d178e7164c59352d40b303a Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 8 Jan 2021 12:28:12 +0800 Subject: [PATCH] [Review] NudgeEvent: Simplify and stabilize NudgeEvent, remove all XXXNudgeEvent, add single NudgeEvent --- .../src/commonMain/kotlin/event/events/bot.kt | 151 ++---------------- .../commonMain/kotlin/event/events/friend.kt | 59 +------ .../commonMain/kotlin/event/events/group.kt | 31 +--- .../kotlin/event/events/stranger.kt | 64 ++------ .../commonMain/kotlin/message/action/Nudge.kt | 9 +- .../packet/chat/receive/OnlinePush.ReqPush.kt | 72 ++++----- 6 files changed, 55 insertions(+), 331 deletions(-) diff --git a/mirai-core-api/src/commonMain/kotlin/event/events/bot.kt b/mirai-core-api/src/commonMain/kotlin/event/events/bot.kt index 4c2c7e9aa..395d77ef8 100644 --- a/mirai-core-api/src/commonMain/kotlin/event/events/bot.kt +++ b/mirai-core-api/src/commonMain/kotlin/event/events/bot.kt @@ -136,149 +136,24 @@ public data class BotNickChangedEvent( ) : BotEvent, Packet, AbstractEvent() -@MiraiExperimentalApi -public sealed class BotNudgedEvent : AbstractEvent(), BotEvent, Packet { - /** - * 戳一戳的发起人,为 [Bot] 的某一好友, 或某一群员, 或 [Bot.asFriend] - */ - public abstract val from: UserOrBot - - /** 戳一戳的动作名称 */ - public abstract val action: String - - /** 戳一戳中设置的自定义后缀 */ - public abstract val suffix: String - - @MiraiExperimentalApi - /** [Bot] 在群聊中被戳 */ - public sealed class InGroup : BotNudgedEvent(), GroupMemberEvent { - abstract override val from: Member - override val bot: Bot get() = from.bot - - /** [Bot] 在 [Group] 中被 [Member] 戳了 */ - public data class ByMember internal constructor( - override val action: String, - override val suffix: String, - override val member: Member - ) : InGroup() { - override val from: Member - get() = member - - override fun toString(): String { - return "BotNudgedEvent.InGroup.ByMember(member=$member, action=$action, suffix=$suffix)" - } - } - - /** [Bot] 在 [Group] 中自己戳了自己 */ - public data class ByBot internal constructor( - override val action: String, - override val suffix: String, - override val group: Group - ) : InGroup() { - override val member: Member get() = group.botAsMember - override val from: Member get() = member - - override fun toString(): String { - return "BotNudgedEvent.InGroup.ByBot(group=$group, action=$action, suffix=$suffix)" - } - } - } - - @MiraiExperimentalApi - /** [Bot] 在私聊中被戳 */ - public sealed class InPrivateSession : BotNudgedEvent() { - abstract override val from: User - override val bot: Bot get() = from.bot - - /** 在私聊中 [Friend] 戳了 [Bot] */ - public data class ByFriend internal constructor( - override val friend: Friend, - override val action: String, - override val suffix: String - ) : InPrivateSession(), FriendEvent { - override val from: Friend get() = friend - override val bot: Bot get() = from.bot - - override fun toString(): String { - return "BotNudgedEvent.InPrivateSession.ByFriend(friend=$friend, action=$action, suffix=$suffix)" - } - } - - /** 在私聊中 [Stranger] 戳了 [Bot] */ - public data class ByStranger internal constructor( - override val stranger: Stranger, - override val action: String, - override val suffix: String - ) : InPrivateSession(), StrangerEvent { - override val from: Stranger get() = stranger - override val bot: Bot get() = stranger.bot - - override fun toString(): String { - return "BotNudgedEvent.InPrivateSession.ByFriend(friend=$stranger, action=$action, suffix=$suffix)" - } - } - - /** [Bot] 在私聊中自己戳了自己 */ - public data class ByBot internal constructor( - /** [Bot] 的对话对象 */ - /** 可能是 [Stranger] 或 [Friend] */ - val user: User, - override val action: String, - override val suffix: String - ) : InPrivateSession(), BotEvent { - override val from: User - get() = if (user is Stranger) { - bot.asStranger - } else { - bot.asFriend - } - - override fun toString(): String { - return "BotNudgedEvent.InPrivateSession.ByBot(friend=$user, action=$action, suffix=$suffix)" - } - } - } -} - -/* /** - * [Bot] 被 [戳][Nudge] 的事件. + * 戳一戳事件 */ -@MiraiExperimentalAPI -public data class BotNudgedEvent internal constructor( +public data class NudgeEvent @MiraiInternalApi constructor( /** - * 戳一戳的发起人,为 [Bot] 的某一好友, 或某一群员, 或 [Bot.selfQQ] + * 戳一戳发起人 */ - public val from: User, + public val from: UserOrBot, /** - * 戳一戳的动作名称 + * 戳一戳目标, 可能与 [from] 相同. */ + public val target: UserOrBot, + /** + * 消息语境, 同 [MessageEvent.subject]. 可能为 [Group], [Stranger], [Friend], [Member]. + */ + public val subject: Contact, public val action: String, - /** - * 戳一戳中设置的自定义后缀 - */ public val suffix: String, -) : BotEvent, Packet, AbstractEvent() { - /** - * 戳一戳的目标 - */ - public override val bot: Bot get() = from.bot - -} -*/ - -/** - * 戳一戳发起的会话环境, 可能是 [Friend] 或者 [Group] - * - * @see MessageEvent.subject - */ -@MiraiExperimentalApi -public val BotNudgedEvent.subject: ContactOrBot - get() = when (val inlineFrom = from) { - is Member -> inlineFrom.group - else -> inlineFrom - } - -// region 图片 - -// endregion +) : AbstractEvent(), BotEvent, Packet { + override val bot: Bot get() = from.bot +} \ No newline at end of file diff --git a/mirai-core-api/src/commonMain/kotlin/event/events/friend.kt b/mirai-core-api/src/commonMain/kotlin/event/events/friend.kt index 457282f7c..892eb721c 100644 --- a/mirai-core-api/src/commonMain/kotlin/event/events/friend.kt +++ b/mirai-core-api/src/commonMain/kotlin/event/events/friend.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 Mamoe Technologies and contributors. * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. @@ -21,8 +21,6 @@ import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.User import net.mamoe.mirai.event.AbstractEvent import net.mamoe.mirai.internal.network.Packet -import net.mamoe.mirai.message.action.Nudge -import net.mamoe.mirai.utils.MiraiExperimentalApi import java.util.concurrent.atomic.AtomicBoolean @@ -119,57 +117,4 @@ public data class FriendInputStatusChangedEvent internal constructor( public override val friend: Friend, public val inputting: Boolean -) : FriendEvent, Packet, AbstractEvent() - -/** - * 在 [Friend] 与 [Bot] 的对话中, [Friend] 被 [戳][Nudge] 事件 - * - * 注: 此事件仅可能在私聊中发生 - */ -@MiraiExperimentalApi -public sealed class FriendNudgedEvent : AbstractEvent(), FriendEvent, Packet { - /** - * 戳一戳的发起人, 为 [Bot] 的某一好友, 或是 [Bot.asFriend] - */ - public abstract val from: Friend - - /** - * 戳一戳的动作名称 - */ - public abstract val action: String - - /** - * 戳一戳中设置的自定义后缀 - */ - public abstract val suffix: String - - /** 在 [Bot] 与 [Friend] 的对话中 [Friend] 戳了自己事件 */ - @MiraiExperimentalApi - public data class NudgedByHimself internal constructor( - override val friend: Friend, - override val action: String, - override val suffix: String - ) : FriendNudgedEvent() { - override fun toString(): String { - return "FriendNudgedEvent.NudgedByHimself(friend=$friend, action=$action, suffix=$suffix)" - } - - override val from: Friend - get() = friend - } - - /** [Bot] 戳了 [Friend] */ - @MiraiExperimentalApi - public data class NudgedByBot internal constructor( - override val friend: Friend, - override val action: String, - override val suffix: String - ) : FriendNudgedEvent() { - override fun toString(): String { - return "FriendNudgedEvent.NudgedByBot(friend=$friend, action=$action, suffix=$suffix)" - } - - override val from: Friend - get() = bot.asFriend - } -} +) : FriendEvent, Packet, AbstractEvent() \ No newline at end of file diff --git a/mirai-core-api/src/commonMain/kotlin/event/events/group.kt b/mirai-core-api/src/commonMain/kotlin/event/events/group.kt index 6961335f4..02dbd75f7 100644 --- a/mirai-core-api/src/commonMain/kotlin/event/events/group.kt +++ b/mirai-core-api/src/commonMain/kotlin/event/events/group.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 Mamoe Technologies and contributors. * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. @@ -21,7 +21,6 @@ import net.mamoe.mirai.data.GroupHonorType import net.mamoe.mirai.event.AbstractEvent import net.mamoe.mirai.event.BroadcastControllable import net.mamoe.mirai.internal.network.Packet -import net.mamoe.mirai.message.action.Nudge import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiInternalApi import java.util.concurrent.atomic.AtomicBoolean @@ -493,34 +492,6 @@ public data class MemberUnmuteEvent @MiraiInternalApi constructor( // endregion -// region 戳一戳 - - -/** - * [Member] 被 [戳][Nudge] 的事件. - */ -@MiraiExperimentalApi -public data class MemberNudgedEvent @MiraiInternalApi constructor( - /** - * 戳一戳的发起人, 不可能是 bot - */ - public val from: Member, - /** - * 戳一戳的目标 (被戳的群员), 不可能是 bot - */ - public override val member: Member, - /** - * 戳一戳的动作名称 - */ - public val action: String, - /** - * 戳一戳中设置的自定义后缀 - */ - public val suffix: String, -) : GroupMemberEvent, BotPassiveEvent, Packet, AbstractEvent() - -// endregion - // region 群荣誉 /** * [Member] 荣誉改变时的事件, 目前只支持龙王 diff --git a/mirai-core-api/src/commonMain/kotlin/event/events/stranger.kt b/mirai-core-api/src/commonMain/kotlin/event/events/stranger.kt index 8e368dca3..3fd7df69f 100644 --- a/mirai-core-api/src/commonMain/kotlin/event/events/stranger.kt +++ b/mirai-core-api/src/commonMain/kotlin/event/events/stranger.kt @@ -1,3 +1,12 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + package net.mamoe.mirai.event.events import net.mamoe.mirai.Bot @@ -5,8 +14,6 @@ import net.mamoe.mirai.contact.Friend import net.mamoe.mirai.contact.Stranger import net.mamoe.mirai.event.AbstractEvent import net.mamoe.mirai.internal.network.Packet -import net.mamoe.mirai.message.action.Nudge -import net.mamoe.mirai.utils.MiraiExperimentalApi /** * 新增陌生人的事件 @@ -58,57 +65,4 @@ public abstract class StrangerRelationChangeEvent( public val friend: Friend ) : StrangerRelationChangeEvent(stranger) -} - -/** - * 在 [Stranger] 与 [Bot] 的对话中, [Stranger] 被 [戳][Nudge] 事件 - * - * 注: 此事件仅可能在私聊中发生 - */ -@MiraiExperimentalApi -public sealed class StrangerNudgedEvent : AbstractEvent(), StrangerEvent, Packet { - /** - * 戳一戳的发起人, 为 [Bot] 的某一好友, 或是 [Bot.asFriend] - */ - public abstract val from: Stranger - - /** - * 戳一戳的动作名称 - */ - public abstract val action: String - - /** - * 戳一戳中设置的自定义后缀 - */ - public abstract val suffix: String - - /** 在 [Bot] 与 [Stranger] 的对话中 [Stranger] 戳了自己事件 */ - @MiraiExperimentalApi - public data class NudgedByHimself internal constructor( - override val stranger: Stranger, - override val action: String, - override val suffix: String - ) : StrangerNudgedEvent() { - override fun toString(): String { - return "StrangerNudgedEvent.NudgedByHimself(stranger=$stranger, action=$action, suffix=$suffix)" - } - - override val from: Stranger - get() = stranger - } - - /** [Bot] 戳了 [Stranger] */ - @MiraiExperimentalApi - public data class NudgedByBot internal constructor( - override val stranger: Stranger, - override val action: String, - override val suffix: String - ) : StrangerNudgedEvent() { - override fun toString(): String { - return "StrangerNudgedEvent.NudgedByBot(stranger=$stranger, action=$action, suffix=$suffix)" - } - - override val from: Stranger - get() = bot.asStranger - } } \ No newline at end of file diff --git a/mirai-core-api/src/commonMain/kotlin/message/action/Nudge.kt b/mirai-core-api/src/commonMain/kotlin/message/action/Nudge.kt index f335824b9..b37d29fc5 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/action/Nudge.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/action/Nudge.kt @@ -12,8 +12,7 @@ import net.mamoe.kjbb.JvmBlockingBridge import net.mamoe.mirai.Bot import net.mamoe.mirai.Mirai import net.mamoe.mirai.contact.* -import net.mamoe.mirai.event.events.BotNudgedEvent -import net.mamoe.mirai.event.events.MemberNudgedEvent +import net.mamoe.mirai.event.events.NudgeEvent import net.mamoe.mirai.utils.BotConfiguration import net.mamoe.mirai.utils.BotConfiguration.MiraiProtocol @@ -42,8 +41,7 @@ public sealed class Nudge { * @return 成功发送时为 `true`. 若对方禁用 "戳一戳" 功能, 返回 `false`. * @throws UnsupportedOperationException 当未使用 [安卓协议][MiraiProtocol.ANDROID_PHONE] 时抛出 * - * @see MemberNudgedEvent 成员被戳事件 - * @see BotNudgedEvent [Bot] 被戳事件 + * @see NudgeEvent 事件 * @see Contact.sendNudge */ @JvmBlockingBridge @@ -62,8 +60,7 @@ public sealed class Nudge { * * @throws UnsupportedOperationException 当未使用 [安卓协议][MiraiProtocol.ANDROID_PHONE] 时抛出 * - * @see MemberNudgedEvent 成员被戳事件 - * @see BotNudgedEvent [Bot] 被戳事件 + * @see NudgeEvent 事件 */ @JvmSynthetic @JvmStatic diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.ReqPush.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.ReqPush.kt index 8e7529fbd..1ea64c5f2 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.ReqPush.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.ReqPush.kt @@ -268,15 +268,15 @@ private object Transformers732 : Map by mapOf( } } } - if (target.id == bot.id) { - return@lambda732 sequenceOf( - if (from.id == bot.id) - BotNudgedEvent.InGroup.ByBot(action, suffix, group) - else - BotNudgedEvent.InGroup.ByMember(action, suffix, from) + return@lambda732 sequenceOf( + NudgeEvent( + from = if (from.id == bot.id) bot else from, + target = if (target.id == bot.id) bot else target, + action = action, + suffix = suffix, + subject = group ) - } - return@lambda732 sequenceOf(MemberNudgedEvent(from, target, action, suffix)) + ) } //龙王 10093L, 1053L, 1054L -> { @@ -567,8 +567,8 @@ internal object Transformers528 : Map by mapOf( var action = "" var target: User? = null var suffix = "" - body.msgTemplParam.asSequence().map { - it.name.decodeToString() to it.value.decodeToString() + body.msgTemplParam.asSequence().map { param -> + param.name.decodeToString() to param.value.decodeToString() }.forEach { (key, value) -> when (key) { "action_str" -> action = value @@ -579,44 +579,26 @@ internal object Transformers528 : Map by mapOf( "suffix_str" -> suffix = value } } - val subject: User = bot.getFriend(msgInfo.lFromUin) ?: bot.getStranger(msgInfo.lFromUin) - ?: return@lambda528 emptySequence() - //机器人自己戳自己 - if ((target == null && from == null) || (target?.id == from?.id && from?.id == bot.id)) { - sequenceOf(BotNudgedEvent.InPrivateSession.ByBot(subject, action, suffix)) - } else sequenceOf( - when (subject) { - is Friend -> when { - //机器人自身为目标 - target == null || target!!.id == bot.id -> BotNudgedEvent.InPrivateSession.ByFriend( - subject, - action, - suffix - ) - //机器人自身为发起者 - from == null || from!!.id == bot.id -> FriendNudgedEvent.NudgedByBot( - subject, - action, - suffix - ) - else -> FriendNudgedEvent.NudgedByHimself(subject, action, suffix) + + val subject: User = bot.getFriend(msgInfo.lFromUin) + ?: bot.getStranger(msgInfo.lFromUin) + ?: return@lambda528 emptySequence() + + sequenceOf( + when { + target == null && from == null || target?.id == from?.id && from?.id == bot.id -> { + //机器人自己戳自己 + NudgeEvent(from = bot, target = bot, subject = subject, action, suffix) } - is Stranger -> when { + target == null || target!!.id == bot.id -> { //机器人自身为目标 - target == null || target!!.id == bot.id -> BotNudgedEvent.InPrivateSession.ByStranger( - subject, - action, - suffix - ) - //机器人自身为发起者 - from == null || from!!.id == bot.id -> StrangerNudgedEvent.NudgedByBot( - subject, - action, - suffix - ) - else -> StrangerNudgedEvent.NudgedByHimself(subject, action, suffix) + NudgeEvent(from = subject, target = bot, subject = subject, action, suffix) } - else -> error("Internal Error: Unable to find nudge type") + from == null || from!!.id == bot.id -> { + //机器人自身为发起者 + NudgeEvent(from = bot, target = subject, subject = subject, action, suffix) + } + else -> NudgeEvent(from = subject, target = subject, subject = subject, action, suffix) } ) }