Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增用户可以自定义回复图片信息模板的功能 #96

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}