Skip to content

Commit

Permalink
feat: enhancement (#13)
Browse files Browse the repository at this point in the history
- add commenting drift bottle feature
- add a composite command for querying and deleting items in the sea
- add an option for disabling certain message types
- add a reply when commands invocation is throttled

Signed-off-by: Samarium <[email protected]>

Co-authored-by: Samarium <[email protected]>
  • Loading branch information
LaoLittle and Samarium150 authored Jan 31, 2022
1 parent 5fed9b6 commit 71b3383
Show file tree
Hide file tree
Showing 17 changed files with 598 additions and 128 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ plugins {
}

group = "io.github.samarium150"
version = "1.5.0"
version = "1.6.0"

repositories {
mavenLocal()
maven("https://maven.aliyun.com/repository/public")
maven("https://maven.aliyun.com/repository/central")
mavenCentral()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
*/
package io.github.samarium150.mirai.plugin.driftbottle

import io.github.samarium150.mirai.plugin.driftbottle.command.JumpInto
import io.github.samarium150.mirai.plugin.driftbottle.command.Pickup
import io.github.samarium150.mirai.plugin.driftbottle.command.ThrowAway
import io.github.samarium150.mirai.plugin.driftbottle.config.CommandConfig
import io.github.samarium150.mirai.plugin.driftbottle.config.ContentCensorConfig
import io.github.samarium150.mirai.plugin.driftbottle.config.GeneralConfig
import io.github.samarium150.mirai.plugin.driftbottle.config.ReplyConfig
import io.github.samarium150.mirai.plugin.driftbottle.command.*
import io.github.samarium150.mirai.plugin.driftbottle.config.*
import io.github.samarium150.mirai.plugin.driftbottle.data.CommentData
import io.github.samarium150.mirai.plugin.driftbottle.data.ContentCensorToken
import io.github.samarium150.mirai.plugin.driftbottle.data.Sea
import io.github.samarium150.mirai.plugin.driftbottle.util.alsoSave
import io.ktor.client.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
Expand All @@ -37,7 +34,7 @@ object MiraiConsoleDriftBottle : KotlinPlugin(
JvmPluginDescription(
id = "io.github.samarium150.mirai.plugin.mirai-console-drift-bottle",
name = "Drift Bottle",
version = "1.5.0",
version = "1.6.0",
) {
author("Samarium150")
info("简单的漂流瓶插件")
Expand All @@ -46,19 +43,33 @@ object MiraiConsoleDriftBottle : KotlinPlugin(

lateinit var client: HttpClient

override fun onEnable() {
// 重载数据
GeneralConfig.reload()
ReplyConfig.reload()
CommandConfig.reload()
private fun init() {
// 重载只读配置
AdvancedConfig.alsoSave()
GeneralConfig.alsoSave()
ReplyConfig.alsoSave()
CommandConfig.alsoSave()

// 重载配置
ContentCensorConfig.reload()

// 重载数据
ContentCensorToken.reload()
CommentData.reload()
Sea.reload()

// 注册命令
JumpInto.register()
Pickup.register()
ThrowAway.register()
SeaOperation.register()
if (GeneralConfig.incrementalBottle)
Comment.register()
}

override fun onEnable() {
// 初始化插件
init()

// 初始化 HTTP 客户端
if (GeneralConfig.enableContentCensor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2020-2021 Samarium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*/
package io.github.samarium150.mirai.plugin.driftbottle.command

import io.github.samarium150.mirai.plugin.driftbottle.MiraiConsoleDriftBottle
import io.github.samarium150.mirai.plugin.driftbottle.config.CommandConfig
import io.github.samarium150.mirai.plugin.driftbottle.data.CommentData
import io.github.samarium150.mirai.plugin.driftbottle.util.indexOfBottle
import io.github.samarium150.mirai.plugin.driftbottle.util.isNotOutOfRange
import io.github.samarium150.mirai.plugin.driftbottle.util.randomDelay
import net.mamoe.mirai.console.command.CommandSenderOnMessage
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.contact.nameCardOrNick
import net.mamoe.mirai.message.data.PlainText
import net.mamoe.mirai.message.data.SingleMessage

/**
* @author LaoLittle
*/
object Comment : SimpleCommand(
MiraiConsoleDriftBottle,
primaryName = "comment",
secondaryNames = CommandConfig.comment,
description = "评论漂流瓶"
) {
private val comments by CommentData.Companion::comments

@ExperimentalCommandDescriptors
@ConsoleExperimentalApi
override val prefixOptional: Boolean = true

override val usage: String
get() = "评论 内容 漂流瓶序号"

@Suppress("unused")
@Handler
suspend fun CommandSenderOnMessage<*>.handle(
comment: SingleMessage,
index: Int? = indexOfBottle[fromEvent.subject.id]?.takeIf { it.isNotEmpty() }?.peek()?.plus(1)

) {
if (comment !is PlainText) {
sendMessage("评论只能包含纯文本!")
return
}
val commentStr = comment.content
val realIndex = index?.minus(1)
if (isNotOutOfRange(realIndex)) {
val nick = fromEvent.sender.nameCardOrNick
val id = fromEvent.sender.id
comments[realIndex]?.add(CommentData(id, nick, commentStr)) ?: comments.put(
realIndex,
mutableListOf(CommentData(id, nick, commentStr))
)
randomDelay()
sendMessage("已评论漂流瓶 $index") // 或许可由用户自行配置
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ object JumpInto : SimpleCommand(
@Handler
suspend fun CommandSender.handle() {
val sender = user
if (sender == null) randomDelay().also {
if (sender == null)
sendMessage(ReplyConfig.jumpInto.replace("%num", Sea.contents.size.toString()))
} else {
if (!lock(sender.id)) return
else {
if (!lock(sender.id)) {
sendMessage(ReplyConfig.inCooldown)
return
}
val subject = subject
val owner = Owner(
sender.id,
Expand All @@ -64,12 +67,12 @@ object JumpInto : SimpleCommand(
) else null
val body = Item(Item.Type.BODY, owner, source)
Sea.contents.add(body)
randomDelay().also {
sendMessage(ReplyConfig.jumpInto.replace("%num", Sea.contents.size.toString())).also {
delay(GeneralConfig.perUse * 1000L)
unlock(sender.id)
}
runCatching {
randomDelay()
sendMessage(ReplyConfig.jumpInto.replace("%num", (Sea.contents.size - 1).toString()))
delay(GeneralConfig.perUse * 1000L)
}
unlock(sender.id)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ import io.github.samarium150.mirai.plugin.driftbottle.config.GeneralConfig
import io.github.samarium150.mirai.plugin.driftbottle.config.ReplyConfig
import io.github.samarium150.mirai.plugin.driftbottle.data.Item
import io.github.samarium150.mirai.plugin.driftbottle.data.Sea
import io.github.samarium150.mirai.plugin.driftbottle.util.disableAt
import io.github.samarium150.mirai.plugin.driftbottle.util.lock
import io.github.samarium150.mirai.plugin.driftbottle.util.randomDelay
import io.github.samarium150.mirai.plugin.driftbottle.util.unlock
import io.github.samarium150.mirai.plugin.driftbottle.util.*
import kotlinx.coroutines.delay
import net.mamoe.mirai.console.command.CommandSenderOnMessage
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.message.data.MessageChain
import java.util.*

object Pickup : SimpleCommand(
Expand All @@ -45,28 +43,41 @@ object Pickup : SimpleCommand(

@Suppress("unused")
@Handler
suspend fun CommandSenderOnMessage<*>.handle() {
val sender = fromEvent.sender
val subject = fromEvent.subject
if (!lock(sender.id)) return
if (Sea.contents.size == 0) {
randomDelay().also {
sendMessage(ReplyConfig.noItem)
suspend fun CommandSenderOnMessage<*>.handle(index: Int = Random().nextInt(Sea.contents.size) + 1) {
val realIndex = index - 1
if (isNotOutOfRange(realIndex)) {
val sender = fromEvent.sender
val subject = fromEvent.subject
if (!lock(sender.id)) {
sendMessage(ReplyConfig.inCooldown)
return
}
if (Sea.contents.size == 0) {
randomDelay()
unlock(sender.id)
sendMessage(ReplyConfig.noItem)
return
}
return
}
val index = Random().nextInt(Sea.contents.size)
val item = Sea.contents[index]
if ((item.type == Item.Type.BOTTLE && !GeneralConfig.incrementalBottle)
|| (item.type == Item.Type.BODY && !GeneralConfig.incrementalBody)
)
Sea.contents.removeAt(index)
randomDelay().also {
sendMessage(disableAt(item.toMessageChain(subject), subject)).also {
val item = Sea.contents[realIndex]
if ((item.type == Item.Type.BOTTLE && !GeneralConfig.incrementalBottle)
|| (item.type == Item.Type.BODY && !GeneralConfig.incrementalBody)
) {
Sea.contents.removeAt(realIndex)
rearrangeComments(realIndex)
} else {
indexOfBottle[subject.id]?.remove(realIndex)
indexOfBottle[subject.id]?.push(realIndex) ?: indexOfBottle.put(
subject.id,
Stack<Int>().put(realIndex)
)
}
runCatching {
randomDelay()
val message = item.toMessage(subject, realIndex)
sendMessage(if (message is MessageChain) disableAt(message, subject) else message)
delay(GeneralConfig.perUse * 1000L)
unlock(sender.id)
}
unlock(sender.id)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright (c) 2020-2021 Samarium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*/
package io.github.samarium150.mirai.plugin.driftbottle.command

import io.github.samarium150.mirai.plugin.driftbottle.MiraiConsoleDriftBottle
import io.github.samarium150.mirai.plugin.driftbottle.config.CommandConfig
import io.github.samarium150.mirai.plugin.driftbottle.data.Sea
import io.github.samarium150.mirai.plugin.driftbottle.util.*
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.CompositeCommand

/**
* @author LaoLittle
*/
object SeaOperation : CompositeCommand(
MiraiConsoleDriftBottle,
primaryName = "sea",
secondaryNames = CommandConfig.seaOperation,
description = "漂流瓶操作复合指令"
) {
@Suppress("unused")
@SubCommand("del", "rm")
suspend fun CommandSender.remove(
index: Int? = subject?.let { sub ->
indexOfBottle[sub.id]?.takeIf { it.isNotEmpty() }?.pop()?.plus(1)
}
) {
val realIndex = index?.minus(1)
if (isNotOutOfRange(realIndex)) {
val result = runCatching {
Sea.contents.removeAt(realIndex)
rearrangeComments(realIndex)
}.onFailure { e ->
if (e !is IndexOutOfBoundsException) MiraiConsoleDriftBottle.logger.error(e)
}
randomDelay()
sendMessage(if (result.isSuccess) "已删除漂流瓶$index" else "删除漂流瓶$index 失败")
}
}

@Suppress("unused")
@SubCommand("query", "get")
suspend fun CommandSender.query(
index: Int? = subject?.let { sub ->
indexOfBottle[sub.id]?.takeIf { it.isNotEmpty() }?.peek()?.plus(1)
}
) {
val realIndex = index?.minus(1)
if (isNotOutOfRange(realIndex)) {
val result = runCatching {
val item = Sea.contents[realIndex]
"""
漂流瓶序号: $index
${item.source?.let { "漂流瓶发送群: ${it.name}(${it.id})" } ?: "此漂流瓶是私聊发送"}
漂流瓶发送人: ${item.owner.name}(${item.owner.id})
漂流瓶发送时间: ${item.timestamp.timestampToString()}
""".trimIndent()
}.onFailure { e ->
if (e !is IndexOutOfBoundsException) MiraiConsoleDriftBottle.logger.error(e)
}
sendMessage(result.getOrNull() ?: "无法找到漂流瓶$index")
}
}
}
Loading

0 comments on commit 71b3383

Please sign in to comment.