mirror of
https://github.com/jie65535/mirai-console-jnr-plugin.git
synced 2025-12-15 18:41:35 +08:00
实现戳一戳间隔时间 (#7)
This commit is contained in:
@@ -14,6 +14,7 @@ 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 top.jie65535.jnr.JNudgeReply.reload
|
||||
|
||||
object JNRCommand : CompositeCommand(
|
||||
JNudgeReply, "jnr",
|
||||
@@ -107,4 +108,10 @@ object JNRCommand : CompositeCommand(
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand
|
||||
@Description("重载配置")
|
||||
suspend fun CommandSender.reload() {
|
||||
JNRPluginConfig.reload()
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,10 @@ object JNRPluginConfig : AutoSavePluginConfig("jnr") {
|
||||
* 优先级 默认为高
|
||||
* @see EventPriority
|
||||
*/
|
||||
@ValueDescription("事件优先级 从高到低可选 HIGHEST, HIGH, NORMAL, LOW, LOWEST, MONITOR\n" +
|
||||
"设置后需要重启插件生效")
|
||||
@ValueDescription(
|
||||
"事件优先级 从高到低可选 HIGHEST, HIGH, NORMAL, LOW, LOWEST, MONITOR\n" +
|
||||
"设置后需要重启插件生效"
|
||||
)
|
||||
var priority: EventPriority by value(EventPriority.HIGH)
|
||||
|
||||
/**
|
||||
@@ -31,4 +33,18 @@ object JNRPluginConfig : AutoSavePluginConfig("jnr") {
|
||||
*/
|
||||
@ValueDescription("是否拦截事件 回复后可阻止其它插件响应戳一戳事件 优先级为MONITOR时拦截无效")
|
||||
var isIntercept: Boolean by value(true)
|
||||
|
||||
/**
|
||||
* 群间隔时间(单位秒)
|
||||
* 0 表示无限制
|
||||
*/
|
||||
@ValueDescription("群回复间隔(秒),0表示无限制")
|
||||
var groupInterval: Long by value(0L)
|
||||
|
||||
/**
|
||||
* 用户间隔(单位秒)
|
||||
* 0 表示无限制
|
||||
*/
|
||||
@ValueDescription("用户私聊回复间隔(秒),0表示无限制")
|
||||
var userInterval: Long by value(0L)
|
||||
}
|
||||
@@ -5,11 +5,13 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||
import net.mamoe.mirai.contact.Group
|
||||
import net.mamoe.mirai.contact.Member
|
||||
import net.mamoe.mirai.contact.nameCardOrNick
|
||||
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.utils.info
|
||||
import java.time.LocalDateTime
|
||||
import kotlin.random.Random
|
||||
|
||||
object JNudgeReply : KotlinPlugin(
|
||||
@@ -22,6 +24,9 @@ object JNudgeReply : KotlinPlugin(
|
||||
info("""自定义戳一戳回复插件""")
|
||||
}
|
||||
) {
|
||||
private val groupLastReply = mutableMapOf<Long, LocalDateTime>()
|
||||
private val userLastReply = mutableMapOf<Long, LocalDateTime>()
|
||||
|
||||
override fun onEnable() {
|
||||
JNRPluginConfig.reload()
|
||||
JNRCommand.register()
|
||||
@@ -29,25 +34,52 @@ object JNudgeReply : KotlinPlugin(
|
||||
globalEventChannel().subscribeAlways<NudgeEvent>(priority = JNRPluginConfig.priority) {
|
||||
if (target.id == bot.id && target.id != from.id && JNRPluginConfig.replyMessageList.isNotEmpty()) {
|
||||
var replyList: List<ReplyMessage> = JNRPluginConfig.replyMessageList
|
||||
if(subject !is Group){
|
||||
val now = LocalDateTime.now()
|
||||
var isReply = true
|
||||
if (subject !is Group) {
|
||||
if (JNRPluginConfig.userInterval > 0) {
|
||||
val t = userLastReply[subject.id]
|
||||
if (t == null || t.plusSeconds(JNRPluginConfig.userInterval) < now) {
|
||||
userLastReply[subject.id] = now
|
||||
} else {
|
||||
isReply = false
|
||||
}
|
||||
}
|
||||
replyList = replyList.filter { !it.message.startsWith("#group") }
|
||||
}else{
|
||||
if((from as Member).permission.level >= (subject as Group).botPermission.level){
|
||||
} else {
|
||||
if (JNRPluginConfig.groupInterval > 0) {
|
||||
val t = groupLastReply[subject.id]
|
||||
if (t == null || t.plusSeconds(JNRPluginConfig.groupInterval) < now) {
|
||||
groupLastReply[subject.id] = now
|
||||
} else {
|
||||
isReply = false
|
||||
}
|
||||
}
|
||||
if ((from as Member).permission.level >= (subject as Group).botPermission.level) {
|
||||
replyList = replyList.filter { !it.message.startsWith("#group.mute:") }
|
||||
}
|
||||
}
|
||||
val totalWeight = replyList.sumOf { it.weight }
|
||||
var w = Random.nextInt(totalWeight)
|
||||
for (msg in replyList) {
|
||||
if (w < msg.weight) {
|
||||
doReply(msg, this)
|
||||
break
|
||||
} else {
|
||||
w -= msg.weight
|
||||
|
||||
// 判断间隔
|
||||
if (isReply) {
|
||||
val totalWeight = replyList.sumOf { it.weight }
|
||||
var w = Random.nextInt(totalWeight)
|
||||
for (msg in replyList) {
|
||||
if (w < msg.weight) {
|
||||
doReply(msg, this)
|
||||
break
|
||||
} else {
|
||||
w -= msg.weight
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logger.info("正在CD中,本次已忽略")
|
||||
}
|
||||
if (JNRPluginConfig.priority != EventPriority.MONITOR && JNRPluginConfig.isIntercept)
|
||||
|
||||
// 拦截事件
|
||||
if (JNRPluginConfig.priority != EventPriority.MONITOR && JNRPluginConfig.isIntercept) {
|
||||
intercept()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +90,10 @@ object JNudgeReply : KotlinPlugin(
|
||||
if (message.message.startsWith("#")) {
|
||||
when {
|
||||
// 戳回去
|
||||
message.message == "#nudge" -> event.from.nudge().sendTo(event.subject)
|
||||
message.message == "#nudge" -> {
|
||||
event.from.nudge().sendTo(event.subject)
|
||||
logger.info("已尝试戳回发送者")
|
||||
}
|
||||
|
||||
// 禁言
|
||||
message.message.startsWith("#group.mute:") -> {
|
||||
@@ -69,6 +104,7 @@ object JNudgeReply : KotlinPlugin(
|
||||
val member: Member = event.from as Member
|
||||
try {
|
||||
member.mute(duration)
|
||||
logger.info("戳一戳禁言目标 ${member.nameCardOrNick}(${member.id}) $duration 秒")
|
||||
} catch (e: Throwable) {
|
||||
logger.warning("戳一戳禁言失败", e)
|
||||
}
|
||||
@@ -76,7 +112,9 @@ object JNudgeReply : KotlinPlugin(
|
||||
}
|
||||
|
||||
// 忽略
|
||||
message.message == "#ignore" -> Unit
|
||||
message.message == "#ignore" -> {
|
||||
logger.info("已忽略本次戳一戳回复")
|
||||
}
|
||||
|
||||
// 其它
|
||||
else -> event.subject.sendMessage(message.message.deserializeMiraiCode())
|
||||
|
||||
Reference in New Issue
Block a user