Files
FancyBot/src/main/kotlin/cn/rtast/fancybot/commands/misc/BullShitGenerateCommand.kt
T

72 lines
2.5 KiB
Kotlin
Raw Normal View History

2024-09-26 15:26:07 +08:00
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/26
*/
package cn.rtast.fancybot.commands.misc
2024-10-03 17:15:12 +08:00
import cn.rtast.fancybot.annotations.CommandDescription
2024-09-28 16:31:55 +08:00
import cn.rtast.fancybot.configManager
2024-09-26 15:26:07 +08:00
import cn.rtast.fancybot.entity.bullshit.BullShit
import cn.rtast.fancybot.util.Resources
import cn.rtast.fancybot.util.str.fromJson
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.NodeMessageChain
import cn.rtast.rob.util.ob.OneBotListener
import kotlin.random.Random
2024-10-03 17:15:12 +08:00
@CommandDescription("狗屁不通文章生成器")
2024-09-26 15:26:07 +08:00
class BullShitGenerateCommand : BaseCommand() {
override val commandNames = listOf("/gpbt", "狗屁不通")
private val bullShitData = String(Resources.loadFromResourcesAsBytes("misc/bull_shit.json")!!)
.fromJson<BullShit>()
private fun <T> shuffleList(list: List<T>): Iterator<T> {
val pool = mutableListOf<T>().apply { addAll(list) }.toMutableList()
repeat(1) { pool.addAll(list) }
return generateSequence {
pool.shuffle()
pool
}.flatten().iterator()
}
private fun getFamousQuote(nextQuote: Iterator<String>, beforeList: List<String>, afterList: List<String>): String {
var quote = nextQuote.next()
quote = quote.replace("a", beforeList.random())
quote = quote.replace("b", afterList.random())
return quote
}
private fun newParagraph(): String {
return ".\n\n "
}
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
val topic = args.joinToString(" ")
val nextFamousQuote = shuffleList(bullShitData.famous)
val nextBosh = shuffleList(bullShitData.bosh)
val output = StringBuilder()
while (output.length < 6000) {
val branch = Random.nextInt(100)
if (branch < 5) {
output.append(newParagraph())
} else if (branch < 20) {
output.append(getFamousQuote(nextFamousQuote, bullShitData.before, bullShitData.after))
} else {
output.append(nextBosh.next())
}
}
val finalText = output.toString().replace("x", topic)
val node = NodeMessageChain.Builder()
2024-09-28 16:31:55 +08:00
.addMessageChain(MessageChain.Builder().addText(finalText).build(), configManager.selfId)
2024-09-26 15:26:07 +08:00
.build()
message.reply(node)
message.sender.poke()
2024-09-26 15:26:07 +08:00
}
}