Migrate to new GroupTempMessageEvent

This commit is contained in:
Him188 2021-01-11 19:26:15 +08:00
parent 564c961d3f
commit d1c9acf7f4
10 changed files with 84 additions and 40 deletions

View File

@ -94,8 +94,8 @@ public interface Member : User {
* @see FriendMessagePreSendEvent 当此成员是好友时发送消息前事件
* @see FriendMessagePostSendEvent 当此成员是好友时发送消息后事件
*
* @see TempMessagePreSendEvent 当此成员不是好友时发送消息前事件
* @see TempMessagePostSendEvent 当此成员不是好友时发送消息后事件
* @see GroupTempMessagePreSendEvent 当此成员不是好友时发送消息前事件
* @see GroupTempMessagePostSendEvent 当此成员不是好友时发送消息后事件
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出

View File

@ -98,8 +98,8 @@ public interface NormalMember : Member {
* @see FriendMessagePreSendEvent 当此成员是好友时发送消息前事件
* @see FriendMessagePostSendEvent 当此成员是好友时发送消息后事件
*
* @see TempMessagePreSendEvent 当此成员不是好友时发送消息前事件
* @see TempMessagePostSendEvent 当此成员不是好友时发送消息后事件
* @see GroupTempMessagePreSendEvent 当此成员不是好友时发送消息前事件
* @see GroupTempMessagePostSendEvent 当此成员不是好友时发送消息后事件
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出

View File

@ -86,7 +86,7 @@ public interface MessageEvent : Event, Packet, BotPassiveEvent { // TODO: 2021/1
* 来自 [User] 的消息
*
* @see FriendMessageEvent
* @see TempMessageEvent
* @see GroupTempMessageEvent
*/
public interface UserMessageEvent : MessageEvent {
public override val subject: User
@ -146,7 +146,7 @@ public class OtherClientMessageEvent constructor(
* 来自一个可以知道其 [Group] 的用户消息
*
* @see FriendMessageEvent
* @see TempMessageEvent
* @see GroupTempMessageEvent
*/
public interface GroupAwareMessageEvent : MessageEvent {
public val group: Group
@ -198,21 +198,7 @@ public sealed class TempMessageEvent constructor(
public override val sender: NormalMember,
public override val message: MessageChain,
public override val time: Int
) : AbstractMessageEvent(), GroupAwareMessageEvent, UserMessageEvent {
init {
val source = message[MessageSource] ?: error("Cannot find MessageSource from message")
check(source is OnlineMessageSource.Incoming.FromTemp) { "source provided to a TempMessageEvent must be an instance of OnlineMessageSource.Incoming.FromTemp" }
}
public override val bot: Bot get() = sender.bot
public override val subject: NormalMember get() = sender
public override val group: Group get() = sender.group
public override val senderName: String get() = sender.nameCardOrNick
public override val source: OnlineMessageSource.Incoming.FromTemp get() = message.source as OnlineMessageSource.Incoming.FromTemp
public override fun toString(): String =
"TempMessageEvent(sender=${sender.id} from group(${sender.group.id}), message=$message)"
}
) : AbstractMessageEvent(), GroupAwareMessageEvent, UserMessageEvent
// support by mirai 2.1
//
@ -233,7 +219,7 @@ public class GroupTempMessageEvent(
) : @Suppress("DEPRECATION_ERROR") TempMessageEvent(sender, message, time), GroupAwareMessageEvent, UserMessageEvent {
init {
val source = message[MessageSource] ?: error("Cannot find MessageSource from message")
check(source is OnlineMessageSource.Incoming.FromTemp) { "source provided to a TempMessageEvent must be an instance of OnlineMessageSource.Incoming.FromTemp" }
check(source is OnlineMessageSource.Incoming.FromTemp) { "source provided to a GroupTempMessageEvent must be an instance of OnlineMessageSource.Incoming.FromTemp" }
}
public override val bot: Bot get() = sender.bot

View File

@ -147,7 +147,15 @@ public data class FriendMessagePostSendEvent @MiraiInternalApi constructor(
* 在群临时会话消息发送后广播的事件.
* @see MessagePostSendEvent
*/
public data class TempMessagePostSendEvent @MiraiInternalApi constructor(
@Deprecated(
"mirai 正计划支持其他渠道发起的临时会话, 届时此事件会变动. 原 TempMessagePostSendEvent 已更改为 GroupTempMessagePostSendEvent",
replaceWith = ReplaceWith(
"GroupTempMessagePostSendEvent",
"net.mamoe.mirai.event.events.GroupTempMessagePostSendEvent"
),
DeprecationLevel.ERROR
)
public sealed class TempMessagePostSendEvent @MiraiInternalApi constructor(
/** 发信目标. */
public override val target: Member,
/** 待发送的消息. 此为 [MessagePreSendEvent.message] 的最终值. */
@ -163,7 +171,30 @@ public data class TempMessagePostSendEvent @MiraiInternalApi constructor(
*/
public override val receipt: MessageReceipt<Member>?
) : UserMessagePostSendEvent<Member>() {
public val group: Group get() = target.group
public open val group: Group get() = target.group
}
/**
* 在群临时会话消息发送后广播的事件.
* @see MessagePostSendEvent
*/
public data class GroupTempMessagePostSendEvent @MiraiInternalApi constructor(
/** 发信目标. */
public override val target: Member,
/** 待发送的消息. 此为 [MessagePreSendEvent.message] 的最终值. */
public override val message: MessageChain,
/**
* 发送消息时抛出的异常. `null` 表示消息成功发送.
* @see result
*/
public override val exception: Throwable?,
/**
* 发送消息成功时的回执. `null` 表示消息发送失败.
* @see result
*/
public override val receipt: MessageReceipt<Member>?
) : @kotlin.Suppress("DEPRECATION_ERROR") TempMessagePostSendEvent(target, message, exception, receipt) {
public override val group: Group get() = target.group
}
/**

View File

@ -73,16 +73,37 @@ public data class FriendMessagePreSendEvent @MiraiInternalApi constructor(
) : UserMessagePreSendEvent()
/**
* 在发送临时会话消息前广播的事件.
* 在发送临时会话消息前广播的事件.
* @see MessagePreSendEvent
*/
public data class TempMessagePreSendEvent @MiraiInternalApi constructor(
@Deprecated(
"mirai 正计划支持其他渠道发起的临时会话, 届时此事件会变动. 原 TempMessagePreSendEvent 已更改为 GroupTempMessagePreSendEvent",
replaceWith = ReplaceWith(
"GroupTempMessagePreSendEvent",
"net.mamoe.mirai.event.events.GroupTempMessagePreSendEvent"
),
DeprecationLevel.ERROR
)
public sealed class TempMessagePreSendEvent @MiraiInternalApi constructor(
/** 发信目标. */
public override val target: Member,
/** 待发送的消息. 修改后将会同时应用于发送. */
public override var message: Message
) : UserMessagePreSendEvent() {
public val group: Group get() = target.group
public open val group: Group get() = target.group
}
/**
* 在发送群临时会话消息前广播的事件.
* @see MessagePreSendEvent
*/
public data class GroupTempMessagePreSendEvent @MiraiInternalApi constructor(
/** 发信目标. */
public override val target: Member,
/** 待发送的消息. 修改后将会同时应用于发送. */
public override var message: Message
) : @kotlin.Suppress("DEPRECATION_ERROR") TempMessagePreSendEvent(target, message) {
public override val group: Group get() = target.group
}
/**

View File

@ -28,15 +28,21 @@
- 被动收到消息:[MessageEvent](MessageEvent.kt)
- 群消息GroupMessageEvent
- 好友消息FriendMessageEvent
- 群临时会话消息TempMessageEvent
- 群临时会话消息GroupTempMessageEvent
- 陌生人消息StrangerMessageEvent
- 其他客户端消息OtherClientMessageEvent
- 主动发送消息前: [MessagePreSendEvent](MessagePreSendEvent.kt)
- 群消息: GroupMessagePreSendEvent
- 好友消息: FriendMessagePreSendEvent
- 群临时会话消息: TempMessagePreSendEvent
- 群临时会话消息: GroupTempMessagePreSendEvent
- 陌生人消息StrangerMessagePreSendEvent
- 其他客户端消息OtherClientMessagePreSendEvent
- 主动发送消息后: [MessagePostSendEvent](MessagePostSendEvent.kt)
- 群消息: GroupMessagePostSendEvent
- 好友消息: FriendMessagePostSendEvent
- 群临时会话消息: TempMessagePostSendEvent
- 群临时会话消息: GroupTempMessagePostSendEvent
- 陌生人消息StrangerMessagePostSendEvent
- 其他客户端消息OtherClientMessagePostSendEvent
- 消息撤回: [MessageRecallEvent](MessageRecallEvent.kt)
- 好友撤回: FriendRecall
- 群撤回: GroupRecall

View File

@ -69,7 +69,7 @@ public typealias MessageEventSubscribersBuilder = MessageSubscribersBuilder<Mess
*
* @see subscribeFriendMessages
* @see subscribeGroupMessages
* @see subscribeTempMessages
* @see subscribeGroupTempMessages
* @see subscribeOtherClientMessages
* @see subscribeStrangerMessages
*/

View File

@ -378,7 +378,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
time: Int
): Boolean = bot.asQQAndroidBot().run {
val response = network.run {
PbMessageSvc.PbMsgWithDraw.createForTempMessage(
PbMessageSvc.PbMsgWithDraw.createForGroupTempMessage(
client,
groupUin,
targetId,
@ -447,7 +447,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
"can only recall a message sent by bot"
}
source as OnlineMessageSourceToTempImpl
PbMessageSvc.PbMsgWithDraw.createForTempMessage(
PbMessageSvc.PbMsgWithDraw.createForGroupTempMessage(
bot.client,
(source.target.group as GroupImpl).uin,
source.targetId,
@ -474,7 +474,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
check(source.fromId == bot.id) {
"can only recall a message sent by bot"
}
PbMessageSvc.PbMsgWithDraw.createForTempMessage(
PbMessageSvc.PbMsgWithDraw.createForGroupTempMessage(
bot.client,
source.targetId, // groupUin
source.targetId, // memberUin

View File

@ -69,13 +69,13 @@ internal class NormalMemberImpl constructor(
private suspend fun sendMessageImpl(message: Message): MessageReceipt<NormalMember> {
val chain = kotlin.runCatching {
TempMessagePreSendEvent(this, message).broadcast()
GroupTempMessagePreSendEvent(this, message).broadcast()
}.onSuccess {
check(!it.isCancelled) {
throw EventCancelledException("cancelled by TempMessagePreSendEvent")
throw EventCancelledException("cancelled by GroupTempMessagePreSendEvent")
}
}.getOrElse {
throw EventCancelledException("exception thrown when broadcasting TempMessagePreSendEvent", it)
throw EventCancelledException("exception thrown when broadcasting GroupTempMessagePreSendEvent", it)
}.message.toMessageChain()
chain.firstIsInstanceOrNull<QuoteReply>()?.source?.ensureSequenceIdAvailable()
@ -98,10 +98,10 @@ internal class NormalMemberImpl constructor(
result.fold(
onSuccess = {
TempMessagePostSendEvent(this, chain, null, it)
GroupTempMessagePostSendEvent(this, chain, null, it)
},
onFailure = {
TempMessagePostSendEvent(this, chain, it, null)
GroupTempMessagePostSendEvent(this, chain, it, null)
}
).broadcast()

View File

@ -79,7 +79,7 @@ internal class PbMessageSvc {
}
}
fun createForTempMessage(
fun createForGroupTempMessage(
client: QQAndroidClient,
groupUin: Long,
toUin: Long,