-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add MessageRecorder and recall command
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters