Skip to content

Commit

Permalink
Merge pull request #167 from simple-robot/dev/support-msg-from-ref
Browse files Browse the repository at this point in the history
KookMessageContent 和 KookBot 支持根据引用查询消息内容的相关API
  • Loading branch information
ForteScarlet authored Aug 17, 2024
2 parents f81d7e7 + 6564cb8 commit 4b9ce01
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
/*
* Copyright (c) 2023. ForteScarlet.
* Copyright (c) 2023-2024. ForteScarlet.
*
* This file is part of simbot-component-kook.
* This file is part of simbot-component-kook.
*
* simbot-component-kook is free software: you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
* simbot-component-kook is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* simbot-component-kook is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* simbot-component-kook is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-kook,
* If not, see <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Lesser General Public License
* along with simbot-component-kook,
* If not, see <https://www.gnu.org/licenses/>.
*/

package love.forte.simbot.component.kook.bot
Expand All @@ -25,20 +28,25 @@ import love.forte.simbot.bot.GuildRelation
import love.forte.simbot.common.collectable.Collectable
import love.forte.simbot.common.id.ID
import love.forte.simbot.common.id.StringID.Companion.ID
import love.forte.simbot.common.id.literal
import love.forte.simbot.component.kook.KookComponent
import love.forte.simbot.component.kook.KookGuild
import love.forte.simbot.component.kook.KookUserChat
import love.forte.simbot.component.kook.message.KookAsset
import love.forte.simbot.component.kook.message.KookAssetImage
import love.forte.simbot.component.kook.message.KookChannelMessageDetailsContent.Companion.toContent
import love.forte.simbot.component.kook.message.KookMessageContent
import love.forte.simbot.component.kook.util.requestDataBy
import love.forte.simbot.kook.api.ApiResponseException
import love.forte.simbot.kook.api.ApiResultException
import love.forte.simbot.kook.api.asset.Asset
import love.forte.simbot.kook.api.asset.CreateAssetApi
import love.forte.simbot.kook.api.message.GetChannelMessageViewApi
import love.forte.simbot.kook.api.userchat.GetUserChatListApi
import love.forte.simbot.kook.messages.MessageType
import love.forte.simbot.kook.stdlib.Ticket
import love.forte.simbot.logger.Logger
import love.forte.simbot.message.MessageReference
import love.forte.simbot.suspendrunner.ST
import love.forte.simbot.suspendrunner.STP
import kotlin.coroutines.CoroutineContext
Expand Down Expand Up @@ -162,6 +170,23 @@ public interface KookBot : Bot, CoroutineScope {
@Deprecated("Unsupported in KOOK", ReplaceWith("null"))
override val groupRelation: GroupRelation?
get() = null

/**
* 根据引用ID查询对应的 **频道消息** 的 [KookMessageContent]。
*/
@ST
override suspend fun messageFromId(id: ID): KookMessageContent {
val view = GetChannelMessageViewApi.create(id.literal).requestDataBy(this)
return view.toContent(this)
}

/**
* 根据引用查询对应的 **频道消息** 的 [KookMessageContent]。
*/
@ST
override suspend fun messageFromReference(reference: MessageReference): KookMessageContent {
return messageFromId(reference.id)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public data class KookChannelMessageDetailsContent internal constructor(
return details.quote?.asMessage()
}

override suspend fun referenceMessage(): KookMessageContent? {
val quote = details.quote ?: return null
val view = GetChannelMessageViewApi.create(quote.id).requestDataBy(bot)
return view.toContent(bot)
}

/**
* 删除当前的频道消息。
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import love.forte.simbot.common.id.IntID.Companion.ID
import love.forte.simbot.common.id.StringID.Companion.ID
import love.forte.simbot.component.kook.bot.KookBot
import love.forte.simbot.component.kook.message.KookAttachmentMessage.Companion.asMessage
import love.forte.simbot.component.kook.message.KookChannelMessageDetailsContent.Companion.toContent
import love.forte.simbot.component.kook.message.KookMessages.AT_TYPE_ROLE
import love.forte.simbot.component.kook.message.KookMessages.AT_TYPE_USER
import love.forte.simbot.component.kook.message.KookQuote.Companion.asMessage
Expand All @@ -46,6 +47,8 @@ import love.forte.simbot.kook.api.message.GetChannelMessageViewApi
import love.forte.simbot.kook.api.message.GetDirectMessageViewApi
import love.forte.simbot.kook.api.userchat.CreateUserChatApi
import love.forte.simbot.kook.event.*
import love.forte.simbot.kook.messages.ChannelMessageDetails
import love.forte.simbot.kook.messages.DirectMessageDetails
import love.forte.simbot.kook.objects.card.CardMessage
import love.forte.simbot.logger.Logger
import love.forte.simbot.logger.LoggerFactory
Expand Down Expand Up @@ -159,6 +162,12 @@ public interface KookMessageContent : MessageContent, DeleteSupport {
@STP
override suspend fun reference(): KookQuote?

/**
* 根据 [消息引用][reference] 查询其对应的消息原内容。
*/
@STP
override suspend fun referenceMessage(): KookMessageContent?

/**
* 尝试根据当前消息ID删除目标。
*
Expand Down Expand Up @@ -192,6 +201,20 @@ private suspend fun referenceFromDirectWithChatCode(bot: KookBot, msgId: String,
return details.quote?.asMessage()
}

private suspend fun detailsFromChannel(bot: KookBot, msgId: String): ChannelMessageDetails {
val api = GetChannelMessageViewApi.create(msgId)
return bot.requestData(api)
}

private suspend fun detailsFromDirect(bot: KookBot, msgId: String, authorId: String): DirectMessageDetails {
val chat = CreateUserChatApi.create(authorId).requestDataBy(bot)
return detailsFromDirectWithChatCode(bot, msgId, chat.code)
}

private suspend fun detailsFromDirectWithChatCode(bot: KookBot, msgId: String, chatCode: String): DirectMessageDetails {
return GetDirectMessageViewApi.create(chatCode, msgId).requestDataBy(bot)
}

/**
* KOOK 消息事件所收到的消息正文类型。
*
Expand Down Expand Up @@ -239,6 +262,26 @@ public class KookReceiveMessageContent internal constructor(
}
}

override suspend fun referenceMessage(): KookMessageContent {
return if (isDirect) {
val details = detailsFromDirect(bot, source.msgId, source.authorId)
KookUpdatedMessageContent(
bot = bot,
isDirect = false,
chatCode = null,
rawContent = details.content,
msgId = details.id,
mention = emptyList(),
mentionRoles = emptyList(),
isMentionAll = false,
isMentionHere = false
)
} else {
val details = detailsFromChannel(bot, source.msgId)
details.toContent(bot)
}
}

@JvmSynthetic
override suspend fun delete(vararg options: DeleteOption) {
// TODO options
Expand Down Expand Up @@ -300,6 +343,27 @@ public class KookUpdatedMessageContent internal constructor(
}
}

@JvmSynthetic
override suspend fun referenceMessage(): KookMessageContent {
return if (isDirect) {
val details = detailsFromDirectWithChatCode(bot, msgId, chatCode!!)
KookUpdatedMessageContent(
bot = bot,
isDirect = false,
chatCode = null,
rawContent = details.content,
msgId = details.id,
mention = emptyList(),
mentionRoles = emptyList(),
isMentionAll = false,
isMentionHere = false
)
} else {
val details = detailsFromChannel(bot, msgId)
details.toContent(bot)
}
}

@JvmSynthetic
override suspend fun delete(vararg options: DeleteOption) {
// TODO options
Expand Down

0 comments on commit 4b9ce01

Please sign in to comment.