generated from project-mirai/mirai-console-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
5fed9b6
commit 71b3383
Showing
17 changed files
with
598 additions
and
128 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
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
75 changes: 75 additions & 0 deletions
75
src/main/kotlin/io/github/samarium150/mirai/plugin/driftbottle/command/Comment.kt
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,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") // 或许可由用户自行配置 | ||
} | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
src/main/kotlin/io/github/samarium150/mirai/plugin/driftbottle/command/SeaOperation.kt
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,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") | ||
} | ||
} | ||
} |
Oops, something went wrong.