Skip to content

Commit

Permalink
add MessageRecorder and recall command
Browse files Browse the repository at this point in the history
  • Loading branch information
tiedanGH committed Jul 5, 2024
1 parent 04e2d4f commit 01fbb5e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/main/kotlin/MessageRecorder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.tiedan

import com.tiedan.config.BotConfig
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.event.EventHandler
import net.mamoe.mirai.event.EventPriority
import net.mamoe.mirai.event.SimpleListenerHost
import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.event.events.MessagePostSendEvent
import net.mamoe.mirai.event.events.MessageRecallEvent
import net.mamoe.mirai.event.events.source
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.message.data.QuoteReply
import net.mamoe.mirai.message.data.findIsInstance

internal object MessageRecorder : SimpleListenerHost() {

private val records: MutableMap<Long, MutableList<MessageSource>> = HashMap()

@EventHandler(priority = EventPriority.HIGHEST)
fun MessageEvent.mark() {
val record = records.getOrPut(subject.id, ::mutableListOf)
if (record.size == BotConfig.recordLimit) {
record.removeFirst()
}
record.add(source)
}

@EventHandler(priority = EventPriority.HIGHEST)
fun MessagePostSendEvent<*>.mark() {
val record = records.getOrPut(target.id, ::mutableListOf)
if (record.size == BotConfig.recordLimit) {
record.removeFirst()
}
record.add(source ?: return)
}

@EventHandler(priority = EventPriority.HIGHEST)
fun MessageRecallEvent.mark() {
when (this) {
is MessageRecallEvent.FriendRecall -> records[author.id]?.removeIf {
it.ids.contentEquals(messageIds) && it.internalIds.contentEquals(messageInternalIds)
}
is MessageRecallEvent.GroupRecall -> records[group.id]?.removeIf {
it.ids.contentEquals(messageIds) && it.internalIds.contentEquals(messageInternalIds)
}
}
}

fun from(member: Member): MessageSource? {
return records[member.group.id]?.findLast { it.fromId == member.id }
}

fun target(contact: Contact): MessageSource? {
return records[contact.id]?.findLast { it.fromId == contact.bot.id }
}

fun quote(event: MessageEvent): MessageSource? {
return event.message.findIsInstance<QuoteReply>()?.source
?: records[event.subject.id]?.findLast { it.fromId != event.source.fromId }
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/TiedanGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ object TiedanGame : KotlinPlugin(
CommandAdmin.unregister()
CommandBotHelp.unregister()
CommandTime.unregister()
CommandRecall.unregister()
CommandApply.unregister()
}

Expand All @@ -66,12 +67,14 @@ object TiedanGame : KotlinPlugin(

private fun regEvent() {
GlobalEventChannel.registerListenerHost(Events)
GlobalEventChannel.registerListenerHost(MessageRecorder)
}

private fun regCommand() {
CommandAdmin.register()
CommandBotHelp.register()
CommandTime.register()
CommandRecall.register()
CommandApply.register()
// Commandkanxi.register()
// Commandgkx.register()
Expand Down
51 changes: 51 additions & 0 deletions src/main/kotlin/command/CommandRecall.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.tiedan.command

import com.tiedan.MessageRecorder.from
import com.tiedan.MessageRecorder.quote
import com.tiedan.MessageRecorder.target
import com.tiedan.TiedanGame
import com.tiedan.TiedanGame.logger
import com.tiedan.config.BotConfig
import com.tiedan.plugindata.AdminListData
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.CommandSenderOnMessage
import net.mamoe.mirai.console.command.IllegalCommandArgumentException
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.console.util.ContactUtils.render
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.message.data.MessageSource.Key.recall

object CommandRecall : SimpleCommand(
owner = TiedanGame,
primaryName = "recall",
secondaryNames = arrayOf("撤回"),
description = "撤回消息"
) {
@Handler @ConsoleExperimentalApi
suspend fun CommandSender.handle(contact: Contact? = null) {
if (AdminListData.AdminList.contains(user?.id).not() && AdminListData.AdminList.contains(0).not() && user?.id != BotConfig.master && user != null) {
sendMessage("未持有管理员权限")
return
}
val message = try {
val source = when {
contact is Member -> from(member = contact)
contact != null -> target(contact = contact)
this is CommandSenderOnMessage<*> -> quote(event = fromEvent)
else -> throw IllegalCommandArgumentException("参数不足以定位消息")
}
if (source != null) {
source.recall()
"${contact?.render() ?: source.fromId}的消息撤回成功"
} else {
"${contact?.render().orEmpty()}未找到消息"
}
} catch (ex: Exception) {
logger.warning(ex)
"出现错误:${ex.message}"
}
sendMessage(message)
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/config/BotConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ object BotConfig : AutoSavePluginConfig("BotConfig") {
@ValueDescription("时区")
var TimeZone : MutableList<String> by value(mutableListOf("Asia/Shanghai", "北京"))

@ValueDescription("记录消息上限")
var recordLimit: Int by value(500)

@ValueDescription("启用专注功能")
var focus_enable: Boolean by value(false)

Expand Down

0 comments on commit 01fbb5e

Please sign in to comment.