Skip to content

Commit

Permalink
feat: support configure ImageData.toReadable (#96)
Browse files Browse the repository at this point in the history
Co-authored-by: Samarium <[email protected]>
  • Loading branch information
heartalborada-del and Samarium150 authored Feb 26, 2024
1 parent d911944 commit 8c8fd41
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/main/kotlin/command/Lolicon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ object Lolicon : CompositeCommand(
for (imageData in response.data) {
when {
imageData.urls.size > 1 -> {
imageMsgBuilder.add(contact.bot, PlainText(imageData.toReadable(imageData.urls)))
imageMsgBuilder.add(contact.bot, PlainText(imageData.toReadable()))
for (url in imageData.urls.values) {
runCatching {
val stream = getImageInputStream(url)
Expand All @@ -198,12 +198,12 @@ object Lolicon : CompositeCommand(
runCatching {
val stream = getImageInputStream(imageData.urls.values.first())
val image = contact.uploadImage(stream)
imageMsgBuilder.add(contact.bot, PlainText(imageData.toReadable(imageData.urls)))
imageMsgBuilder.add(contact.bot, PlainText(imageData.toReadable()))
imageMsgBuilder.add(contact.bot, image)
stream
}.onFailure {
logger.error(it)
imageMsgBuilder.add(contact.bot, PlainText(imageData.toReadable(imageData.urls)))
imageMsgBuilder.add(contact.bot, PlainText(imageData.toReadable()))
imageMsgBuilder.add(contact.bot, PlainText(ReplyConfig.networkError))
}.onSuccess {
runInterruptible(Dispatchers.IO) {
Expand Down Expand Up @@ -246,7 +246,7 @@ object Lolicon : CompositeCommand(
logger.error(it)
sendMessage(ReplyConfig.networkError)
}.onSuccess {
imageInfoMsgBuilder.add(imageData.toReadable(imageData.urls))
imageInfoMsgBuilder.add(imageData.toReadable())
imageInfoMsgBuilder.add("\n")
runInterruptible(Dispatchers.IO) {
it.close()
Expand All @@ -268,7 +268,7 @@ object Lolicon : CompositeCommand(
logger.error(it)
sendMessage(ReplyConfig.networkError)
}.onSuccess {
imageInfoMsgBuilder.add(imageData.toReadable(imageData.urls))
imageInfoMsgBuilder.add(imageData.toReadable())
imageInfoMsgBuilder.add("\n")
runInterruptible(Dispatchers.IO) {
it.close()
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/config/ReplyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ import net.mamoe.mirai.console.data.value

object ReplyConfig : AutoSavePluginConfig("ReplyConfig") {

@ValueDescription(
"""
图片信息消息模板, 可用占位符:
标题 {Title}, 图片PID {PID}, 图片标签 {Tags}
作者名称 {Author}, 作者UID {UID}
P站链接 {PixivURL}, 图片代理链接 {ProxyURLs}
"""
)
val imageDataMessageTemplate: String by value(
"""
标题: {Title}
作者: {Author} (uid: {UID})
标签: {Tags}
链接: {PixivURL}
代理链接:{ProxyURLs}
""".trimIndent()
)

@ValueDescription("API返回了错误")
val invokeException: String by value("API调用错误,请检查日志")

Expand Down
26 changes: 12 additions & 14 deletions src/main/kotlin/data/ImageData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package io.github.samarium150.mirai.plugin.lolicon.data

import io.github.samarium150.mirai.plugin.lolicon.config.ReplyConfig
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
Expand All @@ -37,23 +38,20 @@ data class ImageData(
val urls: Map<String, String>
) {

private val template = """
标题: $title
作者: $author (uid: ${uid})
标签: $tags
链接: https://pixiv.net/artworks/${pid}
代理链接:
""".trimIndent()

override fun toString(): String {
return "ImageData" + Json.encodeToString(this)
}

fun toReadable(url: String): String {
return template + url
}

fun toReadable(urls: Map<String, String>): String {
return template + urls.toString()
fun toReadable(url: String? = null): String {
return StringBuilder().append(
ReplyConfig.imageDataMessageTemplate
.replace("{Title}", title, ignoreCase = true)
.replace("{Author}", author, ignoreCase = true)
.replace("{UID}", uid.toString(), ignoreCase = true)
.replace("{Tags}", tags.toString(), ignoreCase = true)
.replace("{PID}", pid.toString(), ignoreCase = true)
.replace("{PixivURL}", "https://pixiv.net/artworks/${pid}", ignoreCase = true)
.replace("{ProxyUrls}", url ?: urls.toString(), ignoreCase = true)
).toString()
}
}

0 comments on commit 8c8fd41

Please sign in to comment.