mirror of
https://github.com/jie65535/mirai-console-jnr-plugin.git
synced 2025-12-15 18:41:35 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c2ef779a72 | |||
| 22e8624f0b | |||
| f20fd9b807 |
@@ -8,7 +8,7 @@ MiraiConsolePlugin 自定义戳一戳回复消息
|
||||
/jnr add [weight] # 添加回复消息(权重默认为1)
|
||||
/jnr add <message> [weight] # 添加简单回复消息(权重默认为1)
|
||||
/jnr clear # 清空回复消息列表
|
||||
/jnr list # 列出当前回复消息列表
|
||||
/jnr list [page] [pageSize] # 列出当前回复消息列表,参数可翻页
|
||||
/jnr remove <index> # 删除指定索引的回复消息
|
||||
/jnr reload # 重载配置
|
||||
```
|
||||
|
||||
@@ -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.0"
|
||||
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")
|
||||
}
|
||||
17
src/main/kotlin/top/jie65535/jnr/HttpUtil.kt
Normal file
17
src/main/kotlin/top/jie65535/jnr/HttpUtil.kt
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -11,10 +11,10 @@ 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
|
||||
|
||||
object JNRCommand : CompositeCommand(
|
||||
JNudgeReply, "jnr",
|
||||
@@ -47,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) {
|
||||
@@ -64,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) {
|
||||
@@ -84,7 +119,7 @@ object JNRCommand : CompositeCommand(
|
||||
|
||||
@SubCommand
|
||||
@Description("列出当前回复消息列表")
|
||||
suspend fun CommandSender.list() {
|
||||
suspend fun CommandSender.list(page: Int = 0, pageSize: Int = 50) {
|
||||
val list = JNRPluginConfig.replyMessageList
|
||||
if (list.isEmpty()) {
|
||||
sendMessage("当前列表为空")
|
||||
@@ -97,11 +132,20 @@ object JNRCommand : CompositeCommand(
|
||||
sendMessage(sb.toString())
|
||||
}, {
|
||||
if (list.size > 1) {
|
||||
val begin = page * pageSize
|
||||
val end = min(list.size, (page + 1) * pageSize)
|
||||
if (begin < 0 || end <= begin) {
|
||||
sendMessage("翻页参数错误")
|
||||
} else {
|
||||
sendMessage(buildForwardMessage(subject) {
|
||||
for (i in list.indices) {
|
||||
for (i in begin until end) {
|
||||
bot named "[$i] (${list[i].weight})" says list[i].message.deserializeMiraiCode()
|
||||
}
|
||||
if (end < list.size) {
|
||||
bot says "当前显示 $begin~$end 共 ${list.size}"
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
sendMessage(list[0].message.deserializeMiraiCode())
|
||||
}
|
||||
|
||||
@@ -47,4 +47,9 @@ object JNRPluginConfig : AutoSavePluginConfig("jnr") {
|
||||
*/
|
||||
@ValueDescription("用户私聊回复间隔(秒),0表示无限制")
|
||||
var userInterval: Long by value(0L)
|
||||
|
||||
/**
|
||||
* 是否在间隔期间依然拦截事件,与 [isIntercept] 有关
|
||||
*/
|
||||
var interceptAtInterval: Boolean by value(true)
|
||||
}
|
||||
@@ -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.0",
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user