Skip to content

Commit 4b9ce01

Browse files
authored
Merge pull request #167 from simple-robot/dev/support-msg-from-ref
KookMessageContent 和 KookBot 支持根据引用查询消息内容的相关API
2 parents f81d7e7 + 6564cb8 commit 4b9ce01

File tree

3 files changed

+105
-10
lines changed

3 files changed

+105
-10
lines changed

simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/bot/KookBot.kt

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
/*
2-
* Copyright (c) 2023. ForteScarlet.
2+
* Copyright (c) 2023-2024. ForteScarlet.
33
*
4-
* This file is part of simbot-component-kook.
4+
* This file is part of simbot-component-kook.
55
*
6-
* simbot-component-kook is free software: you can redistribute it and/or modify it under the terms of
7-
* the GNU Lesser General Public License as published by the Free Software Foundation,
8-
* either version 3 of the License, or (at your option) any later version.
6+
* simbot-component-kook is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
910
*
10-
* simbot-component-kook is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11-
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12-
* See the GNU Lesser General Public License for more details.
11+
* simbot-component-kook is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
1315
*
14-
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-kook,
15-
* If not, see <https://www.gnu.org/licenses/>.
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with simbot-component-kook,
18+
* If not, see <https://www.gnu.org/licenses/>.
1619
*/
1720

1821
package love.forte.simbot.component.kook.bot
@@ -25,20 +28,25 @@ import love.forte.simbot.bot.GuildRelation
2528
import love.forte.simbot.common.collectable.Collectable
2629
import love.forte.simbot.common.id.ID
2730
import love.forte.simbot.common.id.StringID.Companion.ID
31+
import love.forte.simbot.common.id.literal
2832
import love.forte.simbot.component.kook.KookComponent
2933
import love.forte.simbot.component.kook.KookGuild
3034
import love.forte.simbot.component.kook.KookUserChat
3135
import love.forte.simbot.component.kook.message.KookAsset
3236
import love.forte.simbot.component.kook.message.KookAssetImage
37+
import love.forte.simbot.component.kook.message.KookChannelMessageDetailsContent.Companion.toContent
38+
import love.forte.simbot.component.kook.message.KookMessageContent
3339
import love.forte.simbot.component.kook.util.requestDataBy
3440
import love.forte.simbot.kook.api.ApiResponseException
3541
import love.forte.simbot.kook.api.ApiResultException
3642
import love.forte.simbot.kook.api.asset.Asset
3743
import love.forte.simbot.kook.api.asset.CreateAssetApi
44+
import love.forte.simbot.kook.api.message.GetChannelMessageViewApi
3845
import love.forte.simbot.kook.api.userchat.GetUserChatListApi
3946
import love.forte.simbot.kook.messages.MessageType
4047
import love.forte.simbot.kook.stdlib.Ticket
4148
import love.forte.simbot.logger.Logger
49+
import love.forte.simbot.message.MessageReference
4250
import love.forte.simbot.suspendrunner.ST
4351
import love.forte.simbot.suspendrunner.STP
4452
import kotlin.coroutines.CoroutineContext
@@ -162,6 +170,23 @@ public interface KookBot : Bot, CoroutineScope {
162170
@Deprecated("Unsupported in KOOK", ReplaceWith("null"))
163171
override val groupRelation: GroupRelation?
164172
get() = null
173+
174+
/**
175+
* 根据引用ID查询对应的 **频道消息** 的 [KookMessageContent]。
176+
*/
177+
@ST
178+
override suspend fun messageFromId(id: ID): KookMessageContent {
179+
val view = GetChannelMessageViewApi.create(id.literal).requestDataBy(this)
180+
return view.toContent(this)
181+
}
182+
183+
/**
184+
* 根据引用查询对应的 **频道消息** 的 [KookMessageContent]。
185+
*/
186+
@ST
187+
override suspend fun messageFromReference(reference: MessageReference): KookMessageContent {
188+
return messageFromId(reference.id)
189+
}
165190
}
166191

