2 Commits

Author SHA1 Message Date
c2ef779a72 增加图片本地缓存
避免图片失效
2023-02-18 19:08:21 +08:00
22e8624f0b 更新版本到1.3.0
增加在间隔时间内是否拦截事件选项
更新依赖版本
2023-02-13 22:44:14 +08:00
5 changed files with 111 additions and 16 deletions

View File

@@ -1,16 +1,23 @@
plugins {
val kotlinVersion = "1.6.0"
val kotlinVersion = "1.7.10"
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
id("net.mamoe.mirai-console") version "2.12.2"
id("net.mamoe.mirai-console") version "2.14.0"
}
group = "top.jie65535"
version = "1.2.1"
version = "1.3.0"
repositories {
mavenLocal()
maven("https://maven.aliyun.com/repository/public")
mavenCentral()
}
val ktorVersion = "2.2.3"
dependencies {
implementation("io.ktor:ktor-client-core-jvm:$ktorVersion")
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
}

View File

@@ -0,0 +1,17 @@
package top.jie65535.jnr
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.request.*
import java.io.File
object HttpUtil {
private val httpClient = HttpClient(OkHttp)
suspend fun download(url: String, file: File): ByteArray {
val data = httpClient.get(url).body<ByteArray>()
file.writeBytes(data)
return data
}
}

View File

@@ -11,9 +11,8 @@ import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.event.globalEventChannel
import net.mamoe.mirai.event.nextEvent
import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
import net.mamoe.mirai.message.data.PlainText
import net.mamoe.mirai.message.data.buildForwardMessage
import net.mamoe.mirai.message.data.isContentBlank
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.Image.Key.queryUrl
import top.jie65535.jnr.JNudgeReply.reload
import kotlin.math.min
@@ -48,13 +47,19 @@ object JNRCommand : CompositeCommand(
if (isUser()) {
try {
sendMessage("请在${WAIT_REPLY_TIMEOUT_MS / 1000}秒内发送要添加的消息内容,你可以发送空白消息来取消。")
val msg = withTimeout(WAIT_REPLY_TIMEOUT_MS) {
val nextEvent = withTimeout(WAIT_REPLY_TIMEOUT_MS) {
subject.globalEventChannel().nextEvent<MessageEvent>(EventPriority.MONITOR) { it.sender == user }
}
if (msg.message.isContentBlank()) {
if (nextEvent.message.isContentBlank()) {
sendMessage("已取消")
} else {
JNRPluginConfig.replyMessageList.add(ReplyMessage(msg.message.serializeToMiraiCode(), weight))
if (nextEvent.message.contains(OnlineAudio.Key)) {
sendMessage("暂不支持语音消息!")
return
}
saveResources(nextEvent.message)
JNRPluginConfig.replyMessageList.add(ReplyMessage(nextEvent.message.serializeToMiraiCode(), weight))
sendMessage("已添加一条消息,权重为$weight")
}
} catch (e: TimeoutCancellationException) {
@@ -65,6 +70,35 @@ object JNRCommand : CompositeCommand(
}
}
/**
* 保存消息中的图片和音频
*/
private suspend fun saveResources(message: MessageChain) {
for (it in message) {
if (it is Image) {
val imgDir = JNudgeReply.resolveDataFile("images")
if (!imgDir.exists()) {
imgDir.mkdir()
}
val imgFile = imgDir.resolve(it.imageId)
if (!imgFile.exists()) {
JNudgeReply.logger.info("下载图片 ${it.imageId}")
HttpUtil.download(it.queryUrl(), imgFile)
}
} else if (it is OnlineAudio) {
val audioDir = JNudgeReply.resolveDataFile("audios")
if (!audioDir.exists()) {
audioDir.mkdir()
}
val audioFile = audioDir.resolve(it.filename)
if (!audioFile.exists()) {
JNudgeReply.logger.info("下载语音 ${it.filename}")
HttpUtil.download(it.urlForDownload, audioFile)
}
}
}
}
@SubCommand
@Description("删除指定索引的回复消息")
suspend fun CommandSender.remove(index: Int) {

View File

@@ -47,4 +47,9 @@ object JNRPluginConfig : AutoSavePluginConfig("jnr") {
*/
@ValueDescription("用户私聊回复间隔0表示无限制")
var userInterval: Long by value(0L)
/**
* 是否在间隔期间依然拦截事件,与 [isIntercept] 有关
*/
var interceptAtInterval: Boolean by value(true)
}

View File

@@ -3,6 +3,7 @@ package top.jie65535.jnr
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.nameCardOrNick
@@ -10,6 +11,11 @@ import net.mamoe.mirai.event.EventPriority
import net.mamoe.mirai.event.events.NudgeEvent
import net.mamoe.mirai.event.globalEventChannel
import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
import net.mamoe.mirai.message.data.Audio
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.Image.Key.isUploaded
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage
import net.mamoe.mirai.utils.info
import java.time.LocalDateTime
import kotlin.random.Random
@@ -18,7 +24,7 @@ object JNudgeReply : KotlinPlugin(
JvmPluginDescription(
id = "me.jie65535.mirai-console-jnr-plugin",
name = "J Nudge Reply",
version = "1.2.1",
version = "1.3.0",
) {
author("jie65535")
info("""自定义戳一戳回复插件""")
@@ -61,7 +67,7 @@ object JNudgeReply : KotlinPlugin(
}
// 判断间隔
if (isReply) {
val isIgnored = if (isReply) {
val totalWeight = replyList.sumOf { it.weight }
var w = Random.nextInt(totalWeight)
for (msg in replyList) {
@@ -72,18 +78,23 @@ object JNudgeReply : KotlinPlugin(
w -= msg.weight
}
}
false
} else {
logger.info("正在CD中本次已忽略")
true
}
// 拦截事件
if (JNRPluginConfig.priority != EventPriority.MONITOR && JNRPluginConfig.isIntercept) {
if (JNRPluginConfig.priority != EventPriority.MONITOR && JNRPluginConfig.isIntercept
) {
// 在被忽略的情况下判断是否拦截
if (!isIgnored || JNRPluginConfig.interceptAtInterval)
intercept()
}
}
}
logger.info { "Plugin loaded" }
logger.info { "Plugin loaded. https://github.com/jie65535/mirai-console-jnr-plugin" }
}
private suspend fun doReply(message: ReplyMessage, event: NudgeEvent) {
@@ -117,10 +128,31 @@ object JNudgeReply : KotlinPlugin(
}
// 其它
else -> event.subject.sendMessage(message.message.deserializeMiraiCode())
else -> sendRecordMessage(event.subject, message.message.deserializeMiraiCode())
}
} else {
event.subject.sendMessage(message.message.deserializeMiraiCode())
sendRecordMessage(event.subject, message.message.deserializeMiraiCode())
}
}
private suspend fun sendRecordMessage(subject: Contact, message: MessageChain) {
for (it in message) {
if (it is Image) {
if (!it.isUploaded(subject.bot)) {
val imgFile = resolveDataFile("images/" + it.imageId)
if (imgFile.exists()) {
imgFile.uploadAsImage(subject)
} else {
logger.warning(
"图片的服务器缓存已失效,本地缓存已丢失,请重新设置该消息内的图片!" +
"消息内容:" + message.serializeToMiraiCode()
)
}
}
} else if (it is Audio) {
// TODO
}
}
subject.sendMessage(message)
}
}