167192
/**

simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/message/KookChannelMessageDetailsContent.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ public data class KookChannelMessageDetailsContent internal constructor(
100100
return details.quote?.asMessage()
101101
}
102102

103+
override suspend fun referenceMessage(): KookMessageContent? {
104+
val quote = details.quote ?: return null
105+
val view = GetChannelMessageViewApi.create(quote.id).requestDataBy(bot)
106+
return view.toContent(bot)
107+
}
108+
103109
/**
104110
* 删除当前的频道消息。
105111
*

simbot-component-kook-core/src/commonMain/kotlin/love/forte/simbot/component/kook/message/KookReceiveMessageContent.kt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import love.forte.simbot.common.id.IntID.Companion.ID
3030
import love.forte.simbot.common.id.StringID.Companion.ID
3131
import love.forte.simbot.component.kook.bot.KookBot
3232
import love.forte.simbot.component.kook.message.KookAttachmentMessage.Companion.asMessage
33+
import love.forte.simbot.component.kook.message.KookChannelMessageDetailsContent.Companion.toContent
3334
import love.forte.simbot.component.kook.message.KookMessages.AT_TYPE_ROLE
3435
import love.forte.simbot.component.kook.message.KookMessages.AT_TYPE_USER
3536
import love.forte.simbot.component.kook.message.KookQuote.Companion.asMessage
@@ -46,6 +47,8 @@ import love.forte.simbot.kook.api.message.GetChannelMessageViewApi
4647
import love.forte.simbot.kook.api.message.GetDirectMessageViewApi
4748
import love.forte.simbot.kook.api.userchat.CreateUserChatApi
4849
import love.forte.simbot.kook.event.*
50+
import love.forte.simbot.kook.messages.ChannelMessageDetails
51+
import love.forte.simbot.kook.messages.DirectMessageDetails
4952
import love.forte.simbot.kook.objects.card.CardMessage
5053
import love.forte.simbot.logger.Logger
5154
import love.forte.simbot.logger.LoggerFactory
@@ -159,6 +162,12 @@ public interface KookMessageContent : MessageContent, DeleteSupport {
159162
@STP
160163
override suspend fun reference(): KookQuote?
161164

165+
/**
166+
* 根据 [消息引用][reference] 查询其对应的消息原内容。
167+
*/
168+
@STP
169+
override suspend fun referenceMessage(): KookMessageContent?
170+
162171
/**
163172
* 尝试根据当前消息ID删除目标。
164173
*
@@ -192,6 +201,20 @@ private suspend fun referenceFromDirectWithChatCode(bot: KookBot, msgId: String,
192201
return details.quote?.asMessage()
193202
}
194203

204+
private suspend fun detailsFromChannel(bot: KookBot, msgId: String): ChannelMessageDetails {
205+
val api = GetChannelMessageViewApi.create(msgId)
206+
return bot.requestData(api)
207+
}
208+
209+
private suspend fun detailsFromDirect(bot: KookBot, msgId: String, authorId: String): DirectMessageDetails {
210+
val chat = CreateUserChatApi.create(authorId).requestDataBy(bot)
211+
return detailsFromDirectWithChatCode(bot, msgId, chat.code)
212+
}
213+
214+
private suspend fun detailsFromDirectWithChatCode(bot: KookBot, msgId: String, chatCode: String): DirectMessageDetails {
215+
return GetDirectMessageViewApi.create(chatCode, msgId).requestDataBy(bot)
216+
}
217+
195218
/**
196219
* KOOK 消息事件所收到的消息正文类型。
197220
*
@@ -239,6 +262,26 @@ public class KookReceiveMessageContent internal constructor(
239262
}
240263
}
241264

265+
override suspend fun referenceMessage(): KookMessageContent {
266+
return if (isDirect) {
267+
val details = detailsFromDirect(bot, source.msgId, source.authorId)
268+
KookUpdatedMessageContent(
269+
bot = bot,
270+
isDirect = false,
271+
chatCode = null,
272+
rawContent = details.content,
273+
msgId = details.id,
274+
mention = emptyList(),
275+
mentionRoles = emptyList(),
276+
isMentionAll = false,
277+
isMentionHere = false
278+
)
279+
} else {
280+
val details = detailsFromChannel(bot, source.msgId)
281+
details.toContent(bot)
282+
}
283+
}
284+
242285
@JvmSynthetic
243286
override suspend fun delete(vararg options: DeleteOption) {
244287
// TODO options
@@ -300,6 +343,27 @@ public class KookUpdatedMessageContent internal constructor(
300343
}
301344
}
302345

346+
@JvmSynthetic
347+
override suspend fun referenceMessage(): KookMessageContent {
348+
return if (isDirect) {
349+
val details = detailsFromDirectWithChatCode(bot, msgId, chatCode!!)
350+
KookUpdatedMessageContent(
351+
bot = bot,
352+
isDirect = false,
353+
chatCode = null,
354+
rawContent = details.content,
355+
msgId = details.id,
356+
mention = emptyList(),
357+
mentionRoles = emptyList(),
358+
isMentionAll = false,
359+
isMentionHere = false
360+
)
361+
} else {
362+
val details = detailsFromChannel(bot, msgId)
363+
details.toContent(bot)
364+
}
365+
}
366+
303367
@JvmSynthetic
304368
override suspend fun delete(vararg options: DeleteOption) {
305369
// TODO options

0 commit comments

Comments
 (0)